Coding best practices: Difference between revisions

Content deleted Content added
m please read inline comment and see referenced link. Undid revision 949862197 by 2600:6C64:647F:D86B:84AF:28C9:1A80:FDE4 (talk)
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags
Line 304:
'''For example,''' consider these equivalent lines of C code:
 
<sourcesyntaxhighlight lang="c">
if (hours < 24 && minutes < 60 && seconds < 60)
{
Line 313:
return false;
}
</syntaxhighlight>
</source>
 
and
 
<sourcesyntaxhighlight lang="c">
if (hours < 24 && minutes < 60 && seconds < 60)
return true;
else
return false;
</syntaxhighlight>
</source>
 
and
 
<sourcesyntaxhighlight lang="c">
return hours < 24 && minutes < 60 && seconds < 60;
</syntaxhighlight>
</source>
 
The 1st approach, which is much more commonly used{{dubious|date=December 2017}}, is considerably larger than the 3rd. In particular, it consumes 5 times more screen vertical space (lines), and 97 characters versus 52 (though editing tools may reduce the difference in actual typing). It is arguable, however, which is "simpler". The first has an explicit if/then else, with an explicit return value obviously connected with each; even a novice programmer should have no difficulty understanding it. The 2nd merely discards the braces, cutting the "vertical" size in half with little change in conceptual complexity. In most languages the "return" statements could also be appended to the prior lines, bringing the "vertical" size to only one more line that the 3rd form.