Washing your code
Washing your code
A book on clean code for frontend developers
About the Book
On dozens of examples, based on production code, I’ll show you how to make your code more readable and maintainable, and how to avoid hard-to-track bugs. I’ll show you code smells and antipatterns I often see during code reviews (and I review a lot of code every day!) and will walk you through the refactoring process to make your code better.
These techniques help me every day to write code that my colleagues will have no problems working with. All examples in the book are written in JavaScript with a bit of TypeScript, React, CSS, and HTML.
Table of Contents
- 1 Preface
- 2 Acknowledgments
- 3 Avoid loops
- 3.1 Replacing loops with array methods
- 3.2 Implied semantics of array methods
- 3.3 Dealing with side effects
- 3.4 Sometimes loops aren’t so bad
- 3.5 Iterating over objects
- 3.6 But aren’t array methods slow?
- 4 Avoid conditions
- 4.1 Unnecessary conditions
- 4.2 Processing arrays
- 4.3 Deduplicating an algorithm
- 4.4 Optional function parameters
- 4.5 Early return
- 4.6 Repeated conditions
- 4.7 Tables or maps
- 4.8 Formulas
- 4.9 Nested ternaries
- 5 Avoid reassigning variables
- 5.1 Don’t reuse variables
- 5.2 Incremental computations
- 5.3 Building complex objects
- 5.4 Avoid Pascal style variables
- 5.5 Avoid temporary variables for function return values
- 5.6 Indeterminate loops
- 5.7 Help the brain with conventions
- 6 Avoid mutation
- 6.1 Avoid mutating operations
- 6.2 Beware of the mutating array methods
- 6.3 Avoid mutation of function parameters
- 6.4 Make mutations explicit if they are necessary
- 6.5 Updating objects
- 6.6 Even mutation is not so bad sometimes
- 7 Avoid comments
- 7.1 Getting rid of comments (or not)
- 7.2 Good comments
- 7.3 Bad comments
- 8 Naming is hard
- 8.1 Negative booleans are not not hard to read
- 8.2 The larger the scope, the longer the name
- 8.3 The shorter the scope the better
- 8.4 Abbreviations and acronyms
- 8.5 Prefixes and suffixes
- 8.6 Dealing with updates
- 8.7 Beware of incorrect names
- 8.8 Beware of abstract and imprecise names
- 8.9 Use common terms
- 8.10 Use a single term for each concept
- 8.11 Use common opposite pairs
- 8.12 Check the spelling of your names
- 8.13 Use destructuring
- 8.14 Avoid unnecessary variables
- 8.15 Tips to avoid name clashes
- 9 Code style
- 9.1 Not all code styles are good
- 9.2 Obsolete code styles
- 9.3 Nonsensical code styles
- 9.4 Improving readability
- 9.5 Christmas trees vs. sausages
- 9.6 Make it easy to remember and use
- 9.7 The rest doesn’t matter
- 9.8 Conclusion
- 10 Constants
- 10.1 Making magic numbers less magic
- 10.2 Not all numbers are magic
- 10.3 Group related constants
- 11 Separate “what” and “how”
- 12 Don’t be clever
- 12.1 Dark patterns of JavaScript
- 12.2 Gray areas
- 13 Divide and conquer, or merge and relax
- 13.1 Let abstractions grow
- 13.2 Separate code that changes often
- 13.3 Keep together code that changes together
- 13.4 Sweep that ugly code under the rug
- 13.5 Bless the inline refactoring!
- 14 Don’t make me think
- 14.1 Make differences in code obvious
- 14.2 Write parallel code
- 14.3 Name things
- 15 Make impossible states impossible
- 16 Don’t try to predict the future
- 17 Become a code scout
- 18 Make the code greppable
- 19 Cargo cult programming
- 19.1 Never write functions longer than…
- 19.2 Always comment your code
- 19.3 Always use constants for magic numbers
- 19.4 Never repeat yourself
- 19.5 Never say never
- 20 Not invented here syndrome
- 20.1 What’s wrong with in-house solutions
- 20.2 Why third-party libraries might be better
- 20.3 What to keep in mind when using third-party libraries
- 20.4 My approach to using third-party libraries
- 21 Refactoring is inevitable
- 22 Code is evil
- 23 Tooling
- 23.1 Linters
- 23.2 Other tools
- 24 Resources
- 24.1 Books
- 24.2 Articles
- 24.3 Talks
- 25 Conclusion
Other books by this author
The Leanpub 60-day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms
80% Royalties. Earn $16 on a $20 book.
We pay 80% royalties. That's not a typo: you earn $16 on a $20 sale. If we sell 5000 non-refunded copies of your book or course for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earnedover $12 millionwriting, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them