49
BE CLEAN, MY FRIEND A modest review of Robert C. Martin Clean Code, A handbook of Agile Craftmanshipbook BCN } JUG

Be clean, my friend (Clean code review by BarcelonaJUG)

  • Upload
    icougil

  • View
    485

  • Download
    1

Embed Size (px)

DESCRIPTION

BE CLEAN, MY FRIEND A modest review of Robert C. Martin “Clean Code, A handbook of Agile Craftmanship” book. 11/10/13 @GDGBarcelona #devfestbcn with @pensashure

Citation preview

Page 1: Be clean, my friend (Clean code review by BarcelonaJUG)

BE CLEAN, MY FRIENDA modest review ofRobert C. Martin

“Clean Code, A handbook of Agile Craftmanship” book

BCN } JUG

Page 2: Be clean, my friend (Clean code review by BarcelonaJUG)

{ THANK YOU ALL }

BCN } JUG

Page 3: Be clean, my friend (Clean code review by BarcelonaJUG)

{ DO WE NEED TO CODE BETTER? }

BCN } JUG{ Motivation }

Page 4: Be clean, my friend (Clean code review by BarcelonaJUG)

{ 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 }

Page 5: Be clean, my friend (Clean code review by BarcelonaJUG)

{ REALLY ?? ?? }

BCN } JUG

int d; // elapsed time in days

or

int elapsedTimeInDays;int daysSinceCreation;

int fileAgeInDays;

{ Motivation }

Page 6: Be clean, my friend (Clean code review by BarcelonaJUG)

{ WHAT IS CLEAN CODE ?? }

BCN } JUG

Set of techniques to help us achieve complexity from

simplicity.

Complex things =

composition of simple things.

{ Concepts }

Page 7: Be clean, my friend (Clean code review by BarcelonaJUG)

{ 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

Page 8: Be clean, my friend (Clean code review by BarcelonaJUG)

BCN } JUG

/* NAMING GUIDELINES */

{ BAPTISM }

Page 9: Be clean, my friend (Clean code review by BarcelonaJUG)

/* USE INTENTION-REVEALING NAMES */

BCN } JUG{ Baptism - Naming guidelines }

Page 10: Be clean, my friend (Clean code review by BarcelonaJUG)

/* USE INTENTION-REVEALING NAMES */

BCN } JUG{ Baptism - Naming guidelines }

METHOD CODE

Page 11: Be clean, my friend (Clean code review by BarcelonaJUG)

/* USE PRONOUNCEABLE NAMES */

BCN } JUG{ Baptism - Naming guidelines }

Page 12: Be clean, my friend (Clean code review by BarcelonaJUG)

/* 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 }

Page 13: Be clean, my friend (Clean code review by BarcelonaJUG)

/* CLASSES AND METHODS */

BCN } JUG{ Baptism - Naming guidelines }

Page 14: Be clean, my friend (Clean code review by BarcelonaJUG)

BCN } JUG

{ WHAT & HOW ARE YOU BUILDING }

/* CLASSES & METHODS */

Page 15: Be clean, my friend (Clean code review by BarcelonaJUG)

/* POLYMORPHISM OVER IF/ELSE */

BCN } JUG{ Classes & methods }

http://www.antiifcampaign.com/

Page 16: Be clean, my friend (Clean code review by BarcelonaJUG)

/* FUNCTIONS SHOULD DO ONE THING*/

BCN } JUG{ Classes & methods }

Page 17: Be clean, my friend (Clean code review by BarcelonaJUG)

/* OUTPUT ARGUMENTS */

BCN } JUG{ Classes & methods }

Page 18: Be clean, my friend (Clean code review by BarcelonaJUG)

BCN } JUG

{ UNEXPECTED THINGS }

/* ERROR HANDLING */

Page 19: Be clean, my friend (Clean code review by BarcelonaJUG)

/* PREFER EXCEPTIONS TO RETURN CODES */

BCN } JUG{ Error handling }

Page 20: Be clean, my friend (Clean code review by BarcelonaJUG)

/* PREFER EXCEPTIONS TO RETURN CODES */

BCN } JUG{ Error handling }

