alexandru / scala-best-practices

A collection of Scala best practices
4.39k stars 623 forks source link

Avoid type inference for public methods' return type #6

Closed padurean closed 10 years ago

padurean commented 10 years ago

At least for public methods, prefer this:

def someFunction: SomeType = ...

to this:

def someFunction = ...

in order to avoid:

  1. the need to rely on IDE or to inspect implementation in order to understand what is the actual return type
  2. unintended actual return type due to some implementation bug. E.g. one logic branch returns SomeType, another logic branch returns SomeOtherType => actual return type may be AnyRef, while SomeType was actually intended. If return type is specified explicitly, such a situation would result in a compilation error, otherwise not.
alexandru commented 10 years ago

Added Rule 2.16