Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Clean code

4,012 views

Published on

In Swedish.
Mitt försök att provocera fram lite diskussion på Javaforum 2011-Q2.

Published in: Technology

Clean code

  1. 2. Clean Code
  2. 3. This work by Fredrik Wendt is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License http://creativecommons.org/licenses/by-nc-sa/3.0/
  3. 6. Coding Dojo / User Group JDojo@Gbg
  4. 7. How is it even possible for code to be this bad?
  5. 8. Exhibits <ul><li>Hudson class is 3900+ LOC
  6. 9. Hudson class is untestable Singleton
  7. 10. Konstruktorn anropar en metod som hämtar singleton-instansen och anropar en metod på den.
  8. 11. Hudson extends Node, men Node använder instansmetoder på Hudson </li></ul>
  9. 12. ”Smart” vs Professional <ul><li>Smart </li><ul><li>Great coding skills
  10. 13. Writes advanced code
  11. 14. String r; // lowercase url </li></ul></ul><ul><li>Professional </li><ul><li>Readable code
  12. 15. Maintainable code
  13. 16. 'Clarity is king'
  14. 17. String lowerCaseUrl OfCurrentPage // lowercase url </li></ul></ul>http://www.slideshare.net/JandV/clean-code-summary
  15. 18. Clean Code Book Comparison http://blog.xebia.com/2009/01/clean-code-vs-implementation-patterns/
  16. 19. Vad är Clean Code?
  17. 20. int d; // elapsed time in days
  18. 21. int d; // elapsed time in days int elapsedTimeInDays;
  19. 22. int d; // elapsed time in days int elapsedTimeInDays; int daysSinceCreation;
  20. 23. int d; // elapsed time in days int elapsedTimeInDays; int daysSinceCreation; int fileAgeInDays;
  21. 24. public List<int[]> getThem() { List<int[]> list1 = new ArrayList<int[]>(); for (int[] x : theList) if (x[0] == 4) list1.add(x); return list1; }
  22. 25. public List<int[]> getThem() { List<int[]> list1 = new ArrayList<int[]>(); for (int[] cell : gameBoard ) if ( cell [ STATUS_VALUE ] == FLAGGED ) list1.add( cell ); return list1; }
  23. 26. public List<int[]> getFlaggedCells () { List<int[]> flaggedCells = new ArrayList<int[]>(); for (int[] cell : gameBoard) if (cell[STATUS_VALUE] == FLAGGED) flaggedCells .add(cell); return flaggedCells ; }
  24. 27. public List< Cell > getFlaggedCells() { List< Cell > flaggedCells = new ArrayList< Cell >(); for ( Cell cell : gameBoard) if ( cell.isFlagged() ) flaggedCells.add( cell ); return flaggedCells; }
  25. 28. Clean code always looks like it was written by someone who cares
  26. 29. Clean code always looks like it was wrtten by somone who craes
  27. 30. Readable Maintainable Changeable
  28. 31. Agile Manifesto Individuals and interactions over processes and tools Working software over comprehensive documentation Customer collaboration over contract negotiation Responding to change over following a plan
  29. 33. Read able?
  30. 34. ~90 % av din tid är läsning Vad händer om du dubblar skrivtiden , och därmed halverar lästiden ?
  31. 35. 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; } }
  32. 36. Varför bry dig? Vem skrivs koden för – användaren eller programmeraren? Den primära användaren av koden är en utvecklare, kanske <ul><ul><li>Din kund
  33. 37. Din kollega
  34. 38. Du själv </li></ul></ul>someone who cares
  35. 39. Hur ska du skriva kod då?
  36. 40. DRY - Don't Repeat Yourself YAGNI - You Ain't Gonna Need It
  37. 41. small
  38. 42. Meaningful Names <ul><li>Intention-Revealing
  39. 43. Pronounceable
  40. 44. Avoid Encoding </li></ul><ul><li>Klasser
  41. 45. Metoder </li></ul><ul><li>JobQueue
  42. 46. Problemdomänens ord
  43. 47. Kommentarer
  44. 48. Kodformatering! </li></ul>
  45. 49. Don't talk to strangers http://www.ccs.neu.edu/research/demeter/demeter-method/LawOfDemeter/paper-boy/
  46. 50. public class PaperBoy { private Wallet myWallet; ... public void chargeCustomer(Customer customer) { Collection<Bill> payment = customer.getPayment(5) myWallet.add(payment); } } Law of Demeter
  47. 51. public 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? } } Law of Demeter
  48. 52. public class CareTaker { private Dog dog; ... public void walkTheDog(){ dog.walk(); dog.stop(); // tree or grass etc dog.doYourThing(); ... } } Dog's Legs
  49. 53. public 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(); ... } } Dog's Legs
  50. 54. 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 itself 2. M's parameters 3. any objects created/instantiated within M 4. O's direct component objects 5. a variable, accessible by O, in the scope of M
  51. 55. Boy Scout Rule Leave the campground cleaner than you found it.
  52. 56. The only valid measurement of code quality WTFs / minute
  53. 57. Diskussion Kodkvalitet och retrospectives Smart vs Professional Skriva för mottagaren swenug vs JavaForum swejug?
  54. 58. Kod utan test Legacy Code
  55. 59. Vad gör jag med kod som redan ligger i ryggsäcken? <ul><li>Få upp gubben på banan – få koden testbar (och testad)
  56. 60. Fixa ev buggar som testen plockar fram i ljuset
  57. 61. ”Make it right” </li></ul>
  58. 62. Vad gör jag med kod som redan ligger i ryggsäcken? <ul><li>Få upp gubben på banan – få koden testbar (och testad)
  59. 63. Fixa ev buggar som testen plockar fram i ljuset
  60. 64. ”Make it right” – refactor! </li></ul>
  61. 65. Kod utan test ... <ul><li>är legacy-kod
  62. 66. refaktoreras inte
  63. 67. ändras gärna inte av någon annan än den som skrev koden senaste dygnen
  64. 68. är en ryggsäck </li></ul>
  65. 70. Outline <ul><li>Vad är Clean Code?
  66. 71. Varför är det intressant? </li><ul><li>Problemet
  67. 72. Lösningen </li></ul><li>Diskussion </li></ul>
  68. 73. Outline2 <ul><li>”Problemet”?
  69. 74. ”Lösningen”?
  70. 75. Diskussion </li></ul>

×