Coding best practices: Difference between revisions

Content deleted Content added
m Keep the code simple: '3rd' -> 'third'
Xeno333 (talk | contribs)
m Added some clarity and fixed spelling.
Line 4:
}}
 
'''Coding best practices''' or '''programming best practices''' are a set of informal, sometimes personal, rules (''[[best practice]]s'') that many [[software developer]]s, in [[computer programming]] follow to improve [[software quality]].<ref name="McConnell 2004 p. ">{{cite book |last=McConnell |first=Steve |author-link=Steve McConnell |title=Code Complete |publisher=Microsoft Press |publication-place=Redmond, Wash. |year=2004 |isbn=978-0-7356-9125-4 |oclc=61315783 |page={{page needed|date=November 2023}}}}</ref> Many computer programs require being robust and reliable for long periods of time,<ref>{{cite book|title=Software Engineering|edition=Seventh|last=Sommerville|first=Ian|year=2004|publisher=Pearson|isbn=0-321-21026-3|page=38}}</ref> so any rules need to facilitate both initial development and subsequent maintenance of [[source code]] by people other than the original authors.
 
In the [[ninety–ninety rule]], Tom Cargill is credited with an explanation as to why programming projects often run late: <!-- these do add to 180%, and are meant to (=late project). See linked Ninety-ninety rule --> "The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time."<ref name="Bentley1985">{{cite journal|last=Bentley|first=Jon|year=1985|title=Programming pearls: Bumper-Sticker Computer Science|journal=Communications of the ACM|volume=28|issue=9|pages=896–901|issn=0001-0782|doi=10.1145/4284.315122|s2cid=5832776|doi-access=free}}</ref> Any guidance which can redress this lack of foresight is worth considering.
Line 123:
It is usually considered good practice to use descriptive names.
 
Example: A variable for taking in weight as a parameter for a truck can be named TrkWeight, TruckWeightKilograms or TruckWeightKilogramsTruck_Weight_Kilograms, with TruckWeightKilograms (See [[Pascal case]] naming of variables) often being the preferable one since it is instantly recognisable.recognizable, Seebut [[Pascalnaming case]]convention namingis ofnot variablesalways consistent between projects and/or companies.
 
===Keep the code simple===
The code that a programmer writes should be simple. Complicated logic for achieving a simple thing should be kept to a minimum since the code might be modified by another programmer in the future. The logic one programmer implemented may not make perfect sense to another. So, always keep the code as simple as possible.<ref>{{cite web |author=Multiple (wiki) |title=Best practices |url=http://docforge.com/wiki/Best_practices|title=Best practices|author=Multiple (wiki)|work=Docforge|accessdate=2012-11-13 |work=Docforge}}</ref> Sometimes, a developer or project manager will decide to employ more complex code for performance.
 
'''For example,''' consider these equivalent lines of C code:{{efn|Since true and false are not defined in regular C code, assume that the "stdbool.h" library has been included.}}
Line 175:
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 second 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 fourth form.
 
The third takes advantage of the C ''switch'' keyword, this is sometimes done for performance, but in some cases, such as this, it makes the code more complex with little to no increase in performance.
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 fifth 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.
 
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 fifth 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, but in some cases may be at the cost of performance.
 
For large, long lived programs using verbose alternatives could contribute to [[Software bloat|bloat]].{{dubious|date=December 2017}}