Eric Wise

Sponsors

The Lounge

Wicked Cool Jobs

Blogs I Read

Fun & Games

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Interesting Concept (discuss)

I have heard of a senior developer who had a rule that no method may exceed one “page” of screen scroll.  If it does it has to be broken into smaller methods.

Genius or Idiocy?

  • This coding style would help clean up code by forcing you to break everything into managable small pieces. 
  • Small reusable pieces
  • It might complicate things by causing “cursor jump” in debugging.

Posted Mon, Dec 13 2004 8:13 AM by Eric Wise

[Advertisement]

Comments

Jim Bolla wrote re: Interesting Concept (discuss)
on Mon, Dec 13 2004 4:54 AM
I wouldn't consider it a *rule* per se, but if a method starts to get that long, I ltry to see if there is some way to break it into two or more smaller methods that have meaningful method names, making the code easier to read.
Darrell wrote re: Interesting Concept (discuss)
on Mon, Dec 13 2004 5:19 AM
Agree with Bolla. Absolute standards don't usually work. Besides, if a long method is highly cohesive, what's the point in breaking it up?
David Hayden wrote re: Interesting Concept (discuss)
on Mon, Dec 13 2004 6:00 AM
It sounds like more of a "rule of thumb" associated with one of the refactoring code smells of methods being really long. It is a hint that perhaps a new class or method could be introduced.

I agree with the others. As long as the code is understandable and fairly cohesive, I am not sure that "the very long method" code smell in of itself is enough to justify refactoring unless you just like doing refactoring for the sake of refactoring, and in that case, it's a good learning experience.

A better code smell would be if a segment of code within that long method also needed to be used somewhere else and thus would have to be duplicated. I would then extract out the segment of code into a new method (or class), which would eliminate both smells :)

As I was thinking in a recent post about refactoring-

http://davidhayden.com/blog/dave/archive/2004/12/09/675.aspx

the refactoring process seems "un-agile" as the very definition of refactoring talks about "changing code without changing its behavior." This suggests that there has to be more to splitting up that page long method because it doesn't fit on the screen.

Although :), certainly it is nice to be able to see all the code associated with a method on the screen at once.
TetsujinOni wrote re: Interesting Concept (discuss)
on Mon, Feb 14 2005 8:37 AM
Don't forget that a single use which can be clearly named such that a composed method calls well-named, small methods which are cohesive and simple to digest is an improvement in the design of the application.

Agile mantra:

Broken test - (Refactor to make fixing test easy) - Fixed test - Refactor to make design clean.


TetsujinOni wrote re: Interesting Concept (discuss)
on Mon, Feb 14 2005 8:38 AM
Oh, and you shouldn't have to worry about cursor jump in debugging... You should be able to debug in the 'detail' methods if you need to start your debugger at all.
Devlicio.us