7.2 Packaging Code in C#

So far, we’ve used properties, methods and classes to package code. We’ll present additional packaging mechanisms in later chapters. C# apps are written by combining your properties, methods and classes with predefined properties, methods and classes available in the .NET Framework Class Library and in other class libraries. Related classes are often grouped into namespaces and compiled into class libraries so that they can be reused in other apps. You’ll learn how to create your own namespaces and class libraries in Chapter 15. The Framework Class Library provides many predefined classes that contain methods for performing common mathematical calculations, string manipulations, character manipulations, input/output operations, graphical user interfaces, graphics, multimedia, printing, file processing, database operations, networking operations, error checking, web-app development, accessibility (for people with disabilities) and more.

Software Engineering Observation 7.1

Don’t try to “reinvent the wheel.” When possible, reuse Framework Class Library classes and methods (https://msdn.microsoft.com/library/mt472912). This reduces app development time and errors, contributes to good performance and often enhances security.

7.2.1 Modularizing Programs

Methods (called functions or procedures in other programming languages) allow you to modularize an app by separating its tasks into self-contained units. You’ve declared methods in every app you’ve written. The actual statements in the method bodies are written only once, can be reused from several locations in an app and are hidden from other methods.

There are several motivations for modularizing an app with methods:

  • Software reuse. For example, in previous apps, we did not have to define how to read a line of text from the keyboard—C# provides this capability via the Console class’s ReadLine method.

  • Avoiding code repetition.

  • Making programs easier to test, 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.

7.2.2 Calling Methods

As you know, a method is invoked by a method call, and when the called method completes its task, it returns control and possibly a result to its caller. An analogy to the method-call-and-return structure is the hierarchical form of management (Fig. 7.1).

Fig. 7.1 Hierarchical boss-method/worker-method relationship.

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 need to know how the worker method performs its designated tasks. The worker may call other worker methods, including ones unknown to the boss. As we’ll see, 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 divides the responsibilities among the various workers. Note that worker1 also acts as a “boss” to worker4 and worker5. The relationship does not need to be hierarchical, but often it is, which makes it easier to test, debug and update programs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset