Three common ways of packaging code are methods, classes and namespaces. C# applications are written by combining new methods and classes that you write with predefined methods and classes available in the .NET Framework Class Library (also referred to as the FCL) and in various other class libraries. Related classes are often grouped into namespaces and compiled into class libraries so that they can be reused in other applications. You will learn how to create your own namespaces and class libraries in Chapter 9. The FCL provides many predefined classes that contain methods for performing common mathematical calculations, string manipulations, character manipulations, input/output operations, database operations, networking operations, file processing, error checking and many other useful operations.
Good Programming Practice 7 1
Familiarize yourself with the classes and methods provided by the FCL (msdn2.microsoft.com/en-us/library/ms229335). In Section 7.8, we present an overview of several common namespaces.
Software Engineering Observation 7 1
Don't try to "reinvent the wheel." When possible, reuse FCL classes and methods. This reduces application development time and avoids introducing programming errors.
Methods (called functions or procedures in other programming languages) allow you to modularize an application by separating its tasks into self-contained units. You have declared methods in every application you have written. These methods are sometimes referred to as user-defined methods. The actual statements in the method bodies are written only once, can be reused from several locations in an application and are hidden from other methods.
There are several motivations for modularizing an application by means of methods. One is the "divide-and-conquer" approach, which makes application development more manageable by constructing applications from small, simple pieces. Another is software reusabilityexisting methods can be used as building blocks to create new applications. Often, you can create applications mostly by reusing existing methods rather than by building customized code. For example, in earlier applications, we did not have to define how to read data values from the keyboardthe FCL provides these capabilities in class Console. A third motivation is to avoid repeating code. Dividing an application into meaningful methods makes the application easier to debug and maintain.
Software Engineering Observation 7 2
To promote software reusability, every method should be limited to performing a single, well-defined task, and the name of the method should express that task effectively. Such methods make applications easier to write, debug, maintain and modify.
Error Prevention Tip 7 1
A small method that performs one task is easier to test and debug than a larger method that performs many tasks.
Software Engineering Observation 7 3
If you cannot choose a concise name that expresses a method's task, your method might be attempting to perform too many diverse tasks. It is usually best to break such a method into several smaller methods.
As you know, a method is invoked by a method call, and when the called method completes its task, it either returns a result or simply control to the caller. An analogy to this application structure is the hierarchical form of management (Figure 7.1). A boss (the caller) asks a worker (the called method) to perform a task and report back (i.e., return) the results after completing the task. The boss method does not know how the worker method performs its designated tasks. The worker may also call other worker methods, unbeknownst to the boss. This "hiding" of implementation details promotes good software engineering. Figure 7.1 shows the boss method communicating with several worker methods in a hierarchical manner. The boss method divides the responsibilities among the various worker methods. Note that worker1 acts as a "boss method" to worker4 and worker5.