Clean Code: Messages From The Book¶
Names¶
Clear and concise. Nobody should have to think longer than necessary what the named thing is. No encoding, no nothing. Gosh, hungarian notation.
Is
UserDB
a good name? It keeps data in-memory, and has methodsread()
andwrite()
bolted onto it⟶ unclear semantics
Class member names: no mangling, like prefixing with
m_
. Classes must be short enough to not need this.
Functions¶
Not everything must take dogmatically though
⟶ Make a case for
switch
Formatting¶
Newspaper Metaphor
Objects and Data Structures¶
Error Handling¶
Exception handling, clearly
But what if no language support?
Show
/home/jfasch/work/linux/kernel/fork.c
,copy_process()
Boundaries¶
Unit Tests¶
Three laws of TDD …
You may not write production code until you have written a failing unit test
You may not write more of a unit test than is sufficient to fail (and not compiling is a failure)
You may not write more production code than is necessary to pass current tests
Refactoring …
Initial code is a mess (nobody writes clean code from the beginning)
Takes roughly the same time than it took to write code initially
⟶ nobody likes it
Anyway, it is the only way to keep code clean
⟶ integral part of our craftsmanship
Another look …
If you are not able to test in isolation, then something is wrong
If you must fire up an Oracle database to conduct testing, then something’s seriously wrong
It’s only tests?
Seriously, no!
Tests are the safety net of your architecture ⟶ keep production code together
⟶ treat them carefully!
Production code follows the tests, actually!
F.I.R.S.T.
Fast
Independent
Repeatable
Self-validating (either pass or fail)
Timely (test first)
Note
Couple words about debugging!!!!
Writing tests after the fact -> come across code that is hard to test
Classes¶
Gosh, OO. Couple principles maybe.
Not loose overview ⟶ classes should be small
One responsibility
… (it’s always the same)
Comments¶
Wear T-shirt
Comments do not make up for bad code
Incorrect comments?