Often my methods have a List
parameter that is optional. When I receive a list of items I do something with them, otherwise that parameter is ignored. Here's a trivial example.
scala> def convertToUpper(s: String, appenders: List[String] = List()) {
(s.toUpperCase :: appenders).mkString(" ")
}
scala> convertToUpper("cory", List("asks", "questions"))
CORY asks questions
But sometimes I wonder if this contract communicates an expectation of a appenders
parameter when really it is optional. On the other hand, making appenders
an Option[List]
adds complexity.
Is it bad practice to avoid use of Option
when the parameter is a List
and I can just test for emptiness instead of None
?
If an empty list is a valid argument (which it is in your example) and it behaves as None
would, then I would recommend not wrapping the list in an option.
I'd say that wrapping it in an option signals that None
and List()
would be treated differently.
I think it's fairly common for methods that accept lists as argument to behave as a no-op.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With