I hear a lot of talk about ‘less code’ being something we should favour.
[Edit - I should qualify that by 'favouring less code' I mean favouring a shorter class file or fewer classes in the code base, not splitting long methods into shorter methods or large classes into single responsibility classes, both of which I'm absolutely sold on.]
I disagree, and out of curiosity I just did a little experiment to see whether my feeling was well founded.
I wrote a ruby script which counts the words in source files. It’s not perfect but it’s good enough for this purpose because if it’s inaccurate it’ll err on the side of over-counting rather than under-counting. It counts = as a word if it’s surrounded by white space for example.
I can touch-type at 80 wpm. Here’s how that expands up if we imagine that I can type constantly, machine like, while I’m coding. (Of course I can’t, but bear with me).
- 80 x 60 = 4800 words per hour
- 60 x 8 = 38400 words per day
I just ran my script on the src and test folders of my main project. I’d estimate that this code base has taken me 200 (or more) working days to write. This 200 days excludes project management time, coaching other devs, client meetings etc. This is 200 days of quality time Stray spent with TextMate.*
The results from the script are kind of mind blowing:
Typing this project should have taken 1624 minutes, which is 27 hours or 3 days.
test: .as – 718 files / 61457 lines of code / 143728 words of code
Typing this project should have taken 1796 minutes, which is 29 hours or 3 days.
Typing the code in my project should have taken 7 days (the 3 days is a round down on both figures). It’s interesting to note that I write more test code than production code – I find that reassuring, I’d actually like to see that proportion favour test code even further.
But here’s the takeaway: Of my 200 days spent writing this code, only 7 days were spent actually writing it. What was I doing with the other 193 days of time? Thinking about what code to write.
Even if we assumed that I threw-out a third of the code that I wrote, that would only take us to 10 days spent writing code.
95% of time spent writing code is spent
not writing code.
So – even if we doubled the importance of writing code, and called it 10% of our effort, 90% of our concern should be on making our code easy to understand and hard to break.
Sometimes a shorter implementation is less to read and less to think about. But frequently it isn’t.
You can find out how much time you don’t spend writing code with the ruby script here https://gist.github.com/771048
(and thanks to Mr Hallman for the original countLines script I hacked to make it)
Give your code 5 minutes of love
The metrics show that my project consists of about 1650 files. I like a lot of small classes and it’s a big project. I’d estimate that half those classes are events, signals, bootstraps, interfaces and VOs that are so simple that there’s nothing to refactor within them.
So maybe 800 of those files are classes that have something really meaningful inside them – either source files or interesting test cases. The perfect time to refactor your code is when it’s passing unit tests.
I think my code base is healthy on the whole (robotlegs FTW in the war of attrition against code base hatred). But If I’d taken 5 extra minutes on each meaningful class and test, when the class first became ‘part’ of the app, before I closed the files, to do a little extra code craft and really polish the code, I would really *love* my code base. And it would love me back.
Based on this project’s metrics, it would have cost me 20 minutes each day. I’m pretty sure that those 20 minutes would have paid off in saved thinking time, and if not then I’d have made them up in less internet browsing / twittering / youTube watching yada yada.
So – my 2011 new year’s resolution is to give every meaningful class 5 minutes of love right when I would otherwise close it. Anyone care to join me? And remember – it’s 90% not about less code.
* A lot of this time is actually in short bursts which TDD makes much more productive.