CONCEPTS OF CLEAN CODE ADAPTED
FOR JAVASCRIPT
by
Jennifer Zeiser
[1]
What is Clean Code?
“I like my code to be
elegant and efficient. […]
Clean Code does one
thing well.”
Bjarne Stroustrup
(inventor of C++) [2]
“Clean code is simple
and direct. Clean code reads
like well-written prose.” Grady Booch
(inventor of UML) [3]
What is Clean Code?
Dave Thomas
(founder of OTI) [4]
Dave Thomas
(author of
“The Pragmatic
Programmer”) [5]
“Clean code can be
read, and enhanced by a
developer other than its
original author.”
[6]
“There are only two hard things in
Computer Science: cache
invalidation and naming things.”
Phil Karlton [7]
Intention-revealing names
Intention-revealing names
Explanatory names
Pronounceable names
Searchable names
Searchable names
Avoid mental mapping
Avoid mental mapping
Consistent names
Don’t add unnecessary
context
Don’t add unnecessary
context
Meaningful names
•Intention-revealing
•Explanatory
•Pronounceable
•Searchable (no magic numbers, strings, …)
•Explicit (avoids mental mapping)
•Consistent (one word per concept)
•Without unnecessary context
“The first rule of functions is that they
should be small.
The second rule of functions is that
they should be even smaller than that.”
Robert C. Martin [8]
Functions should do one thing
Functions should do one thing
Function arguments
“The ideal number of arguments for
a function is zero (niladic). Next
comes one (monadic), followed
closely by two (dyadic).
Three arguments (triadic) should be
avoided where possible. More than
three (polyadic) requires very
special justification […]”Robert C. Martin [8]
Function arguments: polyadic
Function arguments: flags
Function arguments: flags
Avoid Side Effects I
Avoid Side Effects I
Avoid Side Effects II
Don’t repeat yourself
Don’t repeat yourself
No monkey patching
Functional over imperative
programming
Functional over imperative
programming
“The first rule of classes is that they
should be small.
The second rule of classes is that they
should be even smaller than that.”
Robert C. Martin [8]
Prefer ES2015 (ES6) classes
Single Responsibility Principle
Single Responsibility Principle
Use method chaining
Use method chaining
Resources
• Clean Code - A Handbook of Agile Software
Craftsmanship by Robert C. Martin

• Clean Code Javascript by Ryan McDermot
Code samples
• Code samples were partially adapted from the book
“Clean Code: A Handbook of Agile Software
Craftsmanship” by Robert C. Martin, as well as from the
Github repository “clean-code-javascript” by Ryan
McDermott

• Remaining code samples were created by the author or
taken from the “real-world” with permission of the
respective creators
Images
• [1] https://www.thenile.com.au/books/robert-c-martin/clean-code-a-handbook-of-
agile-software-craftsmanship/9780132350884

• [2] https://www.technologyreview.com/s/407076/bjarne-stroustrup/

• [3] https://en.wikipedia.org/wiki/Grady_Booch

• [4] https://confengine.com/user/dave-thomas-2

• [5] https://en.wikipedia.org/wiki/Dave_Thomas_(programmer)

• [6] https://www.reddit.com/r/ProgrammerHumor/comments/7ym547/
the_only_valid_measurement_of_code_quality_is/

• [7] https://www.nndb.com/people/400/000031307/

• [8] https://en.wikipedia.org/wiki/Robert_C._Martin
Thank you
for your attention!
Any questions?

Concepts of Clean Code adapted for JavaScript - Tech'n'Drinks @myposter