Reduce the size of methods
m |
JaninaVoigt (Talk | contribs) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | In general, a small method is a good method. Not only is a small method an easier chunk of logic for a programmer to understand, it makes subclassing easier. Large methods in superclasses will often be partly correct for subclasses, but not completely correct. This means that the subclass has to re-write the whole method, even if most of it is correct. If your superclass only has small methods, | + | In general, a small method is a good method. Not only is a small method an easier chunk of logic for a programmer to understand, it makes subclassing easier. Large methods in superclasses will often be partly correct for subclasses, but not completely correct. This means that the subclass has to re-write the whole method, even if most of it is correct. If your superclass only has small methods, it is easier to change its behaviour in subclasses. Small methods are therefore an important part of software reusability. |
+ | |||
+ | If you have a large method in the superclass and you only want to change part of it in the subclass, you should split the superclass method into several methods so that the subclass can override only the methods that it wants to change. | ||
+ | |||
+ | Johnson and Foote state that a method with 30 lines of code is large and should be split up. | ||
== See Also == | == See Also == | ||
* [[Long method smell]] | * [[Long method smell]] | ||
+ | * [[Template Method]] | ||
+ | |||
+ | [[Category:Johnson and Foote's heuristics]] |
Latest revision as of 01:02, 20 August 2009
In general, a small method is a good method. Not only is a small method an easier chunk of logic for a programmer to understand, it makes subclassing easier. Large methods in superclasses will often be partly correct for subclasses, but not completely correct. This means that the subclass has to re-write the whole method, even if most of it is correct. If your superclass only has small methods, it is easier to change its behaviour in subclasses. Small methods are therefore an important part of software reusability.
If you have a large method in the superclass and you only want to change part of it in the subclass, you should split the superclass method into several methods so that the subclass can override only the methods that it wants to change.
Johnson and Foote state that a method with 30 lines of code is large and should be split up.