Functional Cohesion: A property of a module in which all concrete steps are directed towards solving just one problem, and any significant subproblems are written as abstract steps.
This is from the book.
A better definition says that the elements of the module "belong" together
They all work towards a common goal or tasks
They are all at the same level of complexity
All low level code
All high level code
But not a mixture.
A function should do one thing, but it might be a complex thing.
When a function becomes too complex, stop and refactor it.
Refactoring is modifying code to improve quality.
Simply break the existing code down into modules.
Look for generic sections or generic processes.
Some information from Chapter 9
The scope of an identifier is the region of program code where it is legal to reference or use the identifier.
scope of visibility
scope of access
Types of scope:
Global scope: identifiers declared outside of all functions.
Named constants and functions
We will also declare data types in a global scope.
DO NOT declare variables in a global scope.
Global scope is from the point of declaration to the end of the code.
local scope: identifiers declared within a code block.
local variables.
function parameters
anything within a statement which employs a code block (for)
Valid from the declaration until the close of the code block.
Class scope: later.
Reusing an identfier
int i;
for (int i = 0; ...) {
}
The programmer has declared i twice.
Both are in scope.
But the C++ rule is that the one in the closest scope is the one that is used.
So inside the for loop for (int i... is employed
Outside the llop int i is used.
This is known as name hiding
Don't do it.
The name precedence syntax rules cover this (as above)
Lifetime
The scope of an identifier is where (in the code) the identifier is available
The lifetime of a variable (or constant) is when the variable (or constant) has memory allocated to it.
Most variables in C++ are automatic
When the flow of control enters the block containing the variable definition, it is allocated memory
When the variable goes out of scope, the memory is deallocated
The lifetime of a variable can be extended by making it static
declare with the reserved word static
static int callCount = 0;
Unusually initialized in declaration.
Semantics
First time the variable comes into scope, it is initialized
When it goes out of scope, the value is retained
Every other time it comes into scope, the initialization is ignored.
Side Effect
Side effect is any effect of one function on another function that is not part of the explicitly defined interface between them.
Global variables cause side effect.
This causes bugs which are exceedingly difficult to find.
Global constants do not cause side effect because they can not be changed.
Stubs/Drivers
A stub is a dummy function that assists in testing part of a program.
A driver is a simple main routine that is used to call a function being tested.