improving code quality

15
Improving code quality @jsuchal

Upload: jano-suchal

Post on 14-Jul-2015

321 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Improving code quality

Improving code quality@jsuchal

Page 2: Improving code quality

Issues

1. Doing the wrong thing

2. Excessive coupling

3. Mutable state

4. Premature abstraction

5. Edge conditions

Page 3: Improving code quality

Tips&

Tricks

Page 4: Improving code quality

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

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
Page 7: Improving code quality
Page 8: Improving code quality

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

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

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

Premature abstraction

● “Third time is the charm.”

● KISS

● YAGNI

Page 12: Improving code quality

Tools

● rubocop○ the code style grammar nazi

● reek○ code smell detection

● mutant○ mutation testing

● mbj/devtools○ the CI task!

Page 13: Improving code quality
Page 14: Improving code quality
Page 15: Improving code quality