I hava a project on github that is analysed by codacy . The analysis suggest to "Avoid using null" for the following line of code:
def doSomethingWithPath(path:Path) = {
require(path != null, "Path should not be null") //<-to avoid
...
}
What is the simplest scala idiomatic thing to do to fix it?
As a word of caution (and balance), the Twitter Effective Scala page recommends not overusing Option , and using the Null Object Pattern where it makes sense. As usual, use your own judgment, but try to eliminate all null values using one of these approaches.
Ignoring the possibility of null might be fine in a well built code base where it shouldn't come up anyway and null would be a bug, but a simple guard to catch it could prevent more subtle bugs from showing up in case something went wrong and null actually happens.
In Scala, using null to represent nullable or missing values is an anti-pattern: use the type Option instead. The type Option ensures that you deal with both the presence and the absence of an element. Thanks to the Option type, you can make your system safer by avoiding nasty NullPointerException s at runtime.
Nothing - at the bottom of the Scala type hierarchy. Null is the type of the null literal. It is a subtype of every type except those of value classes. Value classes are subclasses of AnyVal, which includes primitive types such as Int, Boolean, and user-defined value classes.
I would keep it as is. You aren't really using null here, just guarding against it. The alternative could be to just delete that line altogether, and decide to not handle the null at all. Ignoring the possibility of null might be fine in a well built code base where it shouldn't come up anyway and null would be a bug, but a simple guard to catch it could prevent more subtle bugs from showing up in case something went wrong and null actually happens.
If path
might actually be null
this is probably the simplest.
require(Option(path).isDefined, "Must have a real Path")
the most idiomatic way would be to avoid the require (not sure but I have the idea it can thrown an exception - something Scala heavily recommends against)
def doSomethingWithPath(path:Path): Option[stuff] = { // will return an option of the type you were returning previously.
Option(path).map { notNullPath =>
...
}
}
Now the possible null
case will be returned to the caller, that can/will propagate the returned Option until the layer that knows how to handle it properly.
Note: it is possible that the best place to handle the null case is inside you function. In that case you should do something like
Option(path).map { notNullPath =>
...
}.getOrElse(/* take care of null case */)
If you do want to keep the require
, then the answer by jwvh would be my choice also
There's no need to explicitly check for null or wrap path
in an Option
.
You can do this:
path match {
case p: String => Option(doSomethingWith(p))
case _ => None //if path is null this will be returned
}
That will return an Option
, which may not be what you want, but in that case instead of producing a None
, raise an exception. require
will raise an exception in your example anyway so if that's what your caller expects just do it explicitly.
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