Lecture of 3 April 1996
Examples C++ Programs Describing Instantiation
- A directory of examples shows
how one can and cannot instantiate template classes in a
C++ program using the g++ compiler.
Visibility and Dependency
- Visibility addresses the scope of identifiers, that
is, the region of a program in which an identifier's binding is
known.
- C++ controls a related property, namely
access to bindings. The region of a program
in which a visible binding can be used.
Coupling and Cohesion
- Coupling refers to (generally undesirable) dependencies between
modules or classes.
- Types of coupling idenfitied by Budd (listed from most
obtrusive to least obtrusive) are:
- Internal data coupling (real bad)
- Global data coupling (pretty bad again)
- Control (or sequence) coupling (this is bad too)
- Parameter coupling (can't be avoided)
- Subclass coupling (vital to OOP)
- Consider the solitaire program from the standpoint of coupling.
- Does it exhibit global data coupling?
- Does it exhibit sequence coupling?
- Cohesion refers to the (generally desirable) internal
connectedness of a module or class, that is, how well it hangs
together.
- Types of cohesion identified by Budd (listed from least desirable
to most desirable) are
- Coincidental cohesion
- Logical cohesion
- Temporal cohesion
- Communication cohesion
- Sequential cohesion
- Functional cohesion
- Data cohesion
- (After Constantine's module cohesion evaluation techniques) To
evaluate cohesion of a class, write a sentence describing the
purpose of the class.
- If the sentence is compound, or a run-on, you may have too
many functions in the class.
- If the sentence has reference to time, it may exhibit
sequential or temporal binding.
- If there is not a single well defined subject or object,
it may exhibit logical cohesion.
The Law of Demeter
- Law of Demeter In any method M attached to a class
C, only methods defined by the following classes may be used:
- The instance variable classes of C.
- The argument classes of method M (including C)
[Global objects or objects created inside method M are
considered to be arguments of M.]
- Law of Demeter (weak form) Inside a method, it
is only permitted to access or send messages to the following
objects:
- The arguments associated with the method being executed
(including the self object)
- Instance variables for the receiver of the method
- Global variables
- Temporary variables created inside the method
- Law of Demeter (strong form) Inside a method it
is only permitted to access or send messages to the following objects:
- The arguments associated with the method being executed
(including the self object)
- Instance variables defined in the class containing the method
being executed
- Global variables
- Temporary variables created inside the method
This form of the law doesn't allow access to data members of base
classes.
- Wirfs-Brok and Wilkerson advocate providing access to data members
within the same class only via accessor functions. This is
enforced in Dylan with the setter/getter methods. There is no
syntactic difference or semantic difference between accessing slots
and calling a programmer-defined setter or getter.
- Note that one can still write undisciplined programs that satisfy
the Law of Demeter. As in life, the law doesn't make us good,
it merely draws distinctions between those practices that are known
to generally produce undesirable results and those that aren't.
In particular, global data coupling can be rampantly exploited
in a program satisfying the law of demeter.
This document is copyright 1996 by Joseph N. Wilson.