Best practices - 6.1 Funnelback Java style guide, coding conventions and standards
General formatting and naming rules
Indentation
Indent by 4 spaces at a time.
Don’t use tabs or mix tabs and spaces for indentation.
public void myMethod() {
if (isTrue()) {
doSomething();
}
}
public void myMethod() {
if (isTrue()) {
doSomething();
}
}
- Definition
-
Indentation improves readability.
- Pros
-
-
Spaces are displayed identically regardless of the IDE.
-
Makes reading DIFFs easier.
-
- Cons
-
-
Users cannot chose the indent display size in their IDE.
-
Tabs saves on source file size.
-
Error handling
Use exceptions to signal an error
When an error occurs, throw an Exception
rather than returning null
or a special value like -1
.
String[] data = getData();
if (data == null) {
throw new IllegalStateException("No data found");
}
return data.length;
Ideally getData()
should throw an Exception
itself, simplifying the code as we could just use:
return getData().length;
This assumes that everything went all right, but if something is wrong the underlying getData()
call will throw an Exception
with the actual cause for the failure.
String[] data = getData();
if (data == null) {
return -1;
} else {
return data.length
}
- Definition
-
It helps to distinguish actual errors against special return values. Some methods might return
null
on purpose to indicate that no data has been found. Throwing anException
ensures the caller can distinguish between "No data" and an error case.It simplifies code for callers as they don’t have to worry about special values. It might also help to avoid
NullPointerException
and to have a proper stacktrace when something goes wrong.
Class design
Implementation
Input parameters validation
Validate input parameters (on public methods only?)
Investigate the use of @NotNull
. See: [NotNull Annotation](http://stackoverflow.com/questions/4963300/which-notnull-java-annotation-should-i-use)
Magic numbers and strings
Use constants for magic numbers and strings.
Consider using collection.cfg
parameters when the value might be configured by the user.
Reading from config files
Always use a default value fall-back when reading from configuration files.
Always use the method that will return a default values if the value doesn’t exist in he configuration file.
- Definition
-
Using such a pattern guarantees that reading from the config file will always return a value, simplifying the code as you don’t have to check for null values, and preventing
NullPointerExceptions
.
Config c = new NoOptionsConfig(...);
String secModel = c.value(Keys.FileCopy.SECURITY_MODEL, DefaultValues.FileCopy.SECURITY_MODEL_NONE)
int maxFilesStored = c.valueAsInt(Keys.FileCopy.MAX_FILES_STORED, DefaultValues.FileCopy.MAX_FILES_STORED);
Config c = new NoOptionsConfig(...);
// No value check, could be null
String secModel = c.value(Keys.FileCopy.SECURITY_MODEL);
// Unnecessary complex check
String secModel = c.value(Keys.FileCopy.SECURITY_MODEL);
if (secModel == null || "".equals(secModel)) {
secModel = "none";
}
- Pros
-
-
Simplifies code
-
Prevents
NullPointerException
-
- Cons
-
-
Default values in the
DefaultValues
class might get out of sync withcollection.cfg.default
.
-
Collection-types return values
Return copies or immutable version of collections.
This applies only if those collections holds states in your class (i.e. are a field of the class)
Comments
Comments content
Ensure comments contain relevant information.
A comment should contain:
-
Context
-
Expectations
-
Results
-
Unexpected behaviors
-
Limitations
Dead code
Remove commented out code.
Commented code is confusing as people reading the source can’t really know if it’s important or not.
There’s no justification for it:
-
If the code is supposed to be removed, just remove it.
-
If history needs to be kept, SVN can be consulted to retrieve a previous version.
-
If the code is supposed to be deprecated, annotate the method but don’t comment it out.
Deprecated approaches and libraries
This section lists approaches, libraries and classes whose use is now deprecated.
They may still be present in existing code but you should not be using them in new work.
Java serialization
Avoid using Java serialization in favor of a custom format or database to serialize data.
- Pros
-
-
Comes for free
-
- Cons
-
-
Hurts interoperability, serialized data cannot be read outside Java
-
Cause problems during upgrades with different versions of the serialized objects
-
serialVersionUid
needs to be manually maintained
-
ObjectCache
Avoid using the ObjectCache
interface for new development.
The ObjectCache
interface has various legacy problems that cascaded to its implementations, resulting in some parameters not being used or some method not implemented.
Its current design makes assumptions on the storage implementation, making it hard to implement for different storage systems.
- Definition
-
Avoid using
ObjectCache
and its implementations:BTreeCache
,SQLiteCache
,RedisCache
, …
Finally
Be consistent.
If you’re editing code, take a few minutes to look at the code around you and determine its style. If they use spaces around all their arithmetic operators, you should too. If their comments have little boxes of hash marks around them, make your comments have little boxes of hash marks around them too.
The point of having style guidelines is to have a common vocabulary of coding so people can concentrate on what you’re saying rather than on how you’re saying it. We present global style rules here so people know the vocabulary, but local style is also important. If code you add to a file looks drastically different from the existing code around it, it throws readers out of their rhythm when they go to read it. Please try to avoid this.