DEV Community

Cover image for Java Coding Best Practices - Coding Conventions - Style Guide
Amit Chaurasia
Amit Chaurasia

Posted on

Java Coding Best Practices - Coding Conventions - Style Guide

  1. It is a good practice to declare a variable close to the point of its possible use. This not only enhances readability of the code but also makes debugging simpler.
  2. Do not use the same name for local variables as the class member variables.
  3. If possible keep method parameters final.
  4. Keep in mind Boxing and Unboxing of primitive data types.
  5. Use StringBuffer and/or StringBuilder where needed instead of just using String.
  6. Use Proper Naming Conventions: Follow naming conventions for classes, interfaces, variables, constants, methods, annotations, packages, enums.
  7. One should always use classes, methods, interfaces, enums, and annotations from the Java standard library as much as possible instead of blindly using the third-party libraries or frameworks.
  8. If possible design your classes to be immutable. This saves you from any trouble from concurrent modifications.
  9. Include unit testing as part of your development/coding. Follow TDD, BDD approach.
  10. Don't make any instance or class variable public without good reason.
  11. Numerical constants (literals) should not be coded directly, except for -1, 0, and 1, which can appear in a for loop as counter values.
  12. Avoid using an object to access a class (static) variable or method. Use a class name instead.
  13. It is generally a good idea to use parentheses liberally in expressions involving mixed operators to avoid operator precedence problems.
  14. Special Comments: Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken.
  15. Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).
  16. Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.
  17. One blank line should always be used in between the local variables in a method and its first statement.
  18. Comment your code.
  19. Use Underscores in lengthy Numeric Literals.
  20. Use Enums where possible. private enum Answer { YES { @Override public String toString() { return "yes"; } },


  1. Use Type-use annotations. Example - final @Nullable String name;
  2. Numeric Literals: long-valued integer literals use an uppercase L suffix, never lowercase (to avoid confusion with the digit 1). For example, 3000000000L rather than 3000000000l
  3. Use of optional braces: Braces are used with if, else, for, do and while statements, even when the body is empty or contains only a single statement. Other optional braces, such as those in a lambda expression, remain optional.
  4. No wildcard imports: Wildcard imports, static or otherwise, are not used.
  5. Every class should be preceded with a descriptive comment using the "JavaDoc" notational convention.

    • Stores the first, middle, and last names for a president. */ class President { //code... }
  6. Open braces (i.e. "{") do not start a new line.
    Close braces (i.e. "}") do start a new line, and are indented with the code they close.
    for (int i=0; i < args.length; i = i + 1) {
    vals.insertElementAt(new Float (args[i]), i);
    // Transmogrify is incremental and more efficient inside the loop.

  7. Lines of code should be kept short, generally less than 80 or 100 characters wide.

  8. Documentation Comments?

  9. Never leave a Catch Blocks empty.

  10. Use enhanced for loops instead of for loops with counter.

  11. Return Empty Collections instead of returning Null elements.


Top comments (0)