We mainly follow the standard Java coding conventions and most of the conventions from the books Effective Java [1] and Clean Code [4]. Some exceptions and additional rules are listed below. Each rule starts with DO, CONSIDER, AVOID or DO NOT, according to [2].
See Build.md. For the formatting rules, check java-common.gradle.kts
- DO encode files in UTF-8. DO NOT use any other format.
- DO use the generally accepted naming and source code formatting conventions of Java (Item 56 of [1], Chapter 1 of [5]). If you are developing in IntelliJ Idea the built in formatting by the project can be used (see Development.md for more information).
- DO start project names with the prefix
hu.bme.mit.theta
. - DO use CamelCase for class names containing subsequent capital letters, except when the whole name is a sequence of capital letters. Examples:
CFA
,CfaEdge
,OsHelper
. - CONSIDER using abbreviations for well known and common names. Examples:
Expression
->Expr
,Statement
->Stmt
,Counterexample
->Cex
.
- CONSIDER static factory methods instead of constructors (Item 1 of [1]). This is especially useful for type inference of generic classes.
- CONSIDER making classes immutable if possible (Item 15 of [1]). If initialization of an immutable class seems to be difficult, consider using a builder.
- CONSIDER making classes final if they are not designed to be inherited from (Item 17 of [1]).
- AVOID unused modifiers, for example methods of interfaces are automatically public.
- CONSIDER using the initialization-on-demand holder idiom [3] for lazy loaded, thread safe singletons.
- CONSIDER using the weakest types on interfaces. For example, instead of
ArrayList
, useList
,Collection
orIterable
if possible.
- DO use JUnit for unit tests.
- DO end the name of each unit test class with
Test
. This way, build tools will run your tests automatically. - CONSIDER using parametrized unit tests where possible.
- CONSIDER using static analysis tools like FindBugs, SonarQube, PMD.
- CONSIDER using
com.google.common.base.Preconditions
methods to check if the preconditions of a method holds. For example: null checks, bound checks, etc. Consider filling theerrorMessage
parameter of these functions with a short error message.
- DO NOT concatenate strings with the
+
operator. ConsiderStringBuilder
,StringJoiner
,String.format()
instead. - AVOID platform specific constructs. For example, prefer
System.lineSeparator()
over\r\n
or\n
. - CONSIDER using
getClass().getSimpleName()
to print the name of a class in itstoString()
. This way, if the class is renamed,toString()
will also adapt.
- Joshua Bloch: Effective Java (2nd edition)
- Krzysztof Cwalina, Brad Abrams: Framework Design Guidelines (2nd edition)
- https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom
- Robert C. Martin: Clean Code: A Handbook of Agile Software Craftsmanship
- Robert Liguori, Patricia Liguori: Java 8 Pocket Guide