Slide 1

Slide 1 text

@_cha1tanya Rescuing a SPAghetti React project Prathamesh Sonpatki Last9

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Build mode

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Reality sinks in

Slide 6

Slide 6 text

Bad code

Slide 7

Slide 7 text

Bad code • Hard to extend

Slide 8

Slide 8 text

Bad code • Hard to extend • Duplication

Slide 9

Slide 9 text

Bad code • Hard to understand

Slide 10

Slide 10 text

Bad code • Hard to understand • Lacks consistency

Slide 11

Slide 11 text

Bad code • Hard to understand • Lacks consistency • Cascading failures

Slide 12

Slide 12 text

Bad code to Good code

Slide 13

Slide 13 text

Bad code to Good code Confidence Safety Shared mental model

Slide 14

Slide 14 text

Confidence

Slide 15

Slide 15 text

Doing one thing well

Slide 16

Slide 16 text

function validateTimestamp(atTimestamp) { if (atTimestamp > 0) { return true; } return false; }

Slide 17

Slide 17 text

function validateTimestamp(atTimestamp) { if (atTimestamp > 0) { dispatch(); return true; } return false; }

Slide 18

Slide 18 text

function validateTimestamp(atTimestamp, currentUser) { if (atTimestamp > 0) { if (currentUser.admin) { dispatch() } return true; } return false; }

Slide 19

Slide 19 text

function validateTimestamp(atTimestamp) { if (atTimestamp > 0) { return true; } return false; } validTs = validateTimestamp(atTimestamp); if (validTs && currentUser.admin) { dispatch() }

Slide 20

Slide 20 text

Tests

Slide 21

Slide 21 text

Cypress

Slide 22

Slide 22 text

Hooks

Slide 23

Slide 23 text

function useDidUpdateEffect(fn, inputs) { const fncRef = useRef(); fncRef.current = fn; const didMountRef = useRef(false); useEffect(() => { if (!didMountRef.current) { didMountRef.current = true; } else { return fncRef.current(); } // eslint-disable-next-line react-hooks/exhaustive-deps }, inputs); } useDidUpdateEffect

Slide 24

Slide 24 text

useCallback true === true // true false === false // true 1 === 1 // true 'a' === 'a' // true {} === {} // false [] === [] // false () => {} === () => {} // false const z = {} z === z // true

Slide 25

Slide 25 text

useCallback const isSelected = useCallback((item) => selectedItems.includes(item), [selectedItems]); return ( {listItems.map((item) => ( onSelect(item)} disableGutters classes={{ root: classes.listItemRoot, selected: classes.listItemSelected }} selected={isSelected(item)} button > {item} ))} );

Slide 26

Slide 26 text

react-hooks/exhaustive-deps

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

useEffect(() => { if (graphType === GraphType.Custom && views.length === 0) { setSelectedNodeIdents([]); } }, [views.length]);

Slide 29

Slide 29 text

useEffect(() => { if (graphType === GraphType.Custom && views.length === 0) { setSelectedNodeIdents([]); } }, [views.length, graphType]);

Slide 30

Slide 30 text

Safety Toolkit

Slide 31

Slide 31 text

TypeScript

Slide 32

Slide 32 text

export type Timestamp = number; export type MaybeTimestamp = Timestamp| null | undefined; export interface TimeRange { from: Timestamp; to: Timestamp; } export enum ViewType { Custom = 'custom', Automatic = 'automatic', }

Slide 33

Slide 33 text

function validateTimestamp(atTimestamp: Timestamp): boolean { .... }

Slide 34

Slide 34 text

export interface Query { where?: { name: string }; select?: { type: 'custom' | ‘automatic' }; } export interface QueryWithLimit extends Query { limit: number; }

Slide 35

Slide 35 text

Code reusability

Slide 36

Slide 36 text

No surprises

Slide 37

Slide 37 text

ESLint & Prettier

Slide 38

Slide 38 text

eslint --fix --ext .js,.jsx,.ts,.tsx src --color --max-warnings 0

Slide 39

Slide 39 text

Shared mental model

Slide 40

Slide 40 text

knowledge structure(s) held by each member of a team that enables them to form accurate explanations and expectations

Slide 41

Slide 41 text

Sequence diagrams

Slide 42

Slide 42 text

Flowcharts

Slide 43

Slide 43 text

Comments

Slide 44

Slide 44 text

Bad code to Good code Confidence Safety Shared mental model

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

Thank you! last9.io prathamesh.tech