SLIDE 1 Twelve Ways to Make Code Suck Less
Venkat Subramaniam
venkats@agiledeveloper.com
@venkat_s
SLIDE 2
Why should we care about code quality?
SLIDE 3 We can’t be agile if
SLIDE 4
SLIDE 5 “Lowering quality lengthens development time.”—First Law of Programming.
http://c2.com/cgi/wiki?FirstLawOfProgramming
SLIDE 6
What’s Quality Code?
SLIDE 7 The quality of code is inversely proportional to the effort it takes to understand it.
http://blog.agiledeveloper.com/2010/05/thoughts-through-tweets_15.html
SLIDE 8
Twelve ways We can Help
SLIDE 9
- 12. Schedule Time to Lower
Technical Debt
SLIDE 10 What’s Technical Debt?
http://c2.com/cgi/wiki?WardExplainsDebtMetaphor
Ward Cunningham
SLIDE 11
Example of Technical Debts
SLIDE 12
Low quality is not technical debt
SLIDE 13
What Causes Technical Debt?
SLIDE 14
Not “All or Nothing”
SLIDE 15
Schedule Time
SLIDE 16 Development Vacation/sickness Meetings Paying Technical Debt Slack time
…
Planning…
SLIDE 18
SLIDE 19 Development Vacation/sickness Meetings Paying Technical Debt Slack time
…
Planning…
SLIDE 21
Narrow, focused, does only one thing well
SLIDE 22
Why?
SLIDE 23
Think about frequency of change
SLIDE 24
low cyclomatic complexity high cohesion ==
SLIDE 26
Tight coupling make code hard to extend hard to test
SLIDE 27
Lower coupling Loose vs. tight coupling Eliminate where possible
SLIDE 28 Excessive Mocking to Test This Non deterministic Dependency
SLIDE 29 Light Mocking to Test This Non deterministic Dependency No Mocking to Test This
SLIDE 30
- 9. Program with Intention
SLIDE 31 http://stackoverflow.com/questions/184618/what-is-the-best- comment-in-source-code-you-have-ever-encountered
SLIDE 32 Beck’s Rule for Simple Design
http://martinfowler.com/bliki/BeckDesignRules.html
SLIDE 33 Programming Deliberately
- When you write test before writing code…
SLIDE 34
- 8. Avoid Primitive Obsession
SLIDE 35
SLIDE 36
SLIDE 37
It’s code like this that prematurely turns programmers into managers
SLIDE 38
SLIDE 39
SLIDE 40 http://blog.agiledeveloper.com/2015/08/the-functional-style.html
SLIDE 41
- 7. Prefer Clear Code
- ver Clever Code
SLIDE 42
SLIDE 43 10% of the time, we write ugly code for performance reasons, the other 90% of the time, we write ugly code to be consistent.
http://blog.agiledeveloper.com/2010/05/thoughts-through-tweets_15.html
SLIDE 44 http://blog.agiledeveloper.com/2010/05/thoughts-through-tweets_15.html
Those who sacrifice quality to get performance may end up getting neither.
SLIDE 45 “There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies and the other is to make it so complicated that there are no
- bvious deficiencies”— Tony Hoare
“There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies and the other is to make it so complicated that there are no
- bvious deficiencies”— Tony Hoare
SLIDE 46
- 6. Apply Zinsser's Principle on
Writing
SLIDE 47
SLIDE 48
Simplicity Clarity Brevity Humanity
SLIDE 50
Don’t comment to cover up bad code
SLIDE 51
Write Expressive Self-Documenting Code
SLIDE 52 A good code is like a good joke
http://blog.agiledeveloper.com/2006/01/comments-on- comments.html
Writing comments is like explaining a joke
SLIDE 53
SLIDE 54
SLIDE 55
- rder(3) 3 cups?…
- rder(CoffeeSize.LARGE)
- rder(3) // large coffee
SLIDE 56 https://media.pragprog.com/titles/pad/PAD-pulloutcard.pdf
SLIDE 57
- 4. Avoid Long Methods—Apply
SLAP
SLIDE 58
Perils of Long Methods
SLIDE 59
How long is long?
SLIDE 60 Turns out long is not about length
- f code, but levels of abstraction
SLIDE 61
- 3. Give Good Meaningful Names
SLIDE 62
SLIDE 63
SLIDE 64
SLIDE 65
Variable Names Represent Abstractions
SLIDE 66
SLIDE 67
- 2. Do Tactical Code Reviews
SLIDE 68 Peer reviews catch 60% of defects. Perspective-based reviews catch 35% more defects than nondirected reviews. Peer reviews complement testing. Technical and social activity.
https://www.cs.umd.edu/projects/SoftEng/ESEG/papers/82.78.pdf
SLIDE 69
Facilitating Tactical Code Reviews
SLIDE 70
- 1. Reduce State & State Mutation
SLIDE 71
SLIDE 72
SLIDE 73
SLIDE 74
Twelve Ways to Make Code Suck Less
SLIDE 75
- 12. Schedule Time to Lower Technical Debt
- 11. Favor High Cohesion
- 10. Favor Loose Coupling
- 9. Program with Intention
- 8. Avoid Primitive Obsession
- 7. Prefer Clear Code over Clever Code
- 6. Apply Zinsser's Principle on Writing
- 5. Comment Why, not What
- 4. Avoid Long Methods—Apply SLAP
- 3. Give Good Meaningful Names
- 2. Do Tactical Code Reviews
- 1. Reduce State & State Mutation
SLIDE 76
SLIDE 77
http://agiledeveloper.com/downloads.html
SLIDE 78
Thank you
http://agiledeveloper.com/downloads.html venkats@agiledeveloper.com @venkat_s