Creating methods is probably the single most often performed activity during programming. Thus, it is crucial that it is performed well. When creating a method, I think the most important thing is that it should perform one task and do it well: it should have high cohesion.
High cohesion is the fundamental principle in object oriented programming (probably the most important). If there was a way to require high cohesive methods, then it should be a requirement. I’m all for it.
If you have cohesive methods then it is a good sign that you are a good Object Oriented developer. It shows that you program for a human to read, not the computer. Having high cohesion is hard, you have to have one-task methods with good method names. It is a constant struggle to keep it cohesive with the additions/modifications. But if you accomplish that, then you’re good. :- )
So do you have to care about the method size?
Actually, you should not. But somehow, you find a lot of methods that are very long. A lot of them are over 100 lines, some are over 150 lines. I’ve seen even longer than that.
I think a good rule of thumb to adopt is to have methods that do not exceed your screen’s size. It’s a good rule. It allows you to see the whole method without scrolling. Which means your method should not be longer than 70-100 lines, roughly.
Alternatively, if you have a generic method name and your method is long, it should (be required to) be refactored and the related things put into its own method.
In the Signs You’re a Crappy Programmer (and don’t know it) article, the author states [that you are a crappy programmer] if:
You are adamantly opposed to function/methods over 20 lines of code. (or 30 or 10 or whatever number of lines) Sorry, sometimes a really long function is just what’s needed for the problem at hand. Usually shorter functions are easier to understand, but sometimes things are most simply expressed in one long function. Code should not be made more complex to meet some arbitrary standard.
I think the key word is “sometimes.” It’s OK to have a long method here and there, the problem is when you have it in a lot of places — in too many places. If your method does one function — one responsibility — and it takes over X lines, it’s fine, no reason to break it up. But a lot of times, those long methods “accrue” extra responsibilties, making them harder to maintain. I think everyone can agree that a long method is harder to understand and modify than a shorter one.
Actually, I think you are a crappy programmer if you have a lot of those long methods with non-specific names.
The bottom line is to have functions that do one task and do it well. If you do that, you don’t have to worry about the length, your methods will be short and easy to modify.
Everybody would benefit if you adhered to the rule of thumb, especially the maintainers of the code.
ReferenceSigns You’re a Crappy Programmer (and don’t know it), Damien Katz
RelatedWrite English the Way You Write Code – very good post
In smalltalk community (i also belongs to there now:) , there is convention usually not to write a method longer than 10-15 lines, So u don’t need to scroll the page to see other part (usually smalltalk browsers provides space for 10-15 lines).It doesn’t mean that all smalltalkers r crappy programmers.
I think it’s a joy to read short methods (if done well, of course), and if there was a way to force short methods I am all for it. Of course, you’re going to have a longer method from time to time, but overall, the closer to the 10-15 lines limit the better.
I have had a chance to modify 1000+ lines method and it is no fun.