Why should methods be as short as possible and how adding tests first makes refactoring faster.
Before: More than 10 lines of code
Advantages of having complex methods
- Features can be implemented faster
- I know how it works
- And it works!
- Hard to debug with developer tools in a browser (needle in a haystack, haystack tracking?)
- /me_version_in_3_weeks—why the heck I put this here?
- /other_dev_version_next_day_code_review-what the heck…?
Don’t write code for yourself. Try to look and check it from the other dev, not only a team member perspective.
Covering with ‘test first’ and an instant feedback loop
- Solid foundation
- After each change test runner re-runs tests
- No fear of breaking something
- You’re only concerned with the public API
- Code is more readable by looking at private methods
- Debugging is faster as private methods will appear in the stack trace
Final version—does it smell?
- I will be ashamed of it in 3 weeks
- Feedback will be appreciated
Each step is a commit. Just checkout out to it and run tests with:
ember t -s -f 'errorize' -l 'PhantomJS'
Last but not least
How do you test Ember utils? How long do you think a method can be?
As I supposed, the code smell bad and was kind of anti-example. ;) I rewrote it a bit with Tomasz Bąk help. Another lesson learnt.