Code Style
Apache Cassandra follows Sun’s Java coding conventions with the modifications described on this page.
General Conventions
-
Braces on new lines:
{and}are always placed on a new line, unlike the Sun convention -
Line length: Try to keep lines under 120 characters, but use good judgment — slightly exceeding 120 is better than splitting a line with no natural breaking point
-
Indentation: Use 4 spaces, never tabs
-
Trailing whitespace: Clean it up in a separate patch, or leave it alone — avoid mixing whitespace changes with functional changes
Multiline Statements
When splitting inside a method call, use one line per parameter and align:
SSTableWriter writer = new SSTableWriter(cfs.getTempSSTablePath(),
columnFamilies.size(),
StorageService.getPartitioner());
When splitting a ternary, use one line per clause and carry the operator:
var = bar == null
? doFoo()
: doBar();
Exception Handling
-
Never write
catch (…) {}orcatch (…) { logger.error() }just to satisfy compile-time exception checking. Always propagate the exception or throwRuntimeException(orAssertionErrorif it "can’t happen"). -
Avoid propagating checked exceptions that no caller handles. Rethrow as
RuntimeExceptionorIOError. -
logger.warn()is often a cop-out: decide whether the condition is an error or not. -
If you genuinely know an exception indicates an expected condition, you may ignore it — but explain why in a comment.
Boilerplate
-
Avoid redundant
@Overrideannotations when implementing abstract or interface methods -
Do not implement
equalsorhashCodeunless actually needed -
Prefer
public finalfields to private fields with getters -
Prefer constructor initialization to setters
-
Avoid redundant
thisreferences -
Do not extract interfaces unless you need multiple implementations
-
Always include braces for nested levels of conditionals and loops; braces may be omitted for single-level only
Import Ordering
Imports must follow this order:
java
<blank line>
com.google.common
org.apache.commons
org.junit
org.slf4j
<blank line>
everything else alphabetically
Good:
import java.io.IOException;
import java.util.List;
import com.google.common.collect.ImmutableList;
import org.junit.Test;
import org.slf4j.Logger;
import org.apache.cassandra.utils.FBUtilities;
Bad:
import org.apache.cassandra.utils.FBUtilities;
import java.io.IOException;
import org.slf4j.Logger;
import com.google.common.collect.ImmutableList;
IDE Format Files
Code style files are available for both major IDEs:
-
IntelliJ IDEA: Run
ant generate-idea-filesto apply the Cassandra code style automatically -
Eclipse: cassandra-style-eclipse on GitHub
License Compliance
All source files must include the Apache License header. The project enforces this using the Apache RAT (Release Audit Tool).
Use this Java header template:
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
RAT checks the following file types: .bat, .btm, .cql, .css, .g, .html, .iml, .java, .jflex, .jks, .md, .mod, .name, .pom, .py, .sh, .spec, .textile, .yml, .yaml, .xml.
If a file is missing the license header, the build fails with:
Some files have missing or incorrect license information. Check RAT report in build/rat.txt for more details!
Third-party libraries in lib/ must be Apache License compatible and documented in lib/licenses/.