Questions:
• What are the current Scala code styles?
• Are we respecting them? An analysis of Scala Open Source projects using Codacy
• What might become best practices/standard?
use relative imports from other packages • Always use braces for ifs except for ternary operator behavior • Do not use infix notation if method is not an operator (map, filter, flatMap)
Use vals when possible. • Use private when possible for member variables. • Use named arguments when passing in literal values • Options should be used instead of null whenever possible • Prefer case classes over tuples
contains(b) • .filter(x => ).head can be replaced with find(x => ) match { .. } • .filter(x =>).headOption can be replaced with find(x => ) • .filter(x => Bool).isEmpty can be replaced with !exists(x => Bool) • .filter(_.isDefined).map(_.get) can be replaced with flatten • .filter(x => Bool).size can be replaced more concisely with with count(x => Bool) • sort.filter can be replaced with filter.sort for performance • !Traversable.isEmpty can be replaced with Traversable.nonEmpty • !Traversable.nonEmpty can be replaced with Traversable.isEmpty From Scapegoat
• The use of traits are highly encouraged • Do not use Exceptions for commonplace errors • Encode commonplace errors explicitly: using Option or (scala.util./com.twitter.util.)Try
whenever possible • Do not overuse Option: if there is a sensible default — a Null Object — use that instead. • Don’t use pattern matching for conditional execution • Only use call-by-name for creating new control constructs such as DSLs • Prefer case classes over tuples (specially no ._1)