Be clean, my friend (Clean code review by BarcelonaJUG)
Upcoming SlideShare
Loading in...5
×
 

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

on

  • 340 views

BE CLEAN, MY FRIEND

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

Statistics

Views

Total Views
340
Views on SlideShare
320
Embed Views
20

Actions

Likes
1
Downloads
6
Comments
0

3 Embeds 20

https://twitter.com 16
http://www.linkedin.com 3
https://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

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

  • BCN } JUG BE CLEAN, MY FRIEND A modest review of Robert C. Martin “Clean Code, A handbook of Agile Craftmanship” book
  • { THANK YOU ALL } BCN } JUG
  • { DO WE NEED TO CODE BETTER? } { Motivation } BCN } JUG
  • { MAIN QUESTION: WHY? } 1. You want to be a good programmer 1. You want to be better programmer 1. You like being part of best teams. { Motivation } BCN } JUG
  • { REALLY ?? ?? } int d; // elapsed time in days or int elapsedTimeInDays; int daysSinceCreation; int fileAgeInDays; { Motivation } BCN } JUG
  • { WHAT IS CLEAN CODE ?? } Complex things = composition of simple things. Set of techniques to help us achieve complexity from simplicity. { Concepts } BCN } JUG
  • { TABLE OF CONTENTS } ❏ 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
  • { BAPTISM } /* NAMING GUIDELINES */ BCN } JUG
  • /* USE INTENTION-REVEALING NAMES */ { Baptism - Naming guidelines } BCN } JUG
  • /* USE INTENTION-REVEALING NAMES */ METHOD CODE { Baptism - Naming guidelines } BCN } JUG
  • /* USE PRONOUNCEABLE NAMES */ { Baptism - Naming guidelines } BCN } JUG
  • /* CLASSES AND METHODS */ ❏ 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 } BCN } JUG
  • /* CLASSES AND METHODS */ { Baptism - Naming guidelines } BCN } JUG
  • { WHAT & HOW ARE YOU BUILDING } /* CLASSES & METHODS */ BCN } JUG
  • /* POLYMORPHISM OVER IF/ELSE */ http://www.antiifcampaign.com/ { Classes & methods } BCN } JUG
  • /* FUNCTIONS SHOULD DO ONE THING*/ { Classes & methods } BCN } JUG
  • /* OUTPUT ARGUMENTS */ { Classes & methods } BCN } JUG
  • { UNEXPECTED THINGS } /* ERROR HANDLING */ BCN } JUG
  • /* PREFER EXCEPTIONS TO RETURN CODES */ { Error handling } BCN } JUG
  • /* PREFER EXCEPTIONS TO RETURN CODES */ { Error handling } BCN } JUG
  • /* DON’T RETURN NULL */ { Error handling } BCN } JUG
  • /* DON’T PASS NULL */ { Error handling } BCN } JUG
  • { WHAT IS INSIDE? } /* OBJECTS & DATA STRUCTURES */ BCN } JUG
  • /* HIDE STRUCTURES */ { Objects & data structures } BCN } JUG
  • /* DATA ABSTRACTION */ { Objects & data structures } BCN } JUG
  • /* DATA TRANSFER OBJECTS */ { Objects & data structures } BCN } JUG
  • /* LAW OF DEMETER */ ❏ 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 } BCN } JUG
  • /* LAW OF DEMETER */ ❏ 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 } BCN } JUG
  • /* LAW OF DEMETER */ { Objects & data structures } BCN } JUG
  • { ARE YOU TESTING ? } /* UNIT TESTING ! */ BCN } JUG
  • /* USE A COVERAGE TOOL */ Cobertura Sonar { Unit testing } BCN } JUG
  • /* TAKE CARE OF TESTS */ ❏ 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 } BCN } JUG
  • /* KEEP TEST CLEAN */ { Unit testing } BCN } JUG
  • { BEFORE DEVELOPMENT ... } /* DESIGN */ BCN } JUG
  • /* PRINCIPLES & ADVICES */ ❏ Don’t repeat yourself (DRY): duplication is evil! { Design } BCN } JUG
  • /* PRINCIPLES & ADVICES */ ❏ 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 } BCN } JUG
  • /* FACTORIES */ { Design } BCN } JUG
  • /* DEPENDENCY INJECTION */ With Dependency Injection (Low dependency with the Interface and Implementation, because the Assembler does the job) Without Dependency Injection (Hard dependency with the Interface and Implementation) { Design } BCN } JUG
  • { TELL ME A (BEAUTIFUL) STORY } /* COMMENTS */ BCN } JUG
  • /* EXPLAIN YOURSELF IN CODE */ { Comments } BCN } JUG
  • /* GOOD COMMENTS */ ❏ Legal comments ❏ Informative comments ❏ Explanation of Intent ❏ Warning of Consequences ❏ TODOs ❏ JavaDocs { Comments } BCN } JUG
  • /* BAD COMMENTS */ /** * @param title The title * @param author The author */ /** * Returns the day of the month * * @return the day of the month */ public int getDayOfMonth() { return dayOfMonth; } /** * CHANGES * ------------* 02-Oct-2001 Reorganize process * 15-Nov-2001 Added new Purchaser type * 21-Nov-2001 Some customer objects arrive without city set */ { Comments } BCN } JUG
  • { LAST, BUT NOT LEAST } BCN } JUG
  • /* THINK ABOUT IT */ ❏ This is not a mantra ❏ (Try to) know your environment! ❏ Code is always live ❏ Refactor & test, test & refactor { 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 } BCN } JUG
  • { DO YOU LIKE IT ?? READ IT ... } Clean Code, A Handbook of Agile Software Craftmanship Robert C. Martin Publication date: August 11, 2008 ISBN-13: 978-0132350884 { ... OR ... WATCH IT !! } http://cleancoders.com/ BCN } JUG
  • { RECOMMENDED READINGS } Effective Java (2nd edition) Joshua Bloch Publication Date: May 28, 2008 ISBN-13: 978-0321356680 Thinking in Java (4rd edition) Bruce Eckel Publication Date: February 20, 2006 ISBN-13: 978-0131872486 BCN } JUG
  • { RECOMMENDED READINGS } Design Patterns: Elements of Reusable Object-Oriented Software Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides Publication Date: November 10, 1994 ISBN-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 BCN } JUG
  • Thank you ! 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”); } @BarcelonaJUG BCN } JUG