As a general rule, long functions indicate something that should be broken
up into smaller functions, as there are usually a number of logical
divisions of functionality going on in a function that would be more suited
to separate function definitions for use in other capacities.
However, that is a general rule, and it is based upon a solid foundation of
mathematical logic. A general rule is useful only as a general rule. Once
one steps outside the parameters of the specific logic that creates that
rule, other logical rules apply.
A general rule is not the same as a constant or universal rule.
Understanding the logic behind it is important if one wants to step beyond
the boundaries of what is generally or popularly known and thought, and
enter into the realm of solid scientific principle, fact, and discovery,
wherein lies real power.
Or, as my Uncle Chutney lokes to keep reminding me "Neither a follower nor a
lender be."
A function call employs indirection. This has a corresponding cost, albeit
small. Of course, as Uncle Chutney would say "Big things are made up of lots
of little things." Indirection for no logical purpose is wasteful, any way
you slice it. So, the real question is, why do such "recommendations" and
"rules" exist in certain types of software and technologies?
The answer is, "for the same reason that there are little white speed
recommendation signs on sections of roads that curve steeply." These signs
are not law, they are recommendations. They are there for the inexperienced
driver, those with diminished capacity, and for those who do not care to
think, learn, or research; for the young, the infirm, and the lazy. For
those who can't put down their cell phones long enough to pay attention to
their driving. And for the young and the infirm, they are there for a good
reason. They save lives. For the lazy, on the other hand, there is no
excuse.
I guess where I'm going with this is, who cares what some well-know Java
project management platform, or "Software Developer" magazine has to say
about anything? Surely these statements must be studied, and understood. But
to simply follow one's supposed "peers," or even to care what opinions are
held by others, is to become a Lemming.
There was a day when most software developers were rugged individualists,
bent on "pushing the envelope" with regards to making software do what it
could. There were days when "Best Practices," "Recommendations," and "Design
Patterns" did not exist. Now, am I saying that these things are bad? Far
from it. I tend to follow most of Microsoft's Best Practices and
Recommendations. Not because they come from Microsoft, though. Because they
are logical, I understand them, and I agree with them.
For example, I use Microsoft naming conventions in my code. Why? It makes it
easier to read and maintain. Of course, a naming convention is arbitrary.
So, why not create my own? Because if I hire someone, I want them to be able
to jump right into my code without having to acquaint their self with a
whole new naming convention. And to come up with a new arbitrary one would
serve no logical purpose. And this is how I approach everything I do.
Oddly enough, however, I always seem to find myself being challenged with
something new, something "outside the lines." And I have learned the guiding
principles of logic that do not fail me when I have to step beyond the realm
of "typical" software development. In fact, I actually enjoy it. The only
thing I do NOT enjoy is boredom.
So, when it comes to breaking up functions, I would exhort anyone to not
simply "follow the leader." As well-intentioned as they may (or may not) be,
there are reasons for such conventions. I would be loathe to break up a long
function into smaller pieces simply because it was longer than, say, 42
lines in length. However, I might be inclined to take a second look at it,
an analyze it further to see what might be done to improve it, repurpose
some of it, etc.
The Code Analysis tools in Visual Studio.Net 2005 are another excellent
example. These can be quite helpful. They are not, however, a set of rules
to which one should always adhere. There have been times when I looked at
one of these analyses and said to myself, "Self, that is a good idea there.
You should implement it." There have been other times when I looked at one
of these analyses and said to myself "Self, that may be a good idea in many
circumstances, but if you implement it here, I will never speak to you
again." (Some might consider that a healthy thing, but that's another topic
altogether!)
So, am I disagreeing with anyone here? Nah. But I have seen a trend of late,
particularly among those in the "enemy" camp (the anti-Microsoft camp). I'm
starting to hear a lot of pop programming jargon. And it worries me.
Especially when I see so much lingo from that camp making it over here, as
if the less successful had something to teach to the master (that would be
Microsoft). And that puzzles and concerns me. The world can do with a few
less Dilberts, and a few more Albert Einsteins.
Anyway, that's my story, and I'm stickin' to it!
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.