80 likes | 241 Views
19. PROGRAM DESIGN. Module Design. • It’s a good idea to think of each source file as a module, with an associated header that describes what the module provides to the rest of the program (the module’s interface). • Typical kinds of modules: A collection of functions A data structure
E N D
Module Design • It’s a good idea to think of each source file as a module, with an associated header that describes what the module provides to the rest of the program (the module’s interface). • Typical kinds of modules: A collection of functions A data structure A type that represents a data structure
Information Hiding • Information hiding means that one part of program hides details from other parts of the program. • When we design a module, we want the rest of the program to have access to information in the module’s header file, not information in the source file. • Advantages of information hiding: Easier to find bugs. More importantly, easier to make major changes to a data structure without affecting the rest of the program.
Information Hiding • The static storage class is useful for enforcing information hiding in C. • static variables and functions are limited to use within a single file (module); they’re hidden from the rest of the program.
Abstract Data Types • A module that encapsulates a data structure has one disadvantage: there’s no way to have multiple instances of the data structure. • To accomplish this, we need a new type; the users of the module can then define several variables of that type. • When the details of a type are hidden from the users of the type, it’s called an abstract data type.
Abstract Data Types • Programmers have devised various strategies for implementing abstract data types in C, but most involve confusing preprocessor tricks. • The most effective way to solve the problem of defining abstract data types is to use C++. C++ allows the definition of classes, which resemble structures in C, except that: Classes may contain functions as well as data. Members of classes may be hidden from the rest of the program.
Efficiency • One of C’s strengths—and undoubtedly one of the reasons for its success—is the ability it gives programmers to write efficient programs. • However, there’s a right way and a wrong way to achieve efficiency. • The right way: Focus on choosing the right algorithms and data structures. Use a profiler to find the program’s “hot spots”—sections of code that consume most of the program’s running time. Once the hot spots have been identified, rewrite them for greater efficiency.
Efficiency • The wrong way: Condense code as much as possible. • The gains from “micro-optimization” are small at best (perhaps a few percent) and are often offset by a decrease in readability and modifiability. • In any event, many of today’s compilers routinely perform the sort of optimizations that programmers used to do by hand. • There’s nothing wrong with making code shorter; just be sure that the primary goal is clarity, not efficiency.