Improving code quality@jsuchal
Issues
1. Doing the wrong thing
2. Excessive coupling
3. Mutable state
4. Premature abstraction
5. Edge conditions
Tips&
Tricks
Outside-in development
● Start at the very top○ discover dependencies○ avoids unit interface incompatibility problem○ how would I test this?
● Programming by wishful thinking○ design usage, review/discuss, implement later,
refactor a lot later○ do / refactor mode
TDD
● Acceptance (end-to-end) tests○ start here!○ nonexhaustive (avoid exponential blowup)
● Unit tests○ a design tool○ a testing tool
Immutable OO
● Best trick for maintainable code, by far!● All fields immutable● Mental trick
○ obj.method(*args) == method(Class obj, *args)
● Code smells are obvious● Temporal coupling is impossible● OOP finally makes sense
Smell-Driven Development● private / protected
○ missing collaborator, extract & inject● not using all fields
○ class doing too much, split● prefixes
○ missing abstraction, extract & inject● returning self or void
○ mutable state, avoid!● duplicated logic in parent method
○ inject listener/responder
Smell-Driven Development (2)● Law of Demeter (one dot rule)
○ getters are ok!● Avoid global constants
○ a.k.a. implicit dependencies○ yes, class name is a constant
● Data Clump, Feature Envy● Test smells
Premature abstraction
● “Third time is the charm.”
● KISS
● YAGNI
Tools
● rubocop○ the code style grammar nazi
● reek○ code smell detection
● mutant○ mutation testing
● mbj/devtools○ the CI task!
Top Related