I wanted to check out some of the best Scala programming practices since I am new to Scala. I read online about how Scala usually does not use exceptions, except for “exceptional” circumstances (which do not include parameter checking). Right now in my project I use a lot of require , so I wonder what would be the best way to type check.
For example, if I have a class
class Foo(String bar){ require(StringUtils.isNotEmpty(bar), "bar can't be empty") }
What are my alternatives to verifying? Create such a companion object
Object Foo { def apply(bar: String) = Try[Foo] { bar match = { case null => Failure("can't be null")
Or should I use Option instead?
Also, for Scala methods, how can I check a method parameter? If I already return a parameter, am I just returning an empty parameter, if I get a bad parameter? Doesn’t this mean that I should check for an empty parameter when I use method return and do not throw an exception for a more specific message? (for example, runtime exception cannot use zeros).
scala
jstnchng
source share