nnythm
nnythm

Reputation: 3320

Scala naming convention for options

When I return something of type Option, it seems useful to explain in the name of the function name that it is an option, not the thing itself. For example, seqs have reduceOption. Is there a standard naming convention? Things I have seen:

maybeFunctionName

functionNameOption

- neither seems to be all that great.

Upvotes: 11

Views: 3722

Answers (2)

Tal Pressman
Tal Pressman

Reputation: 7317

Why would you want to make your function names longer? It doesn't contribute anything, as the fact that it returns an Option is obvious when looking at the function's type.

reduceOption is sort of a special case, since in most cases you really want to use reduce, except that it doesn't work on empty sequences.

Upvotes: 8

Travis Brown
Travis Brown

Reputation: 139038

reduceOption and friends (headOption, etc.) are only named that way to distinguish them from their unsafe alternatives (which arguably shouldn't exist in the first place—i.e, there should just be a head that returns an Option[A]).

whateverOption isn't the usual practice in the standard library (or most other Scala libraries that I'm aware of), and in general you shouldn't need or want to use this kind of Hungarian notation in Scala.

Upvotes: 16

Related Questions