Coding Standards

General Code Conventions

  • Code should be readable
  • Code should be beautiful
  • Do not use deprecated APIs
  • Do not create public constructors unless absolutely necessary. Provide static factory methods with descriptive names for object creation instead
  • Make classes immutable if possible
  • Only expose fields/methods if necessary
  • Suppress compiler warnings
  • The ternary operator is cool

Naming Conventions

  • Names for variables, classes, etc should be as short as possible while also being descriptive and unambiguous. Think hard about what you're writing and how it can be distinctly labeled using one or two words
  • Never prepend the letter "I" to an interface merely to indicate that is an interface. Instead name the thing what it is (i.e. use Record instead of IRecord)
  • Never append the term "Impl" to a concrete class merely to indicate that it is the implementation of an abstract class or interface. Instead choose a sensible name that describes the nature of the implementation
  • Try to use adjectives for Interface names, if possible

Documentation

  • Javadoc everything. This includes private methods
  • Write comments for any important but non-obvious semantics
  • Use standard annotations where appropriate (i.e. @Override, @ThreadSafe, etc)

Exceptions

  • Do not merely catch exceptions and log them. Always propagate them as a RuntimeException
  • It is okay to ignore exceptions that are genuinely expected/accepted conditions, but this must be documented with a comment
  • Do not define custom exceptions unless you need to

Unit Tests

Block Statements

  • Always use braces
  • Only one brace per line