Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 606
nnythm Avatar asked Aug 09 '12 15:08

nnythm


2 Answers

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.

like image 52
Travis Brown Avatar answered Sep 17 '22 20:09

Travis Brown


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.

like image 42
Tal Pressman Avatar answered Sep 21 '22 20:09

Tal Pressman