SWEA 2018 Weekplan 4

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

  1. Clean Code template (excel)
  2. 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:

  1. 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!
  2. 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.
  3. 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.
  4. 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(), ....
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).

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...

  1. Session 1 / Intro
  2. Session 2 / Duplication I
  3. Session 3 / 'If' cleanup
  4. Session 4 / Duplication II
  5. Session 5 / Remove nesting
  6. Session 6 / One level of abstraction
  7. Session 7 / Commenting
  8. Session 8 / One level of abstraction
  9. Session 9 / Bail out fast
  10. Session 10 / One Level of Abstraction

Additional exercises:

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.

Exam Rehearsal

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!.