Monday, September 21, 2009

There is an actual coding standards document where I work that seems to be pretty much ignored. The one for Java is pretty long and has some stupid things in it.

3. Set the IDE settings to use 4 spaces instead of tab and use 4 spaces for each indentation.
I have emacs set to always indent with spaces with 4 space increments, so I follow this. A lot of people use tabs.

5. NEVER put a TODO because we may never get back to it. Take extra time or add it to schedule, but do it right then and there.

7. Do not leave commented code in the code base.
These two are routinely violated. I tend to delete code rather than comment it out, but most other people prefer commenting it out.

9. Try to avoid string concate[sic] in the code, especially for the logger statements.
This is the first of many in a theme of being concerned about silly micro-optimizations.

24. Always use StringBuffers for string concate[sic]. This is to avoid memory leaks.
The memory leak statement is just wrong. Some people did follow this rule, leading to a bunch of explicit StringBuffer manipulations that would have better been written as "..." + expression + "..." etc.

8. Maintain 80 columns for the code; however, this restriction should not be applied for the logger statements or expressions that lead to the breaking of strings to next line by concatination[sic].

Remember that one exception to the 80 column rule is to never break up a string to fit it within 80 columns (the ide would never do this).
Example:
the following -
logger.finest("Total destinations obtained by the router" +
"for the operator = " + length);

Should Be :
logger.finest("Total destinations obtained by the router for the operator = " + length);

The first causes 3 new strings to be made (and then garbage collected) every time through this routine.

The second causes 2 new strings to be made
This demonstration of ignorance of implicit StringBuffer (or StringBuilder) usage explains the other silly rules.

27. In For Loop statements like
for (int i = 0; i < relatedContentIDs.length; i++)

always cache the Array/ArrayList length/size one time before the For Loop begins and use this cache value in i the For Loop statement.
Example :
int size = relatedContentIDs.length;
for (int i = 0; i < size; i++)

This is for better Memory Management. However, make sure that the collection size is not changed inside the loop because of addition/deletion of the collection element.
More silly micro-optimization obsession. The "better Memory Management" statement is wrong, but maybe it was an ignorant paraphrase of avoiding instruction cache misses. Anyhow, when using a Collection, it would be less error-prone to use an Iterator.

23. Do not use redundant/unecessary[sic] return statements.
I don't even know what this refers to. The Java compiler disallows unreachable code.

36. Do local caching as well as caching of statics.
I don't know what this means, but it sounds wrong.

No comments:

Post a Comment