Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Clean Code in Javascript

Clean Code in Javascript

How to write clean code in Javascript

Sonny Lazuardi

January 15, 2017
Tweet

More Decks by Sonny Lazuardi

Other Decks in Technology

Transcript

  1. Variables • Use the same vocabulary for the same type

    of variable • Use explanatory variables • Avoid Mental Mapping • Use searchable names • Don't add unneeded context • Use meaningful and pronounceable variable names • Use default arguments instead of short circuiting or conditionals
  2. Use the same vocabulary for the same type of variable

    Use explanatory variables Avoid Mental Mapping ✅ ❎ ✅ ❎ ❎ ✅
  3. Functions • Favor functional programming over imperative programming • Avoid

    Side Effects • Don't write to global functions • Function arguments (2 or fewer ideally) • Functions should do one thing • Function names should say what they do • Functions should only be one level of abstraction • Remove duplicate code • Set default objects with Object.assign • Don't use flags as function parameters
  4. Imperative Programming vs Functional Programming • Functional programming is declarative:

    application state flows through pure functions • Imperative: object oriented programming, where application state is usually shared and colocated with methods in objects.
  5. Imperative Programming vs Functional Programming • Imperative programs describe the

    specific steps used to achieve the desired results — the flow control: How to do things. • Declarative programs abstract the flow control process, and instead spend lines of code describing the data flow: What to do. The how gets abstracted away.
  6. What is Functional Programming? The process of building software by

    composing pure functions, avoiding shared state, mutable data, and side-effects. • Eric Elliot on Medium
  7. Pure Function • Given the same inputs, always returns the

    same output • Has no side-effects f(x) = x + 1 f(1) = 2 g(x) = x + 2 g(f(1)) = 4 f(props) = rendered view