Clean Code 2

2,802 views

Published on

This is a 45 minute talk I've given a couple of times.
Heavily based on the book Clean Code.

Published in: Technology

Clean Code 2

  1. 1. Fredrik Wendt
  2. 2. Clean Code what, why, how
  3. 3. http://blog.xebia.com/2009/01/clean-code-vs-implementation-patterns/
  4. 4. Disclaimer Correlation betweenClean code and success?
  5. 5. How is it even possiblefor code to be this bad?
  6. 6. Exhibits • Hudson class is 3900+ LOC • Hudson class is untestable Singleton • Constructor calls a method that gets the singleton instance and calls a method on the instance. • Hudson extends Node, but Node calls methods defined in Hudson
  7. 7. WTF!So why care – Jenkins is great?
  8. 8. ”Smart” vs Professional• Smart • Professional ● Great coding skills ● Readable code ● Writes advanced code ● Maintainable code ● String r; // lowercase url ● Clarity is king ● String lowerCaseUrl OfCurrentPage; http://www.slideshare.net/JandV/clean-code-summary
  9. 9. What is Clean Code?
  10. 10. int d; // elapsed time in days
  11. 11. int d; // elapsed time in daysint elapsedTimeInDays;
  12. 12. int d; // elapsed time in daysint elapsedTimeInDays;int daysSinceCreation;
  13. 13. int d; // elapsed time in daysint elapsedTimeInDays;int daysSinceCreation;int fileAgeInDays;
  14. 14. public List<int[]> getThem() { List<int[]> list1 = new ArrayList<int[]>(); for (int[] x : theList) if (x[0] == 4) list1.add(x); return list1;}
  15. 15. public List<int[]> getThem() { List<int[]> list1 = new ArrayList<int[]>(); for (int[] cell : gameBoard) if (cell[STATUS_VALUE] == FLAGGED) list1.add(cell); return list1;}
  16. 16. public List<int[]> getFlaggedCells() { List<int[]> flaggedCells = new ArrayList<int[]>(); for (int[] cell : gameBoard) if (cell[STATUS_VALUE] == FLAGGED) flaggedCells.add(cell); return flaggedCells;}
  17. 17. public List<Cell> getFlaggedCells() { List<Cell> flaggedCells = new ArrayList<Cell>(); for (Cell cell : gameBoard) if (cell.isFlagged()) flaggedCells.add(cell); return flaggedCells;}
  18. 18. Clean code alwayslooks like it was written by someone who cares
  19. 19. Clean code alwayslooks like it was wrtten by somone who craes
  20. 20. ReadableMaintainableChangeable
  21. 21. Agile ManifestoIndividuals and interactions over processes and toolsWorking software over comprehensive documentationCustomer collaboration over contract negotiationResponding to change over following a plan
  22. 22. Readable?
  23. 23. ~ 90% of your time is reading What if youd ddouble write-time and thereby cut read-time by half?
  24. 24. public class Part { private String m_dsc; // The textual description void setName(String name) { m_dsc = name; }}public class Part { private String description; void setDescription(String description) { this.description = description; }}
  25. 25. Still, why bother?
  26. 26. Code is written for humans!
  27. 27. Why?Whos the main recipient of the code we write – a computer, anend user or a programmer?The primary user of sourcecode is a programmer, perhaps ● your Customer ● your Colleague ● You
  28. 28. How should I write code?
  29. 29. DRY - Dont Repeat YourselfYAGNI - You Aint Gonna Need It
  30. 30. small
  31. 31. Meaningful Names• Intention-Revealing • JobQueue• Pronounceable • Words from the domain• Avoid Encoding • Comments• Classes • Formatting!• Methods
  32. 32. Law of Demeter More formally, the Law of Demeter for functions requires that a method M of an object O may only invoke the methods of the following kinds of objects:1. O itself2. Ms parameters3. any objects created/instantiated within M4. Os direct component objects5. a variable, accessible by O, in the scope of M
  33. 33. Dogs Legspublic class CareTaker { private Dog dog; ... public void walkTheDog(){ dog.walk(); dog.stop(); // tree or grass etc dog.doYourThing(); ... }}
  34. 34. Dogs Legspublic class CareTaker { private Dog dog; ... public void walkTheDog(){ for (Leg leg : dog.getLegs()) leg.move(); for (Leg leg : dog.getLegs()) leg.stopMoving(); dog.brain.urinationCenter.releaseUrge(); ... }}
  35. 35. Dont talk to strangershttp://www.ccs.neu.edu/research/demeter/demeter-method/LawOfDemeter/paper-boy/
  36. 36. Law of Demeterpublic class PaperBoy { private Wallet myWallet; ... public void chargeCustomer(Customer customer) { Wallet wallet = customer.getWallet(); Collection<Bill> bills = wallet.getBills(); Collection<Bill> payment = extract(5, bills); myWallet.add(payment); // or bills? }}
  37. 37. Law of Demeterpublic class PaperBoy { private Wallet myWallet; ... public void chargeCustomer(Customer customer) { Collection<Bill> payment = customer.getPayment(5); myWallet.add(payment); }}
  38. 38. Single Responsibility 1 small
  39. 39. Boy Scout RuleLeave the campground cleaner than you found it.
  40. 40. Hur mäter man Clean Code? Eller kodkvalitet?
  41. 41. Can you measure Clean Code? Or code quality?
  42. 42. WTFs / minuteThe only validmeasurement of code quality
  43. 43. Summary Source code vs machine code Write with the audience in minde Smart vs Professional Code quality and retrospectivesCode Review (wtf!) vs pair programming
  44. 44. Legacy CodeCode with no tests
  45. 45. What about the code thatsalready in my backpack?• Get the old man on track – make the code testable (and tested)• Fix bugs brought out in daylight by the tests• ”Make it right”
  46. 46. What about the code thatsalready in my backpack?• Get the old man on track – make the code testable (and tested)• Fix bugs brought out in daylight by the tests• ”Make it right” – refactor!
  47. 47. Code without tests ...• is legacy code• is not refactored• unlikely to be changed by someone other than the person who wrote it (the past few days)• is heavy load/back pack

×