Java Style Guide

These are some points taken from the Google Java Style Guide that make sense in MPS.

  • Fall-through: commented: Write a comment why a fall-through is used in a switch statement.
  • Default case is present: Always write a default case for switch statements.
  • Rules common to all identifiers: Identifiers use only ASCII letters and digits, and, in a small number of cases noted below, underscores. Special prefixes or suffixes are not used. For example, these names do not adhere to the style: name_, mName, s_name and kName.
  • Package names: Package names are all lowercase, with consecutive words simply concatenated together (no underscores). For example, com.example.deepspace, not com.example.deepSpace or com.example.deep_space
  • Class names: are written in UpperCamelCase. Class names are typically nouns or noun phrases. For example, Character or ImmutableList. Interface names may also be nouns or noun phrases (for example, List), but may sometimes be adjectives or adjective phrases instead (for example, Readable). Test classes are named starting with the name of the class they are testing, and ending with Test. For example, HashTest or HashIntegrationTest.
  • Method names: are written in lowerCamelCase. Method names are typically verbs or verb phrases. For example, sendMessage or stop. Underscores may appear in JUnit test method names to separate logical components of the name, with each component written in lowerCamelCase. One typical pattern is _, for example pop_emptyStack. There is no one correct Way to name test methods.
  • Constant names: Use all uppercase letters, with each word separated from the next by a single underscore
  • Non-constant field names: Non-constant field names (static or otherwise) are written in lowerCamelCase. These names are typically nouns or noun phrases. For example, computedValues or index.
  • Parameter names are written in lowerCamelCase. One-character parameter names in public methods should be avoided.
  • Local variable names are written in lowerCamelCase. Even when final and immutable, local variables are not considered to be constants, and should not be styled as constants.
  • Type variable names: Each type variable is named in one of two styles: A single capital letter, optionally followed by a single numeral (such as E, T, X, T2) or a name in the form used for classes, followed by the capital letter T (examples: RequestT, FooBarT).
  • Camel case: defined: tutorial on how to write camel case
  • Don't ignore caught exceptions (see also: Avoid empty catch blocks)

Additional java practises can be found at javapractices.com.


Last update: November 10, 2021

Comments

Back to top