Crash Course In Computer Coding
The building blocks of computer coding regardless of language (Ada, Basic, C,
Fortran, Pascal) are: declaration statements, conditional statements and loop
statements.
Declaration Statements: Examples
A = expression;
C = sin(value);
B = CalculateSpeed(parameters);
D = E;
Conditional Statements: Examples
If (some test) Then {statement1; statement 2;...statement N};
If (some test) Then {statement1; statement 2;...statement N}
Else {statement1;....statement n};
Case or Switch (some variable)
{
case (variable 1): statement1;...statement n;
case (variable 2): statement1;...statement n;
...
case (variable n): statement1;...statement n;
}
Loop Statements: Examples
For (some test) repeat {statement1; statement2;...statement n};
While (expression) do { statement1; statement2;...statement n};
Do {statement1; statement2;...statement n;} Until (expression);
Routines
Computer code perform multiple operations. Closely related operations are lumped
together into a routine to improve manageability of code. The relationship between
operations is called cohesion.
There are six levels of acceptable cohesions between operations in a routine;
Functional, Sequential, Communicational, Temporal, Procedural and Logical.
Function Cohesion
This is the best kind of routine. It performs one and only operation. Examples: sin(),
cos()... GetLocation().
Sequential Cohesion
This the second best routine. It is used when closely related operations share the
same data operations and must be performed is a specific order. Example:
OpenFile(), ReadFile(), PrintFile() and CloseFile().
Communicational Cohesion
This routine is used when unrelated operations make use of the same data. Example:
GetNameAndChangePhoneNumber(), GetAddressAndName(),
SaveNameAndPhoneNumber().
Temporal Cohesion
This routine is used to combine unrelated operations done at the same time.
Example: StartUp(), ShutDown(), Suspend(), Resume() Initalize(),
GetInterruptStatus().
Procedural Cohesion
This routine is used when closely related operations don't share the same date.
Example: PrinRevenueReport(dataX), PrintExpenseReport(dataY),
ListEmployeePhoneNumbers(DataZ).
Logical Cohesion
This routine is used when only one of the operations is selected by a control flag. all
the operations are in a big If or Case statement. Example: InputAll(flag),
ComputeAll(flag), EditAll(flag), PrintAll(flag) and Save(flag).
Coupling
Coupling is the complement of cohesion. Cohesion describes how strongly the
internal operations are related in a routine. Coupling describes how strongly a
routine is related to other routines.
Good coupling between routines is loose enough that any routine can easily be
called by other routines. Routines should depend very little on other routines. For
Example, a routine like sin() is loosely coupled because everything it needs to know
is passed to it with a value representing an angle.
|