See, the nasty thing about metrics is that as soon as you start measuring a system, you start changing it. It’s the Heisenberg Uncertainty Principle. Apparently studies have been done that indicate that in successful, maintainable, long lived applications, the average length of methods was 10 lines (I believe this number was derived from Smalltalk applications, but we’re arguing this particular number because we don’t have numbers for Ruby at the moment— this info is also second hand, so we can assume for the sake of argument it’s just arbitrary). The argument is now that we should try to keep our methods to no more than 10 lines, because going above 10 lines means we’re doing something wrong.
I have issues with this mentality. To wit:
end”? Does things.uniq.sort.map(&:to_s) count as more of a line than options ||= {}? Should I write more dense code so I can get more lines into a method?One of my biggest problems with the practice is that it takes the art out of coding. If you want a coder to make smaller methods that do less and are easier to read, tell him that, don’t tell him to make the methods 10 lines or less. The number conveys the means, not the end, in this case, and that means the message is lost. You want smaller methods to mean more maintainable code, not simply shorter methods. By focusing on the numbers, you remember the what but forget the why.
Conscious or not, once you start taking metrics, and placing any kind of value judgment on them, you will find that your team will start to value the metrics more than they value writing code that’s good for the sake of being good. I argue against taking or caring about metrics at all, because while some proponents suggest that you take metrics and then not care about them, I don’t think that’s really possible. Once metrics are taken, coders will want to “improve” them. I can hardly blame them; any good coder will invariably want to get better. But like the problem with Amazon’s Mechanical Turk, coders will change their reward from writing good code to writing code with good metrics.