Clean Code


Published on

This is material that's been used to discuss or introduce clean code JDojo@Gbg (among other places).

Published in: Education, Technology
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Detta gjorde vi sist.
  • Alla borde känna igen cirkeln
    helst 1 minut från red till green
    fokus på refactor-steget [för det låter oss...]
  • Primary user of our code is coders - it's you!
    x % of the time we're reading code, not writing.
  • [men boken handlar om ...]
  • Qode quality och clean code,
    [vad menar boken med detta]
  • Vi bryr oss om olika saker
    Fokus idag = gemensamma saker
    [Konkret exempel först]
  • when one wants to walk a dog, it would be folly to command the dog’s legs to walk directly; instead one commands the dog and lets it take care of its legs itself.
  • Strategi kring exceptions
    checked vs unchecked
    log vs rethrow
    hur mycket kod är ok inom try - catch? (en rad, ett uttryck, jfr med Jms-koden:openConnection()openSession()openProducer()openConsumer()connection.start() ...
  • Clean Code

    1. 1. JDojo & Clean Code
    2. 2. Baby Steps ● as small as possible ● as large as is comfortable
    3. 3. Red, Green, Refactor ● ... ● ... ● Refactor, "clean code", run tests frequently - the "bar" should stay green
    4. 4. User Focus = Clean Code
    5. 5. You are reading this book for two reasons. First, you are a programmer. Second, you want to be a better programmer. Good. We need better programmers.
    6. 6. The only valid measurement of code quality: WTFs/minute
    7. 7. Clean code always looks like it was written by someone who cares
    8. 8. Readable Maintainable Changeable Clean Code Values
    9. 9. 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
    10. 10. public List<int[]> getThem() { List<int[]> list1 = new ArrayList<int[]>(); for (int[] x : theList) if (x[0] == 4) list1.add(x); return list1; }
    11. 11. public List<int[]> getFlaggedCells() { List<int[]> flaggedCells = new ArrayList<int[]>(); for (int[] cell : gameBoard) if (cell[STATUS_VALUE] == FLAGGED) flaggedCells.add(cell); return flaggedCells; }
    12. 12. public List<Cell> getFlaggedCells() { List<Cell> flaggedCells = new ArrayList<Cell>(); for (Cell cell : gameBoard) if (cell.isFlagged()) flaggedCells.add(cell); return flaggedCells; }
    13. 13. int d; // elapsed time in days
    14. 14. int d; // elapsed time in days int elapsedTimeInDays; int daysSinceCreation; int fileAgeInDays;
    15. 15. int d; // elapsed time in days int elapsedTimeInDays; int daysSinceCreation; int fileAgeInDays; Intention-Revealing Names
    16. 16. assertTrue(aList.contains(aMember)); assertTrue(aList.contains(anotherMember)); assertTrue(aList.contains(aThirdMember)); DRY - Don't Repeat Yourself
    17. 17. assertTrue(aList.contains(aMember)); assertTrue(aList.contains(anotherMember)); assertTrue(aList.contains(aThirdMember)); verifyListMembers(aList, aMember, anotherMember, aThirdMember); void verifyListMembers(List list, Object... members) { for (Object member : members) { assertTrue(list.contains(member)); } } DRY - Don't Repeat Yourself
    18. 18. Single Responsibility Principle a function should do one thing only a class should only contain one abstraction
    19. 19. Open Closed Principle Finish what you start the routine or object that allocates a resource should be responsible for deallocating it
    20. 20. 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
    21. 21. Law of Demeter use only one dot! a.b.method() a.method()
    22. 22. small
    23. 23. try catch finally public void doSomeCruftyWork(work) { try { actuallyPerformTheDirtyWork(work); } catch (IOException e) { logger.log(e); scheduler.reschedule(work); } }
    24. 24. Edsger Dijkstra Every function, and every block within a function, should have one entry and one exit. return break continue throw (finally)
    25. 25. The Boy Scout Rule Leave the campground cleaner than you found it.
    26. 26. Test Your Software ... or Your Users Will
    27. 27. This work by Fredrik Wendt is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License