Effective Scala - Best Practices from Twitter
Scala is one of the primary programming languages used at Twitter, and most of the Twitter infrastructure is written in Scala. The Effective Scala guide is a series of short essays, a set of "best practices" learned from using Scala inside Twitter. Twitter's use of Scala is mainly for creating high volume, distributed systems, though most of the guide should be applicable to other domains.
The core ideas in the document are:
- After correctness, think always of the reader. Always favor readability.
- Write generic code but not at the expensive of clarity.
- Take advantage of simple language features that afford great power but avoid the esoteric ones.
- Program in Scala, not in Java, nor Haskell, nor Python
- A sophisticated language requires a complex implementation, and complexity begets complexity. Thus complexity is the tax of sophistication - you must always ensure that its utility exceeds its cost.
Here are the different topics of the Effective Scala guide, taken from the table of contents.
- Formatting: Whitespace, Naming, Imports, Braces, Pattern matching, Comments
- Types and Generics: Return type annotations, Variance, Type aliases, Implicits
- Collections: Hierarchy, Use, Style, Performance, Java Collections
- Concurrency: Futures, Collections
- Control structures: Recursion, Returns, for loops and comprehensions, require and assert
- Functional programming: Case classes as algebraic data types, Options, Pattern matching, Partial functions, Destructuring bindings, Lazyness, Call by name, flatMap
- Object oriented programming: Dependency injection, Traits, Visibility, Structural typing
- Garbage collection
- Java compatibility
The guide assumes you are already familiar with Scala. For users new to Scala, Twitter has made available the written materials for their Scala School, a series of lectures to help introduce Twitter programmers to Scala. The official Learning Scala page is also available from the scala-lang.org website. Another good introduction is Learning Scala in small bites, a series of small examples, each of which highlights one or two aspects of Scala.
Effective Scala is a living document that will continually change to reflect Twitter current "best practices". Users can help improve the guide by creating an issue at the Effective Scala issues page.
Martin Thompson Jul 27, 2014