Java Style Guide

Table of Contents

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.

Indentation and empty space

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.

Commenting

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.

Capitalization

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

Length

  • 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.
  • Keep your methods short. A method shouldn't be longer than 1-2 screens without good reasons.
  • 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.

Author: Dave Musicant

Validate