Page 21: Be clean, my friend (Clean code review by BarcelonaJUG)

/* DON’T RETURN NULL */

BCN } JUG{ Error handling }

Page 22: Be clean, my friend (Clean code review by BarcelonaJUG)

/* DON’T PASS NULL */

BCN } JUG{ Error handling }

Page 23: Be clean, my friend (Clean code review by BarcelonaJUG)

BCN } JUG

{ WHAT IS INSIDE? }

/* OBJECTS & DATA STRUCTURES */

Page 24: Be clean, my friend (Clean code review by BarcelonaJUG)

/* HIDE STRUCTURES */

BCN } JUG{ Objects & data structures }

Page 25: Be clean, my friend (Clean code review by BarcelonaJUG)

/* DATA ABSTRACTION */

BCN } JUG{ Objects & data structures }

Page 26: Be clean, my friend (Clean code review by BarcelonaJUG)

/* DATA TRANSFER OBJECTS */

BCN } JUG{ Objects & data structures }

Page 27: Be clean, my friend (Clean code review by BarcelonaJUG)

/* 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 }

Page 28: Be clean, my friend (Clean code review by BarcelonaJUG)

/* 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 }

Page 29: Be clean, my friend (Clean code review by BarcelonaJUG)

/* LAW OF DEMETER */

BCN } JUG{ Objects & data structures }

Page 30: Be clean, my friend (Clean code review by BarcelonaJUG)

BCN } JUG

{ ARE YOU TESTING ? }

/* UNIT TESTING ! */

Page 31: Be clean, my friend (Clean code review by BarcelonaJUG)

/* USE A COVERAGE TOOL */

BCN } JUG{ Unit testing }

Cobertura

Sonar

Page 32: Be clean, my friend (Clean code review by BarcelonaJUG)

/* 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 }

Page 33: Be clean, my friend (Clean code review by BarcelonaJUG)

/* KEEP TEST CLEAN */

BCN } JUG{ Unit testing }

Page 34: Be clean, my friend (Clean code review by BarcelonaJUG)

BCN } JUG

{ BEFORE DEVELOPMENT ... }

/* DESIGN */

Page 35: Be clean, my friend (Clean code review by BarcelonaJUG)

/* PRINCIPLES & ADVICES */

BCN } JUG

❏ Don’t repeat yourself (DRY): duplication is evil!

{ Design }

Page 36: Be clean, my friend (Clean code review by BarcelonaJUG)

/* 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 }

Page 37: Be clean, my friend (Clean code review by BarcelonaJUG)

/* FACTORIES */

BCN } JUG{ Design }

Page 38: Be clean, my friend (Clean code review by BarcelonaJUG)

/* 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)

Page 39: Be clean, my friend (Clean code review by BarcelonaJUG)

BCN } JUG

{ TELL ME A (BEAUTIFUL) STORY }

/* COMMENTS */

Page 40: Be clean, my friend (Clean code review by BarcelonaJUG)

/* EXPLAIN YOURSELF IN CODE */

BCN } JUG{ Comments }

Page 41: Be clean, my friend (Clean code review by BarcelonaJUG)

/* GOOD COMMENTS */

BCN } JUG{ Comments }

❏ Legal comments

❏ Informative comments

❏ Explanation of Intent

❏ Warning of Consequences

❏ TODOs

❏ JavaDocs

Page 42: Be clean, my friend (Clean code review by BarcelonaJUG)

/* 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;}

Page 43: Be clean, my friend (Clean code review by BarcelonaJUG)

BCN } JUG

{ LAST, BUT NOT LEAST }

Page 44: Be clean, my friend (Clean code review by BarcelonaJUG)

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 }

Page 45: Be clean, my friend (Clean code review by BarcelonaJUG)

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 }

Page 46: Be clean, my friend (Clean code review by BarcelonaJUG)

{ 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/

Page 47: Be clean, my friend (Clean code review by BarcelonaJUG)

{ 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

Page 48: Be clean, my friend (Clean code review by BarcelonaJUG)

{ 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

Page 49: Be clean, my friend (Clean code review by BarcelonaJUG)

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