Scala Method Argument: Option of Collection or Default Value -
Scala Method Argument: Option of Collection or Default Value -
i have method takes map of [int, myclass] argument. this:
mymethod(somemap : map[int, myclass])
however, somemap might not nowadays (null in java world , none in scala). of next improve design of method api point of view:
wrapping in option: mymethod(somemap : option[map[int, myclass]] = none)
defining default empty map: mymethod(somemap : map[int, myclass] = maps.empty)
the first alternative looks elegant, has added complexity 1 has wrap map (if not none) in some() , in implementor has getorelse disclose it. first alternative makes clear consumer of api, map object might not exist (none)
in sec option, 1 not have wrapping (in some) or unwrapping, empty container has instantiated every time there no existing map object.
also, arguments agains 1: alternative container of 0 or 1 item , map container (collection). design wrap container in container ?
from api design point of view, 1 improve approach?
the right question is: create sense mymethod
work option
?
from point of view of mymethod
, maybe only works map
in case responsability of caller not phone call mymethod
if there no map work with.
on other hand, maybe mymethod
special if there no map
or if empty , responsability of method handle case.
so there no right reply right argument 1 responsabilities of methods respected. aim have high cohesion , low coupling between functions , classes.
scala scala-collections scala-option
Comments
Post a Comment