242
LESSON 20 Reusing Code with Methods
Much more importantly, it also saves you the trouble of debugging the code several times. Often
debugging a piece of complex code takes much longer than typing in the code in the first place, so
being able to debug the code in only one place can save you a lot of time.
Reusing code also greatly simplifies maintenance. If you later find a bug in the code, you only need
to fix it in one place. If you had several copies of the code scattered around, you’d need to fix each
one individually and make sure all of the fixes were the same. That may sound easy enough, but
making synchronized changes is actually pretty hard, particularly for larger projects. It’s just too
easy to miss one change or to make slightly different changes that later cause big problems.
Methods can sometimes make finding and fixing bugs much easier. For example, suppose you’re
working on an inventory program that can remove items from inventory for one of many reasons:
external sales, internal sales, ownership transfer, spoilage, and so forth. Unfortunately the program
occasionally “removes” items that don’t exist, leaving you with negative inventory. If the program
has code in many places that can remove items from inventory, figuring out which place is causing
the problem can be tricky. If all of the code uses the same method to remove items, you can set break-
points inside that single method to see what’s going wrong. When you see the problem occurring, you
can trace the program’s flow to see where the problem originated.
A final set of advantages to using methods makes the pieces of the program easier to understand and
use. Breaking a complex calculation into a series of simpler method calls can make the code easier to
understand. No one can keep all of the details of a large program in mind all at once. Breaking the
program into methods makes it possible to understand the pieces separately.
A well-designed method also encapsulates an activity at an abstract level so other developers don’t
need to know the details. For example, you could write a
FindItemForPurchase method that searches
through a database of vendors to find the best possible deal on a particular item. Now developers writ-
ing other parts of the program can call that method without needing to understand exactly how the
search works. The method might perform an amazingly complex search to minimize price and long-
term expected maintenance costs but the programmer calling the method doesn’t need to know or care
how it works.
In summary, some of the key benefits to using methods are:
Code reuse
— You write the code once and use it many times.
Centralized debugging
— You only need to debug the shared code once.
Centralized maintenance
— If you need to fix the code, you only need to do so in the method,
not everywhere it is used.
Problem decomposition
— Methods can break complex problems into simple pieces.
Encapsulation
— The method can hide complex details from developers.
METHOD SYNTAX
In C#, all methods must be part of some class. In many simple programs, the main form contains all
of the program’s code, including all of its methods.
596906c20.indd 242 4/7/10 12:33:27 PM