Please follow the style of the existing codebase.
For Java code, Apache Spark follows Oracle's Java code conversions. Many Scala guidelines below also apply to Java.
For Scala code, Apache Spark follows the official Scala style guide, but with the following changes:
Limit lines to 100 characters. The only exceptions are import statements (although even for those, try to keep them under 100 chars).
Use 2-space indentation in general. For function declarations, use 4 space indentation for its parameters when they don't fit in a single line. For example:
Code documentation style
For Scala doc / Java doc comment before classes, objects and methods, use Java docs style instead of Scala docs style.
For inline comment with the code, use // and not /* .. */.
Always import packages using absolute paths (e.g.
scala.util.Random) instead of relative ones (e.g.
In addition, sort imports in the following order (use alphabetical order within each group):
- Third-party libraries (
- Project classes (
You can also use the IntelliJ import organizer plugin can organize imports for you. Use this configuration for the plugin (configured under Preferences / Editor / Code Style / Scala Imports Organizer):
Don't use infix notation for methods that aren't operators. For example, instead of
list map func, use
list.map(func), or instead of
string contains "foo", use
string.contains("foo"). This is to improve familiarity to developers coming from other languages.
Put curly braces even around one-line
else or loop statements. The only exception is if you are using
if/else as an one-line ternary operator.
Always specify the return types of methods where possible. If a method has no return type, specify Unit instead in accordance with the Scala style guide. Return types for variables are not required unless the definition involves huge code blocks with potentially ambiguous return values.
If in Doubt
If you're not sure about the right style for something, try to follow the style of the existing codebase. Look at whether there are other examples in the code that use your feature. Feel free to ask on the dev mailing list as well.