Many beginners to Java repeat the same basic style errors. Such style errors don't make your program incorrect, but they make your program less maintainable. In essence, many beginners write code that suffers from the same basic underlying defect of not being written with compassion for the reader.
There are in fact two target platforms for all code: the runtime hardware, and the human cerebral cortex. All code 'runs' in the human brain, in the sense that all code needs to be understood by a human being. Coding style is always concerned with this second target platform, not with the first.
Some common basic style errors:
- classes too long
- methods too long
- little or no javadoc
- no convention for distinguishing between local variables, arguments, and fields
- many empty catch blocks that suppress exceptions
- inappropriate use of multiple return statements
- using exceptions to define regular program flow
- excessive use of the instanceof operator
- using floating point data to represent money
- preferring arrays over collections (especially in JDK 5+)
- not ordering class members by scope
Some basic style errors are much more damaging to your code than others.
The single most harmful bad habit is that of excessive length.
Classes that are too long are hard to understand.
As a guideline, if a class is more than about 300 lines long, you should likely consider splitting it up into smaller pieces.
Similarly, methods that are more than a single screen are very likely too long, and would almost always benefit from being split into several methods.
It's not uncommon for a useful method to consist only of a single line of code.
Exceptions and control flow
Beware of instanceof operator
Prefer Collections over older classes
Use javadoc liberally
Multiple return statements
Separate public and private members
Beware of floating point numbers