SOFTWARE ESSENTIALS: DESIGN AND CONSTRUCTION

Chapter 5: Design and Documentation:

Answers for End of Chapter Conceptual Questions

  1. What is the notion of internal control?

The class designer hides all implementation details so that the client (application programmer) is not responsible for object state. Ideally, the client should be able to treat the custom type as if it were a built-in type.

  1. Describe the major components of a standard class design.

Class design encompasses data and its associated functionality. Standard class methods include accessors, mutators, private utility functions, constructors, and public functionality essential to external manipulation of the type.

  1. What are the differences in C++ class design vs. C# class design?

C++ classes with internal heap memory must provide destructors.

C# relies on implicit deallocation (garbage collection); classes do not need destructors.

Both languages should make explicit decisions with respect to copying.

C# class designers must implement a Cloneable interface, and require the application programmer to cast the object to the appropriate type, to support deep copying; by default, copying is shallow.

C++ class designers may suppress copying, support deep copying or employ move semantics.

  1. Why is a constructor needed?

Constructors remove the responsibility for initializing an object from the application programmer. With proper classs design, objects are automatically placed in a valid, initial state upon instantiation.

Recall that failure to initialize data does not mean that there is no value for the data; uninitialized data assumes whatever residual bit string value that resides in the allocated memory. Thus, a class without constructor yields objects in an indeterminate, initial state.

  1. What are the benefits of private utility functions?

Functional decomposition, readability, maintainability and consistency are all reinforced by the appropriate design and use of private utility functions.

Moreover, the encapsulation of private utility functions controls accessibility: application programmers do not have direct access to such methods.

  1. Why should a class designer minimize the provision of set and get routines?

Controlling access to an object’s internal data, state information, and class methods is a means of realizing the OO tenets of encapsulation and information hiding, both of which promote software maintainability. The more a client knows about an object’s internal state, the more feasible it is to program in a manner dependent on such internal details. Accessors (gets) expose part or all of an object state. Mutators change object state, and thus, should be conditional: class designer should determine when it is appropriate to reject a change made through a call to a mutator.

  1. When are destructors needed?

C++ destructors are resource managers. Typically, destructors are used to release internally allocated heap memory. Destructors may also be used to track number of active objects, decrement reference counts, close files, etc.

  1. What are the major differences between defensive programming and contractual design?

What are the benefits and drawbacks of each approach?

Defensive programming makes few assumptions about data, state or environmental factors and, thus, must implement extensive testing to ensure correct execution. Defensive programming may incur unwarranted overhead (and clutter code) but can effectively contain error.

Contractual design views software as a contract between the software designer and the application programmer. Using published pre and postconditions, contractual design places responsibility for correct execution on the client (under the assumption that the software has been implemented correctly). By avoiding exhaustive testing, efficiency is regained under the assumption that the application programmer will meet all preconditions.

  1. Describe the major components of Programming by Contract.
  2. Preconditions state required expectations for a correct function call
  3. Postconditions record potential and actual state changes after function execution.
  4. Interface invariants document stable conditions for use of the class.
  5. Implementation invariants document internal design decisions
  6. Class invariants describe key characteristics of the type defined by the class.
  1. How does Programming by Contract support the dual perspective?

Through careful delineation of responsibilities, via the interface and the implementation invariants, Programming by Contract identifies the responsibilities of the application programmer (program to the interface) as distinct from those of the class designer.

Dingle: SoftwareEssentialsch5 Design and DocumentationPage 1 of 3

Copyright@2014 Adair Dingle All Rights Reserved. Taylor & Francis