Javascript Clean Code @petrabarus BandungJS 28 Ags 2017
Hi Everyone! I am Petra Novandi Now serves as Chief Technology Officer at UrbanIndo.com since 2011 - Contact me at Email: petra.barus@gmail.com Twitter: @petrabarus
WHAT AM I GOING TO TALK ABOUT?
Clean Code: A Handbook of Agile Software Craftsman It’s written for Java, but a lot of things are applicable for Javascript https://github.com/ryanmcdermott/cle an-code-javascript
WHAT IS CLEAN CODE?
Clean Code is…. Elegant Simple Efficient Straightforward Clear Readable Automatically Tested Cared
COST OF CLEAN CODE
Time Productivity Clean Code
Boy-scout Rule: Leave the code cleaner than you found it
WHY IS IT HARD TO WRITE CLEAN CODE IN JAVASCRIPT?
Javascript was 2nd class citizen Too flexible, not a lot of rules Dynamic and weak typed
Javascript is now 1st class citizen » Driven by User Experience ⋄ Comes V8 followed by NodeJS » Highly popular and developed » Efforts for Improvements ⋄ ES5, ES6, ES7, ES8
HOW TO WRITE CLEAN CODE?
1. MEANINGFUL NAMES
Use Meaningful and Pronounceable Variable Names
Use Searchable Names
Use Same Word for Same Concept
Avoid Mental Mapping
Class Name » Class name should have Noun or Noun Phrase. ⋄ e.g. Customer, Account, Parser
Function/Method » Function/method should have verb or verb phrase ⋄ getItem, render, renderItem, setName
Code Obfuscation Javascript code is published, don’t forget to obfuscate the code.
2. FUNCTIONS
Small 20 to 30 line of codes (or statements) or shorter My personal motto: A function can be read in one breathe
Function Should Do One Thing Easier to read Easier to test Easier to maintain
Bad
Good
Avoid Long Parameters Easier to read Easier to test Easier to maintain
Bad
Has No Side Effect
Don’t Write To Global Funcs
Favor Functional over Procedural
3. FORMATTINGS
Code is Communication
Vertical & Horizontal Formatting Vertical Vertical Openness Vertical Density Vertical Distance Vertical Ordering Horizontal Horizontal Openness Horizontal Alignment Indentation
Newspaper Metaphor Starts from headline First paragraph tells whole story Down to more details
Team Rules are Important
Use Tools: ESLint/JSLint/etc
Example configuration
Tips: Use git commit hook
4. COMMENTS
A good code explains itself
Example
Example of Good Comments Legal Comment Explanation of Intent Warning of Consequence Public API Documentation
Example of Bad Comments Unclear, redundant, misleading comments Changelog comments A comment instead of putting code into a separate function Banners, closing brace marker Commented-out code Documenting non-public API
5. TESTS
A Good Code is A Tested Code
Testing Tools for Javascript Jest Mocha Jasmine Chai Istanbul JSDOM Karma Sinon Phantom Chrome https://medium.com/powtoon-engineering/a-complete- guide-to-testing-javascript-in-2017-a217b4cd5a2a
Example test using Mocha & Chai
Coverage Example
Single Concept Per Test
6. CONCURRENCY
Avoid Callbacks Promise is good Async/Await is even better!
7. A
Let’s Wrap This Up!
Clean code is investment It’s possible to code clean in Javascript It takes discipline and teamwork, keep practising! Use newest ES. It’s there for this reason. Use automated tools to help
THANKS! Any questions? You can find me at » @petrabarus » petra.barus@gmail.com

Javascript Clean Code