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.
Acronyms should be treated as normal words.
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:
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:
Classes/Traits
Classes should be named in upper camel case:
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.
Packages
Scala packages should follow the Java package naming conventions.
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.:
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.
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):
If the scope of the type parameter is small enough, a mnemonic can be used in place of a longer, descriptive name.
Annotations
Annotations, such as @volatile should be in lower camel case: