BE CLEAN, MY FRIEND A modest review of Robert C. Martin “Clean Code, A handbook of Agile Craftmanship” book BCN } JUG
Jun 25, 2015
BE CLEAN, MY FRIENDA modest review ofRobert C. Martin
“Clean Code, A handbook of Agile Craftmanship” book
BCN } JUG
{ THANK YOU ALL }
BCN } JUG
{ DO WE NEED TO CODE BETTER? }
BCN } JUG{ Motivation }
{ MAIN QUESTION: WHY? }
1.You want to be a good programmer
2.You want to be better programmer
3.You like being part of best teams.
BCN } JUG{ Motivation }
{ REALLY ?? ?? }
BCN } JUG
int d; // elapsed time in days
or
int elapsedTimeInDays;int daysSinceCreation;
int fileAgeInDays;
{ Motivation }
{ WHAT IS CLEAN CODE ?? }
BCN } JUG
Set of techniques to help us achieve complexity from
simplicity.
Complex things =
composition of simple things.
{ Concepts }
{ TABLE OF CONTENTS }
BCN } JUG
❏ Baptism
❏ What & how are you building?
❏ Unexpected things
❏ What’s inside?
❏ Are you testing?
❏ Before development
❏ Tell me a (beautiful) story
❏ Last, but not least
❏ Recommended readings
BCN } JUG
/* NAMING GUIDELINES */
{ BAPTISM }
/* USE INTENTION-REVEALING NAMES */
BCN } JUG{ Baptism - Naming guidelines }
/* USE INTENTION-REVEALING NAMES */
BCN } JUG{ Baptism - Naming guidelines }
METHOD CODE
/* USE PRONOUNCEABLE NAMES */
BCN } JUG{ Baptism - Naming guidelines }
/* CLASSES AND METHODS */
BCN } JUG
❏ Classes should have noun or noun phrase names❏ payment❏ Page❏ singleUser❏ BankAccount❏ myObj
❏ Methods should have verbs or verbs phrase names❏ postPayment❏ checkPaymentTransfered❏ delPag❏ save❏ checkUserProfileAllowPaymentByCreditCard
{ Baptism - Naming guidelines }
/* CLASSES AND METHODS */
BCN } JUG{ Baptism - Naming guidelines }
BCN } JUG
{ WHAT & HOW ARE YOU BUILDING }
/* CLASSES & METHODS */
/* POLYMORPHISM OVER IF/ELSE */
BCN } JUG{ Classes & methods }
http://www.antiifcampaign.com/
/* FUNCTIONS SHOULD DO ONE THING*/
BCN } JUG{ Classes & methods }
/* OUTPUT ARGUMENTS */
BCN } JUG{ Classes & methods }
BCN } JUG
{ UNEXPECTED THINGS }
/* ERROR HANDLING */
/* PREFER EXCEPTIONS TO RETURN CODES */
BCN } JUG{ Error handling }
/* PREFER EXCEPTIONS TO RETURN CODES */
BCN } JUG{ Error handling }
/* DON’T RETURN NULL */
BCN } JUG{ Error handling }
/* DON’T PASS NULL */
BCN } JUG{ Error handling }
BCN } JUG
{ WHAT IS INSIDE? }
/* OBJECTS & DATA STRUCTURES */
/* HIDE STRUCTURES */
BCN } JUG{ Objects & data structures }
/* DATA ABSTRACTION */
BCN } JUG{ Objects & data structures }
/* DATA TRANSFER OBJECTS */
BCN } JUG{ Objects & data structures }
/* LAW OF DEMETER */
BCN } JUG
❏ Each unit should have only limited knowledge about other units: only units "closely" related to the current unit.
❏ Each unit should only talk to its friends; don't talk to strangers.
❏ Only talk to your immediate friends
{ Objects & data structures }
/* LAW OF DEMETER */
BCN } JUG
❏ A method 'm' of a class 'C' may only invoke the methods of the following kinds of elements:❏ 'C' itself❏ 'm's parameters❏ Any objects created/instantiated within 'm'❏ ‘C’'s direct component objects ❏ A global variable, accessible by 'C', in the
scope of 'm'
{ Objects & data structures }
/* LAW OF DEMETER */
BCN } JUG{ Objects & data structures }
BCN } JUG
{ ARE YOU TESTING ? }
/* UNIT TESTING ! */
/* USE A COVERAGE TOOL */
BCN } JUG{ Unit testing }
Cobertura
Sonar
/* TAKE CARE OF TESTS */
BCN } JUG
❏ Don’t skip trivial tests…
❏ But don’t do ONLY trivial tests!!!
❏ Tests the limits/boundaries of the conditions!
❏ One assert per test!
{ Unit testing }
/* KEEP TEST CLEAN */
BCN } JUG{ Unit testing }
BCN } JUG
{ BEFORE DEVELOPMENT ... }
/* DESIGN */
/* PRINCIPLES & ADVICES */
BCN } JUG
❏ Don’t repeat yourself (DRY): duplication is evil!
{ Design }
/* PRINCIPLES & ADVICES */
BCN } JUG
❏ Runs all the tests❏ They will help you❏ Writing tests leads to better designs!
❏ Refactoring = incrementally improvement
❏ Successive refinement
❏ Separation of main (construction objects <> use)
{ Design }
/* FACTORIES */
BCN } JUG{ Design }
/* DEPENDENCY INJECTION */
BCN } JUG{ Design }
Without Dependency Injection
(Hard dependency with the Interface and Implementation)
With Dependency Injection
(Low dependency with the Interface and Implementation,
because the Assembler does the job)
BCN } JUG
{ TELL ME A (BEAUTIFUL) STORY }
/* COMMENTS */
/* EXPLAIN YOURSELF IN CODE */
BCN } JUG{ Comments }
/* GOOD COMMENTS */
BCN } JUG{ Comments }
❏ Legal comments
❏ Informative comments
❏ Explanation of Intent
❏ Warning of Consequences
❏ TODOs
❏ JavaDocs
/* BAD COMMENTS */
BCN } JUG{ Comments }
/*** @param title The title* @param author The author*/
/*** CHANGES* -------------* 02-Oct-2001 Reorganize process* 15-Nov-2001 Added new Purchaser type* 21-Nov-2001 Some customer objects arrive without city set*/
/*** Returns the day of the month** @return the day of the month*/public int getDayOfMonth() { return dayOfMonth;}
BCN } JUG
{ LAST, BUT NOT LEAST }
BCN } JUG
❏ This is not a mantra
❏ (Try to) know your environment!
❏ Code is always live
❏ Refactor & test, test & refactor
/* THINK ABOUT IT */
{ Last, but not least }
BCN } JUG
/* BE CAREFUL */
Always code as if the guy who ends up maintaining your code will be a violent
psychopath who knows where you live. John F. Woods
{ Last, but not least }
{ DO YOU LIKE IT ?? READ IT ... }
BCN } JUG
Clean Code, A Handbook of Agile Software CraftmanshipRobert C. MartinPublication date: August 11, 2008ISBN-13: 978-0132350884
{ ... OR ... WATCH IT !! }
http://cleancoders.com/
{ RECOMMENDED READINGS }
BCN } JUG
Effective Java (2nd edition)Joshua BlochPublication Date: May 28, 2008ISBN-13: 978-0321356680
Thinking in Java (4rd edition)Bruce Eckel
Publication Date: February 20, 2006 ISBN-13: 978-0131872486
{ RECOMMENDED READINGS }
BCN } JUG
Design Patterns: Elements of Reusable Object-Oriented SoftwareErich Gamma, Richard Helm, Ralph Johnson, John Vlissides Publication Date: November 10, 1994ISBN-13: 978-0201633610
Refactoring: Improving the Design of Existing Code
Martin Fowler, Kent Beck, John Brant, William Opdyke, Don Roberts Publication Date: July 8, 1999
ISBN-13: 978-0201485677
public static void main (String[] args) {
Author bookAuthor = new Author (“Robert”,”C.Martin”);String bookTitle = “Clean Code, A handbook of Agile
Craftmanship”;Book cleanCodeBook = new Book(bookTitle, bookAuthor);
Engineer esteve = new Engineer(“@pensashure”);Engineer nacho = new Engineer(“@icougil”);
Review review = new Review(cleanCodeBook);review.perform( Arrays.asList(esteve, nacho) );
System.out.println(“THANKS FOR YOUR ATTENTION”);}
Thank you !
BCN } JUG@BarcelonaJU
G