The horizontal complexity of source code pertains to both line length and to control nesting level. The Spartan programming methodology dictates minimization of horizontal complexity, and even place this objective higher than any other complexity metric minimization objective. This is because it is believed that horizontal scrolling is even more intellectually demanding then vertical scrolling. Further, it is believed that deep nested control is difficult to understand.
Specific techniques for reducing horizontal complexity include:
return
) or from a loop (via break
or continue
).if
statement, if the main branch ends with a return
statement or any other statement that does not let control carry through to the else
branch. For example, instead of
int abs(int x) {
if (x < 0)
return -x;
else
return x;
}
write
int abs(int x) {
if (x < 0)
return -x;
return x;
}