@@ -6,27 +6,31 @@ partof: style-guide
66num : 3
77---
88
9- Generally speaking, Scala uses "camelCase" naming conventions. That is,
10- each word (except possibly the first) is delimited by capitalizing its
11- first letter. Underscores (` _ ` ) are * heavily* discouraged as they have
12- special meaning within the Scala syntax. Please note that there are a
13- few important exceptions to this guideline (as given below).
9+ Generally speaking, Scala uses "camel case" naming. That is,
10+ each word is capitalized, except possibly the first word:
11+
12+ UpperCamelCase
13+ lowerCamelCase
14+
15+ Underscores in names (` _ ` ) are not actually forbidden by the
16+ compiler, but are strongly discouraged as they have
17+ special meaning within the Scala syntax. (But see below
18+ for exceptions.)
1419
1520## Classes/Traits
1621
17- Classes should be named in the camelCase style with the very first
18- letter of the name capitalized:
22+ Classes should be named in upper camel case:
1923
2024 class MyFairLady
2125
2226This mimics the Java naming convention for classes.
2327
2428## Objects
2529
26- Objects follow the class naming convention (camelCase with a capital
27- first letter) except when attempting to mimic a package or a function.
28- These situations don't happen often, but can be expected in general
29- development. :
30+ Object names are like class names (upper camel case).
31+
32+ An exception is when mimicking a package or function.
33+ This isn't common. Example :
3034
3135 object ast {
3236 sealed trait Expr
@@ -39,19 +43,20 @@ development.:
3943 def apply(x: Int): Int = x + 1
4044 }
4145
42- In * all* other cases, objects should be named according to the class
43- naming convention.
44-
4546## Packages
4647
4748Scala packages should follow the Java package naming conventions:
4849
4950 // wrong!
5051 package coolness
5152
52- // right!
53+ // right! puts only coolness._ in scope
5354 package com.novell.coolness
5455
56+ // right! puts both novell._ and coolness._ in scope
57+ package com.novell
58+ package coolness
59+
5560 // right, for package object com.novell.coolness
5661 package com.novell
5762 /**
@@ -60,34 +65,26 @@ Scala packages should follow the Java package naming conventions:
6065 package object coolness {
6166 }
6267
63- ### Versions Prior to 2.8
64-
65- Scala 2.8 changes how packages worked. For 2.7 and earlier, please note
66- that this convention does occasionally lead to problems when combined
67- with Scala's nested packages feature. For example:
68+ ### _ root_
6869
69- import net.liftweb._
70-
71- This import will actually fail to resolve in some contexts as the ` net `
72- package may refer to the ` java.net ` package (or similar). To compensate
73- for this, it is often necessary to fully-qualify imports using the
74- ` _root_ ` directive, overriding any nested package resolves:
70+ It is occasionally necessary to fully-qualify imports using
71+ ` _root_ ` . For example if another ` net ` is in scope, then
72+ to access ` net.liftweb ` we must write e.g.:
7573
7674 import _root_.net.liftweb._
7775
78- Do not overuse this directive . In general, nested package resolves are a
76+ Do not overuse ` _root_ ` . In general, nested package resolves are a
7977good thing and very helpful in reducing import clutter. Using ` _root_ `
8078not only negates their benefit, but also introduces extra clutter in and
8179of itself.
8280
8381## Methods
8482
85- Textual (alphabetic) names for methods should be in the camelCase style
86- with the first letter lower-case:
83+ Textual (alphabetic) names for methods should be in lower camel case:
8784
8885 def myFairMethod = ...
8986
90- This section is not a comprehensive guide to idiomatic methods in Scala.
87+ This section is not a comprehensive guide to idiomatic method naming in Scala.
9188Further information may be found in the method invocation section.
9289
9390### Accessors/Mutators
@@ -128,7 +125,7 @@ conventions are used:
128125 foo.isBaz // boolean property
129126
130127
131- Quite unfortunately , these conventions fall afoul of the Java convention
128+ Unfortunately , these conventions fall afoul of the Java convention
132129to name the private fields encapsulated by accessors and mutators
133130according to the property they represent. For example:
134131
@@ -326,13 +323,12 @@ the reader that `M[_]` is the type of the Monad.
326323
327324## Annotations
328325
329- Annotations, such as ` @volatile ` should be in camel-case, with the first
330- letter being lower case:
326+ Annotations, such as ` @volatile ` should be in lower camel case:
331327
332328 class cloneable extends StaticAnnotation
333329
334330This convention is used throughout the Scala library, even though it is
335- not consistent with Java annotations .
331+ not consistent with Java annotation naming .
336332
337333Note: This convention applied even when using type aliases on
338334annotations. For example, when using JDBC:
@@ -344,14 +340,14 @@ annotations. For example, when using JDBC:
344340## Special Note on Brevity
345341
346342Because of Scala's roots in the functional languages, it is quite normal
347- for local field names to be extremely brief :
343+ for local names to be very short :
348344
349345 def add(a: Int, b: Int) = a + b
350346
351- While this would be bad practice in languages like Java, it is * good*
347+ This would be bad practice in languages like Java, but it is * good*
352348practice in Scala. This convention works because properly-written Scala
353349methods are quite short, only spanning a single expression and rarely
354- going beyond a few lines. Very few local fields are ever used (including
350+ going beyond a few lines. Few local names are used (including
355351parameters), and so there is no need to contrive long, descriptive
356352names. This convention substantially improves the brevity of most Scala
357353sources. This in turn improves readability, as most expressions fit in
@@ -362,4 +358,3 @@ local fields for very simply classes); everything in the public
362358interface should be descriptive. Also note that the names of arguments
363359are now part of the public API of a class, since users can use named
364360parameters in method calls.
365-
0 commit comments