Naming Conventions
In this lesson we will look at the naming conventions used in Scala.
General Guidelines
Generally speaking, Scala uses “camel case” naming. That is, each word is capitalized, except possibly the first word.
UpperCamelCaselowerCamelCase
Acronyms should be treated as normal words.
// WRONG!XHTMLmaxID
// RIGHT!XhtmlmaxId
Underscores in names (_
) are not actually forbidden by the compiler, but are strongly discouraged as they have special meaning within the Scala syntax. However, there are some exceptions which we will see later in this lesson.
Constants, Values, Variables and Methods
Constant names should be in upper camel case. If the member is final, immutable and it belongs to a package object or an object, it may be considered a constant:
object Container {val MyConstant = ...}
The value: Pi in scala.math package is another example of such a constant.
Method, Value and variable names should be in lower camel case:
val myValue = ...def myMethod = ...var myVariable
Classes/Traits
Classes should be named in upper camel case:
class MyFairLady
Sometimes traits and classes, as well as their members, are used to describe formats, documentation or protocols and generate/derive them. In these cases it is desirable to be close to a 1:1 relation to the output format and the naming conventions don’t apply. In this case, they should only be used for that specific purpose and not throughout the rest of the code.
Objects
Object names are like class names (upper camel case).
An exception is when mimicking a package or function; this isn’t common. Let’s look at an example below.
object ast {sealed trait Exprcase class Plus(e1: Expr, e2: Expr) extends Expr...}object inc {def apply(x: Int): Int = x + 1}
Packages
Scala packages should follow the Java package naming conventions.
// WRONG!package coolness
// RIGHT! Puts only coolness._ in scopepackage com.novell.coolness// RIGHT! Puts both novell._ and coolness._ in scopepackage com.novellpackage coolness// RIGHT! For package object com.novell.coolnesspackage com.novell/*** Provides classes related to coolness*/package object coolness {}
root
It is occasionally necessary to fully-qualify imports using _root_
. For example, if another net
is in scope, then to access net.liftweb
we must write e.g.:
import _root_.net.liftweb._
Do not overuse _root_
. In general, nested package resolves are a good thing and very helpful in reducing import clutter. Using _root_
not only negates their benefit but also introduces extra clutter in and of itself.
Type Parameters (generics)
For simple type parameters, a single upper-case letter (from the English alphabet) should be used, starting with A.
class List[A] {def map[B](f: A => B): List[B] = ...}
If the type parameter has a more specific meaning, a descriptive name should be used, following the class naming conventions (as opposed to an all-uppercase style):
// Rightclass Map[Key, Value] {def get(key: Key): Valuedef put(key: Key, value: Value): Unit}// Wrong; don't use all-capsclass Map[KEY, VALUE] {def get(key: KEY): VALUEdef put(key: KEY, value: VALUE): Unit}
If the scope of the type parameter is small enough, a mnemonic can be used in place of a longer, descriptive name.
class Map[K, V] {def get(key: K): Vdef put(key: K, value: V): Unit}
Annotations
Annotations, such as @volatile
should be in lower camel case:
class cloneable extends StaticAnnotation