You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 14 Next »

Please follow the style of the existing codebase. Apache Spark follows the official Scala style guide, but with the following changes:

Line Length

Limit lines to 100 characters. The only exceptions are import statements (although even for those, try to keep them under 100 chars).

Indentation

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:

// Correct:
if (true) {
  println("Wow!")
}

// Wrong:
if (true) {
    println("Wow!")
}

// Correct:
def newAPIHadoopFile[K, V, F <: NewInputFormat[K, V]](
    path: String,
    fClass: Class[F],
    kClass: Class[K],
    vClass: Class[V],
    conf: Configuration = hadoopConfiguration): RDD[(K, V)] = {
  // function body
}

// Wrong 
def newAPIHadoopFile[K, V, F <: NewInputFormat[K, V]](
  path: String,
  fClass: Class[F],
  kClass: Class[K],
  vClass: Class[V],
  conf: Configuration = hadoopConfiguration): RDD[(K, V)] = {
  // function body
}

Code documentation style

Use Java docs style instead of Scala docs style.

Imports

Always import packages using absolute paths (e.g. scala.util.Random) instead of relative ones (e.g. util.Random).

In addition, sort imports in the following order:

  • java.* and javax.*
  • scala.*
  • Third-party libraries (org.*, com.*, etc)
  • Project classes (org.apache.spark.*)

Infix Methods

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.

Curly Braces

Put curly braces even around one-line if, else or loop statements. The only exception is if you are using if/else as an one-line ternary operator.

// Correct:
if (true) {
  println("Wow!")
}

// Correct:
if (true) statement1 else statement2

// Wrong:
if (true)
  println("Wow!")

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.

  • No labels