Should I use an empty list or make it an option?

Often my methods have a parameter Listthat is optional. When I get a list of elements, I do something with them, otherwise this parameter is ignored. Here is 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 supports waiting for a parameter appenderswhen it really is optional. On the other hand, creating appendersa Option[List]adds complexity.

Is it a bad practice to avoid using Optionwhen the parameter is Listand can I just check the void instead None?

+4
source share
1 answer

( ), None, .

, , None List() -.

, , , no-op.

+8

All Articles