1 / 8

19. PROGRAM DESIGN

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

harlan
Download Presentation

19. PROGRAM DESIGN

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 19. PROGRAM DESIGN

  2. 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

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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.

More Related