clean code coding like a professional
TRANSCRIPT
Clean CodeCoding like a professional
XPDay Vietnam @ Hanoi May, 2016
Nguyễn Khắc Nhật | Learning Master @ Học Viện Agile
| Agile | Scrum | Kanban | Lean
| Java | Scala | Typescript | NodeJS
| Virtual DOM | Bobril | ReactJS | AngularJS
The snake game
What is the difference?
Good CodeBad Code
Bad code looks like this...
or even this...
Good code looks like this...
or this...
Productivity trend
So...
What is clean code?
“Clean code is simple and direct. Clean code reads like well-written prose. Clean code never obscures the designer’s intent but rather is full of crisp abstractions and straightforward lines of control”
Grady Booch Object Oriented Analysis
What is clean code?
“Clean code can be read, and enhanced by an developer other than its original author. It has unit and acceptance tests. It has meaningful names. It provides one way rather than many ways of doing one thing. It has minimal dependencies,...”
Dave Thomas OTI, Eclipse
What is clean code?
“I could list all of the qualities that I notice in clean code, but there is one overarching quality that leads to all of them. Clean code always looks like it was written by someone who cares...”
Michael FeathersWorking effectively with Legacy Code
What is clean code?
“In recent years I begin, and nearly end, with Beck’s rules of simple code. In priority order, simple code:
Run all the tests;
Contains no duplication;
Express all the design ideas that are in the system
Minimizes the numbers of entities such as classes, methods, functions,...”
Ron JeffriesExtreme Programming Installed
Extreme Programing Adventures in C#
and...
What make bad or good code?
Names Functions Comments Formats
Objects andData Structure
Error Handling
Unit Tests
Boundaries
Classes System ...
Names
Meaningful Names
Meaningful Names
Naming Rules
Use Intention-Revealing Names
Avoid Disinformation
Make meaningful Distinctions
Use Pronounceable Names
Use Searchable Names
Avoid Encodings
Avoid Mental Mapping
Class Names
Method Names
Don’t Be Cute
Pick One Word per Concept
Don’t Pun
Use Solution Domain Names
Use Problem Domain Names
Add meaningful Context
Don’t Add Gratuitous Context
Try with this code….
and then….
and, how about this...
Functions
Function Arguments
Function Rules
Functions Should be Small
Functions Should Do Only One Thing
One level of Abstraction per Function
Use Descriptive Names
Function Arguments
Have No Side Effects
Command/Query Separation
Prefer Exceptions to Returning Error Codes
Don’t Repeat Yourself
Multiple Returns
Demo with compareTo() method
The beauty of functional programming
Comments
"Don’t comment bad code—rewrite it."
Formats
Formatting
Vertical Formatting
Horizontal Formatting
The team’s rules first!!!
Conventions
Objects andData Structure
Objects and Data structures
Object Data Structure
Law of Demeter
Given method ƒ of class C, ƒ should only call methods of:
● C
● An object created by ƒ
● An object passed as an argument to ƒ
● An instance variable of C
...
Clean, Clean, Clean
Pair Programming
like this …
CodingDojo
CodeRetreat
THANK YOU!