The learning goals for Week 38 are:
Mon: Code Quality. Wed: Maintainability and Flexibility.
Notes for this weekplan:
Links to the Clean Code Classification Template
- Clean Code template (excel)
- Clean Code template (pdf)
I have not produced literature regarding my own clean code properties, so I forward here the first "definitions" of them for use in the mandatory project:
Note that these properties themselves follow 'do the same thing the same way' - they are all desirable properties (your method is cleaner if they all evaluate TRUE).
- Do the Same Thing, the Same Way: If you have code fragments that do similar things, then express them in similar ways.
Examples of 'not OK': if you half of the time get tiles using the 'getTileAt()' method and the other half of the time uses direct datastructure look up ala 'tileMap.get()'. If you have two methods taking 'from' and 'to' parameters, and one is coded
foo(from, to)and the other
bar(to, from)you pave the road to bugs!
- Name Boolean Expressions: If you have a boolean expression that use AND, OR, NOT, between several subexpressions, then create each subexpression as a separate boolean value having a good name. Then make the complex expression using these named boolean expressions.
See slides for examples.
- Bail out Fast: If condition(s) leads to a method being able to compute the answer right away, then code the 'return' statement as soon as possible.
Examples of 'OK': in moveUnit() let the test for if the 'to' tile is a mountiain bail out fast:
if (getTileAt(to).equals(OCEAN)) return false;. Bail out fast makes the code flow easier to follow because it avoids deep if nesting levels. See the screencasts below for more examples.
- Arguments in Argument Lists: Arguments to a method, should appear as real arguments and not as part of the method name itself.
Examples of 'not OK':
moveRedUnit(...), add17ToX(), createBlueCityAt(), createLegionUnit(), ....
As an alternative/supplement to my lecture on clean code, I have made ten screen casts in which I refactor the 'move(from,to)' method which I have coded heavily inspired by student's code from E16 (thus it is artificial, but all unclean aspects have real counterparts). You may study the GameImpl.java yourself before watching the screencasts.
There are a few places in the videos with rather long pauses! The reason? I think...
- Session 1 / Intro
- Session 2 / Duplication I
- Session 3 / 'If' cleanup
- Session 4 / Duplication II
- Session 5 / Remove nesting
- Session 6 / One level of abstraction
- Session 7 / Commenting
- Session 8 / One level of abstraction
- Session 9 / Bail out fast
- Session 10 / One Level of Abstraction
Find Dirty Code
Find some code from previous courses or projects, and analyze them according to the Uncle Bob+Henrik principles. If in the mood - clean them up.
Rehearse a clean code exam situation. Spend 25 minutes to prepare a 10 minute oral presentation of an exercise in the example exam question set: demo-question-final-2018.pdf.
Next, do a 10 minute presentation at the whiteboard in front of your team.
The team provides constructive feedback on the presentation: Is your presentation clear and understandable, do you discuss the concepts and terms correctly, is you Java example code correct, etc. Swap and ensure all in the team gets a chance to rehearse the situation.
Legend: The typography bold, normal, (brackets), above indicate my perception of how important the exercises are from high to optional. However, solve the mandatory project first!.