SC

Scala Defaults

Core Scala coding conventions for idiomatic, type-safe functional and object-oriented code

Details

Language / Topic
scalaScala
Category
Style Guide

Rules

balanced
- Prefer `val` over `var` for immutability by default; only use `var` when mutation is explicitly required.
- Use `case class` for value objects and data carriers — they provide `equals`, `hashCode`, `copy`, and pattern matching for free.
- Favour expression-oriented style: every `if`, `match`, and `try` block should return a value rather than performing side effects inline.
- Name methods with `camelCase`, types and objects with `PascalCase`, constants with `UPPER_SNAKE_CASE` or `camelCase` in companion objects.
- Use `Option[A]` instead of `null` for optional values — chain with `.map`, `.flatMap`, `.getOrElse`, never `.get`.
- Limit the scope of `implicit` definitions by placing them in companion objects or dedicated `Implicits` objects — never in top-level package objects.
- Keep functions pure where possible: a function that takes the same input should always return the same output with no observable side effects.
- Use `sealed trait` with `case class`/`case object` for ADTs — the compiler will warn on non-exhaustive `match` expressions.