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!

Disadvantages

  • 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…?

Takeaway

Don’t write code for yourself. Try to look and check it from the other dev, not only a team member perspective.

First version

link to gist

Covering with ‘test first’ and an instant feedback loop

  • Solid foundation
  • After each change test runner re-runs tests
  • No fear of breaking something

link to gist

Final version

  • 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

link to gist

Final version—does it smell?

  • Yes
  • I will be ashamed of it in 3 weeks
  • Feedback will be appreciated

Example github repo

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?

Update

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.

link to gist