Coding best practices: Difference between revisions

Content deleted Content added
Turning an in-Wikitext comment that readers will _never_ see unless they edit into {{efn}} ==Notes== {{notelist}}. Loloolk, please do not insert comments into Wikitext as if it was computer code. Readers will never see them. It is better to be more explicit with an explanatory footnote or discuss it on the talk page.
Fixed typos relating to language (4th -> fourth). Added missing semicolon in code block.
Line 166:
 
and<syntaxhighlight lang="c">
return hours < 24 && minutes < 60 && seconds < 60 ? true : false;
</syntaxhighlight>and
 
Line 173:
</syntaxhighlight>
 
The first approach, which is much more commonly used{{dubious|date=December 2017}}, is considerably larger than the fifth. 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 2ndsecond 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 than the 4thfourth form.
 
The fourth and fifth forms obviously minimize the size, but they may increase the complexity: The fourth block requires knowledge of more advanced programming topics, notably [[Ternary operation|ternary operators]], while the fifth block leaves the "true" and "false" values implicit, and intermixes the notions of "condition" and "return value". It is likely obvious to most programmers, but a novice might not immediately understand that the result of evaluating a condition is actually a value (of type Boolean or its equivalent in whatever language), and thus can be manipulated or returned. In more realistic examples, the fourthfifth form could have problems due to [[operator precedence]], perhaps returning an unexpected type, where the prior forms would, in some languages, report an error. Thus, "simplicity" is not merely a matter of length, but of logical and conceptual structure; making code shorter may make it less or more complex.
 
For large, long lived programs using verbose alternatives could contribute to [[Software bloat|bloat]].{{dubious|date=December 2017}}