are you ready for clean code?
DESCRIPTION
What, when, how, and for whom is clean code? It's just ordinary weekly devTalk material at Vox Teneo Indonesia.TRANSCRIPT
Clean CodeWhisnu | Vox Teneo Indonesia
Reason?
• We Are Programmer• We Want to be a Better Programmer
A = 1B = A
Print BA = 1C = AB = C
Print B - Same Task- Same Effort- Same Test Result- No BUG’s
Who’s code you choose?
Who’s code you choose?
Does it matter how the code is written?
or does it only matter that it works?
Good Code Matterbecause it doesn't end there!
• You Will Have To Go Back• Someone Else Will Have To Go Back• They Don’t Teach That in School• Good Code Matter• Definition• Degrees of Bad• The Netscape Fallacy• Why Write Good Code• But Doesn't Good Code Cost More Up Front?
Boy Scout RuleLeave the campground cleaner than you found it.
CLEAN CODE = WTF’sMinutes
Meaningful NamesFunctions
CommentsFormatting
Objects and Data StructuresError Handling
Unit Tests
Meaningful Names
int d; // elapsed time in days
string htmlOutput; // form configuration html
DataGrid dataGrid1;
DataGrid dataGrid2;
Label label1;
Object myObj;int elapsedTimeInDays;
string formConfiguration;
DataGrid dgCustomers;
DataGrid dgProducts;
Label lblFirstName;
Object objCustomer;
Functions
• Small and Should be smaller than that• Do One Thing• One Level of Abstraction
• getHtml();• String pagePathName = PathParser.render(path);• .append(“\n”);
• Switch Statement (break it for SRP/OCP reason)• Ideal number of arguments is ZERO• Object Argument (hidden dependency, inject dependency, SRP)• Have No Side Effect (setAndCheckIfExists())
Comment
• Don’t Comment Bad Code!• Explain on Code• Legal comment, TODO comment, JavaDoc, PHPDocumenter• Clarification• Warning of Consequences• No Mumbling• Redudant Comment• Position Marker• Closing Brace comment• Attributes and ByLine• Comment Out Code (your note code)
Formating
• The Purpose of Formatting?• Vertical Matter, Horizontal Alignment• Breaking Identation• Team Rules
Object and Data Structures*
• The Law Of Demeter (Train Wrecks)
* lol + \m/ =
Error Handling
• Prefer Returning an Exceptions• Don’t Return null
Unit Tests
The Three Laws of TDD:• First Law You may not write production code until you have written a
failing unit test.• Second Law You may not write more of a unit test than is sufficient to
fail, and not compiling is failing.• Third Law You may not write more production code than is sufficient
to pass the currently failing test.
In Practice it’s all about RTM
READABILITYTESTABILITY
MAINTAINABILITY
From STUPID to SOLID
S T UPI D
SingletonTight Coupling
UntestabilityPremature Optimization
Indescriptive NamingDuplication
SingletonTight Coupling
UntestabilityPremature Optimization
Indescriptive NamingDuplication
SingletonTight Coupling
UntestabilityPremature Optimization
Indescriptive NamingDuplication
SingletonTight Coupling
UntestabilityPremature Optimization
Indescriptive NamingDuplication
SingletonTight Coupling
UntestabilityPremature Optimization
Indescriptive NamingDuplication
SingletonTight Coupling
UntestabilityPremature Optimization
Indescriptive NamingDuplication
SingletonTight Coupling
UntestabilityPremature Optimization
Indescriptive NamingDuplication
S OL I D
Single Responsibility PrincipleOpen/Close Principle
Liskov Substitution PrincipleInterface Segregation Principle
Dependency Inversion Principle
Single Responsibility PrincipleOpen/Close Principle
Liskov Substitution PrincipleInterface Segregation Principle
Dependency Inversion Principle
Single Responsibility PrincipleOpen/Close Principle
Liskov Substitution PrincipleInterface Segregation Principle
Dependency Inversion Principle
Single Responsibility PrincipleOpen/Close Principle
Liskov Substitution PrincipleInterface Segregation Principle
Dependency Inversion Principle
Single Responsibility PrincipleOpen/Close Principle
Liskov Substitution PrincipleInterface Segregation Principle
Dependency Inversion Principle
Single Responsibility PrincipleOpen/Close Principle
Liskov Substitution PrincipleInterface Segregation Principle
Dependency Inversion Principle
For Coding Standard ?
…and any Questions?