improving code quality
TRANSCRIPT
![Page 1: Improving code quality](https://reader030.vdocuments.mx/reader030/viewer/2022032420/55a524ab1a28abe50e8b45b6/html5/thumbnails/1.jpg)
Improving code quality@jsuchal
![Page 2: Improving code quality](https://reader030.vdocuments.mx/reader030/viewer/2022032420/55a524ab1a28abe50e8b45b6/html5/thumbnails/2.jpg)
Issues
1. Doing the wrong thing
2. Excessive coupling
3. Mutable state
4. Premature abstraction
5. Edge conditions
![Page 3: Improving code quality](https://reader030.vdocuments.mx/reader030/viewer/2022032420/55a524ab1a28abe50e8b45b6/html5/thumbnails/3.jpg)
Tips&
Tricks
![Page 4: Improving code quality](https://reader030.vdocuments.mx/reader030/viewer/2022032420/55a524ab1a28abe50e8b45b6/html5/thumbnails/4.jpg)
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
![Page 5: Improving code quality](https://reader030.vdocuments.mx/reader030/viewer/2022032420/55a524ab1a28abe50e8b45b6/html5/thumbnails/5.jpg)
TDD
● Acceptance (end-to-end) tests○ start here!○ nonexhaustive (avoid exponential blowup)
● Unit tests○ a design tool○ a testing tool
![Page 6: Improving code quality](https://reader030.vdocuments.mx/reader030/viewer/2022032420/55a524ab1a28abe50e8b45b6/html5/thumbnails/6.jpg)
![Page 7: Improving code quality](https://reader030.vdocuments.mx/reader030/viewer/2022032420/55a524ab1a28abe50e8b45b6/html5/thumbnails/7.jpg)
![Page 8: Improving code quality](https://reader030.vdocuments.mx/reader030/viewer/2022032420/55a524ab1a28abe50e8b45b6/html5/thumbnails/8.jpg)
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
![Page 9: Improving code quality](https://reader030.vdocuments.mx/reader030/viewer/2022032420/55a524ab1a28abe50e8b45b6/html5/thumbnails/9.jpg)
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
![Page 10: Improving code quality](https://reader030.vdocuments.mx/reader030/viewer/2022032420/55a524ab1a28abe50e8b45b6/html5/thumbnails/10.jpg)
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
![Page 11: Improving code quality](https://reader030.vdocuments.mx/reader030/viewer/2022032420/55a524ab1a28abe50e8b45b6/html5/thumbnails/11.jpg)
Premature abstraction
● “Third time is the charm.”
● KISS
● YAGNI
![Page 12: Improving code quality](https://reader030.vdocuments.mx/reader030/viewer/2022032420/55a524ab1a28abe50e8b45b6/html5/thumbnails/12.jpg)
Tools
● rubocop○ the code style grammar nazi
● reek○ code smell detection
● mutant○ mutation testing
● mbj/devtools○ the CI task!
![Page 13: Improving code quality](https://reader030.vdocuments.mx/reader030/viewer/2022032420/55a524ab1a28abe50e8b45b6/html5/thumbnails/13.jpg)
![Page 14: Improving code quality](https://reader030.vdocuments.mx/reader030/viewer/2022032420/55a524ab1a28abe50e8b45b6/html5/thumbnails/14.jpg)
![Page 15: Improving code quality](https://reader030.vdocuments.mx/reader030/viewer/2022032420/55a524ab1a28abe50e8b45b6/html5/thumbnails/15.jpg)