a method that gets longer than 20 to 25 lines is probably trying to do too much and should be divided into seperate methods, each with a clearly defined subtask.
SCJP
Visit my download page
Books: Pragmatic Unit Testing in Java, Agile Java, Modern C++ Programming with TDD, Essential Java Style, Agile in a Flash. Contributor, Clean Code.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Flexibility. Smaller methods make it easier to refactor (and to recognize design flaws, such as feature envy).
42
Originally posted by Greg Reinl:
What is "feature envy"?
Books: Pragmatic Unit Testing in Java, Agile Java, Modern C++ Programming with TDD, Essential Java Style, Agile in a Flash. Contributor, Clean Code.
Originally posted by Jeroen Wenting:
performance: not a myth (at least doesn't have to be).
Each method call takes time, and loads of them take loads of time.
There's a lot of times when there is no way to reduce the size of a function to 25 lines or so.
Just consider JDBC, JNDI and reflection. These produce stacks of Exceptions which have to be handled, leading to reams of code for that alone (in one example in a book there were 32 lines of exception handling code for 5 lines of application code, I'm sure we've all seen similar things many times).
There's no way such occurrances can be simply changed to reduce the number of lines within a method except extremely contrived procedures or simply passing all Exceptions up the chain of events until they hammer the end user.
Books: Pragmatic Unit Testing in Java, Agile Java, Modern C++ Programming with TDD, Essential Java Style, Agile in a Flash. Contributor, Clean Code.
... stacks of exceptions ...
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Originally posted by Jeroen Wenting:
Each method call takes time
In an extreme case (I admit) I once shaved 30% off the runtime of an application by unrolling a single functioncall.
Admittedly this was an extremely simple function (with only 2 short lines of code) which was called in only 2 places (both of which were inside short loops with extremely high (but variable) numbers of passes, but it can happen.
Application runtime from this action alone was reduced from 36 hours to 26 or so
There's a lot of times when there is no way to reduce the size of a function to 25 lines or so.
Just consider JDBC, JNDI and reflection. These produce stacks of Exceptions which have to be handled, leading to reams of code for that alone (in one example in a book there were 32 lines of exception handling code for 5 lines of application code, I'm sure we've all seen similar things many times).
There's no way such occurrances can be simply changed to reduce the number of lines within a method except extremely contrived procedures or simply passing all Exceptions up the chain of events until they hammer the end user.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Jeff Langr:
Some benefits (these are a few off the top of my head):
Originally posted by Ernest Friedman-Hill:
You forgot one of my favorites: testability. More small methods means that it's easier to test individual bits of functionality in isolation, and easier to subclass a class to create a test-specific stub.
Books: Pragmatic Unit Testing in Java, Agile Java, Modern C++ Programming with TDD, Essential Java Style, Agile in a Flash. Contributor, Clean Code.
SCJP
Visit my download page