Java Programming Style Guide
Good programming style makes your code much more readable and
maintainable. Here are some basic style directives that I expect you
to follow in your code.
- Indent well and use consistent and clear alignment of braces.
- Use empty space when appropriate. Separate related sections
of code from unrelated sections of code by the use of blank
lines.
- Comment sections of code that need it. You should
use comments to describe a particular twist that might be hard to
understand, or to explain what a section of code is doing. If your
program has no explanatory comments, you're assuming your readers
are more capable than they are.
- Break up lines that are longer than 80
characters. This makes your code much easier to read when printed
and when listed in a standard terminal window.
- Use capitalization and descriptive names to make your
identifiers clear.
- Variable, function, and method, and file names should
start with a lower case letter. Capital letters or underscores
should be used to separate multiple words.
Examples: name, age, personName (or person_name), getAge (or
get_age). Whether you choose capital letters or underscores to
separate words, stay consistent.
- Class names should start with a capital letter. Capital letters
should be used to separate multiple words.
Examples: class Person, class Gradebook, class BlackjackGame
- Keep your methods short. A
method shouldn't be longer than 1-2 screens without good reasons.
- Each class header should contain comments containing
- Name of class
- Author(s)
- Description of purpose
- Known bugs
- Each method should contain header comments containing
- Name of method
- Author(s) (if not all are same as for class)
- Description of purpose
- Description of parameters and return values
- Citations indicating ideas or code borrowed from
other people / sources
- Your code should be direct and minimal to get the job done. Don't use five variables and three loops when three variables and two loops will get the job done.
This content is a combination of commonly known programming style
guidelines, ideas that have been discussed here in our department,
and Google's
Java style guide.