Hardware Reference
In-Depth Information
One of the greatest sources of bugs is in the interface between functions. It is a common
mistake to have type mismatches between the formal parameters and the actual parameters. It
is also common to mis-order parameters, or to leave some out. The programmer should always
be scrupulous in checking for such problems when writing a program and do it early on. One
way to minimize the problem is to keep the number of parameters to any one function at a mini-
mum. Seven or more parameters for a function is probably too many—having this many param-
eters is an indication that the programmer has structured his or her program in the wrong way.
5.13.4 Code Appearance
In addition to comments, program readability can be improved by
Proper spacing
Proper indentation
Vertical alignment
P ROPER S PACING
Adding spaces makes your program statements easier to read. For example, it is quite obvi-
ous that the second one of the following two statements is easier to read:
for (i 5 0; i , 15; i 11 ) &
for (i 5 0; i , 15; i 11 )
P ROPER I NDENTATION
Indentation is especially helpful in improving the readability of nested if-elseif statements
and nested program loops. With proper indentation, the logical hierarchies of nested if-elseif
statements and nested program loops become much clearer. For example, a programmer may
write the main loop of the seven-segment patterns shifting program as follows:
while(TRUE) {
for (i 5 0; i , 10; i 11 ) {
// pattern array start index
for (j 5 0; j , 100; j 11 ) {
// repeat loop for each pattern sequence
for (k 5 0; k , 6; k 11 ) {
// select the display # to be lighted
PTB 5 SegPat[i 1 k];
// output segment pattern
PTP 5 digit[k];
// output digit select value
delayby1ms(1);
// display one digit for 1 ms
}
}
}
}
Obviously, the readability of these statements is very poor. It is quite difficult to identify
the hierarchical relationship of these for statements. The readability would be much improved
if these for statements are indented as follows:
while(TRUE) {
for (i 5 0; i , 10; i 11 ) { // pattern array start index
for ( j 5 0; j , 100; j 11 ) { // repeat loop for each pattern sequence
for (k 5 0; k , 6; k 11 ) { // select the display # to be lighted
PTB 5 SegPat[i 1 k];
// output segment pattern
PTP 5 digit[k];
// output digit select value
 
Search WWH ::




Custom Search