Official Java coding conventions have evolved from earlier
ones over many years.
They just work!
Nearly all Java code written today follows the
standards and style for coding Java programs,
found at Oracle's (formerly Sun's) website.
You should read this and follow it.
(A more up-to-date and through coding style guide is available at
Google's Java coding standards.)
Your Program n.
A maze of non sequiturs littered with clever tricks
and irrelevant comments.
Compare with My Program
.
My Program n.
A gem of algoristic precision, offering the most sublime
balance between compact, efficient coding on the one hand
and fully commented legibility for posterity on the other.
Compare with Your Program
.
– Stan Kelly-Bootle
- A convention conveys important information concisely.
- Conventions protect against certain types of errors.
(For example using complex expressions using "++"
operator.)
- Convention add predictability to low-level tasks, making
the code easier for others to read.
(For example using for loops for counting type
loops, or the idiom for reading input one character
at a time:
while ((ch=in.read()) != -1)
.)
- Conventions can compensate for missing features in a language.
(For example using the "
enum
" pattern.)
- Large scale (high-level tasks) conventions are sometimes now called
design patterns (in the old days we called them
paradigms).
Using these conventions in standard situations generally makes your code
easier to read and debug, and more efficient.
- It takes less mental effort to use a convension than to re-solve
the problems over and over.
- Many details of coding, such as how many spaces to indent
or how to format a comment, are mostly arbitrary with
many right answers.
How you decide to answer such questions is less important than
consistantly following the same conventions.
- It is possible to become obsessed with conventions, standards, styles, and
guidelines that it becomes difficult to remember them all.