Skip to content

Commit 864e14f

Browse files
som-snyttSethTisue
authored andcommitted
Reduce misleading verbiage on file style
1 parent e42fca5 commit 864e14f

File tree

1 file changed

+27
-42
lines changed

1 file changed

+27
-42
lines changed

_style/files.md

+27-42
Original file line numberDiff line numberDiff line change
@@ -11,60 +11,45 @@ previous-page: method-invocation
1111
next-page: scaladoc
1212
---
1313

14-
As a rule, files should contain a *single* logical compilation unit. By
15-
"logical" I mean a class, trait or object. One exception to this
16-
guideline is for classes or traits which have companion objects.
17-
Companion objects should be grouped with their corresponding class or
18-
trait in the same file. These files should be named according to the
19-
class, trait or object they contain:
14+
The unit of work for the compiler is a "compilation unit",
15+
which is usually just an ordinary file.
16+
The Scala language places few restrictions on how code is organized across files.
17+
The definition of a class, or equivalently a trait or object, can't be split over multiple files,
18+
so it must be contained within a single file.
19+
A class and its companion object must be defined together in the same file.
20+
A sealed class can be extended only in the same file, so all its subclasses must be defined there.
2021

21-
package com.novell.coolness
22+
Similarly, there are no restrictions on the name of a source file or where it is located in the file system,
23+
although certain conventions are broadly honored in practice.
24+
Generally, the file is named after the class it contains,
25+
or if it has more than one class, the parent class.
26+
27+
For example, a file, `Inbox.scala`, is expected to contain `Inbox` and its companion:
28+
29+
package org.coolness
2230

2331
class Inbox { ... }
2432

2533
// companion object
2634
object Inbox { ... }
2735

28-
These compilation units should be placed within a file named
29-
`Inbox.scala` within the `com/novell/coolness` directory. In short, the
30-
Java file naming and positioning conventions should be preferred,
31-
despite the fact that Scala allows for greater flexibility in this
32-
regard.
33-
34-
## Multi-Unit Files
36+
The file may be located in a directory, `org/coolness`, following Java tooling conventions,
37+
but this is at the discretion of the developer and for their convenience.
3538

36-
Despite what was said above, there are some important situations which
37-
warrant the inclusion of multiple compilation units within a single
38-
file. One common example is that of a sealed trait and several
39-
sub-classes (often emulating the ADT language feature available in
40-
functional languages):
39+
It is natural to put the following `Option` ADT in a file, `Option.scala`:
4140

4241
sealed trait Option[+A]
4342

4443
case class Some[A](a: A) extends Option[A]
4544

4645
case object None extends Option[Nothing]
4746

48-
Because of the nature of sealed superclasses (and traits), all subtypes
49-
*must* be included in the same file. Thus, such a situation definitely
50-
qualifies as an instance where the preference for single-unit files
51-
should be ignored.
52-
53-
Another case is when multiple classes logically form a single, cohesive
54-
group, sharing concepts to the point where maintenance is greatly served
55-
by containing them within a single file. These situations are harder to
56-
predict than the aforementioned sealed supertype exception. Generally
57-
speaking, if it is *easier* to perform long-term maintenance and
58-
development on several units in a single file rather than spread across
59-
multiple, then such an organizational strategy should be preferred for
60-
these classes. However, keep in mind that when multiple units are
61-
contained within a single file, it is often more difficult to find
62-
specific units when it comes time to make changes.
63-
64-
**All multi-unit files should be given camelCase names with a lower-case
65-
first letter.** This is a very important convention. It differentiates
66-
multi- from single-unit files, greatly easing the process of finding
67-
declarations. These filenames may be based upon a significant type which
68-
they contain (e.g. `option.scala` for the example above), or may be
69-
descriptive of the logical property shared by all units within (e.g.
70-
`ast.scala`).
47+
The related elements, `Some` and `None`, are easily discoverable, even in the absence of tooling.
48+
49+
When unrelated classes are grouped together, perhaps because they implement a feature or a model a domain,
50+
the source file receives a descriptive `camelCase` name.
51+
Some prefer this naming scheme for top-level terms. For example, `object project` would be found in `project.scala`.
52+
Similarly, a package object defined as `package object model` is located in `package.scala` in the `model` source directory.
53+
54+
Files created just for quick testing can have arbitrary names, such as `demo-bug.scala`.
55+

0 commit comments

Comments
 (0)