White Space
White space enhances readability by making the individual components of statements stand out.
Surround =, &, and | by spaces
Using space around the assignment character provides a strong visual cue separating the left and right hand sides of a statement. Using space around the binary logical operators can clarify complicated expressions.
simpleSum = firstTerm+secondTerm;
Conventional operators can be surrounded by spaces
This practice is controversial. Some believe that it enhances readability.
simpleAverage = (firstTerm + secondTerm) / two;
1 : nIterations
Commas can be followed by a space
These spaces can enhance readability. Some programmers leave them out to avoid split lines.
foo(alpha, beta, gamma)
foo(alpha,beta,gamma)
Semicolons or commas for multiple commands in one line should be followed by a space character
Spacing enhances readability.
if (pi>1); disp(‘Yes’); end
Keywords should be followed by a space
This practice helps to distinguish keywords from functions.
Logical groups of statements within a block should be separated by one blank line
Enhance readability by introducing white space between logical units of a block.
Blocks should be separated by more than one blank line
One approach is to use three blank lines. By making the space larger than space within a block, the blocks will stand out within the file. Another approach is to use the comment symbol followed by a repeated character such as *
or -
.
Use alignment wherever it enhances readability
Code alignment can make split expressions easier to read and understand. This layout can also help to reveal errors.
weightedPopulation = (doctorWeight * nDoctors) + …
(lawyerWeight * nLawyers) + …
(chiefWeight * nChiefs );