Scala Best Practices by Loïc Knuchel

(meaning their are my personal ones, not community ones)

I believe there is 3 very different groups in the Scala community:

  • OOP puristes: coming from OOP languages and use Scala as a better OOP language
  • FP minimalists: sold to FP but want to keep code
  • FP purists:

Codes used in this guide:

  • [MUST] is for rules that should be transgressed only in last resort and needs an explanation of why in comment next to it
  • [PREFER] is for rules that should be followed in most cases but can be sometimes transgressed for more practical code
  • Rules in bold are the important ones I consider you essential to any Scala code

Table of contents

  1. Preface
    • [MUST] Do not take theses rules as is, you should understand and adapt them to your context
  2. General
    • [PREFER] Code comments should explain WHY the code has been done this way, not HOW it does it
    • [MUST] Names should be meaningful
  3. Language
  4. FP
    • [MUST] Functions should be total
    • [PREFER] Functions should be pure
  5. Architecture
  6. Performance
    • [MUST] Performance should not be discussed without a benchmark highlighting the problem and the metrics that must be reached
  7. Play framework
  8. Syntax
    • [PREFER] Functions should have less than 20 lines, less than 10 is better, more than 50 is not acceptable
    • [PREFER] Lines should be less than 120 chars, more than 160 is not acceptable
    • [PREFER] Indentation should use 2 spaces
    • [PREFER] Each file should be terminated by an empty line

Interesting tools

  • scalafmt
  • scalastyle
  • wartRemover

References and inspirations

Contribute

Open a PR or an Issue and let's discuss