Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala avoid using null

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?

like image 814
raisercostin Avatar asked Apr 17 '17 08:04

raisercostin


People also ask

Is it good practice to use null in Scala?

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.

Why should null be avoided in Scala?

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.

How do you handle null values in Scala?

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.

Is there null in Scala?

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.


4 Answers

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.

like image 71
puhlen Avatar answered Dec 09 '22 01:12

puhlen


If path might actually be null this is probably the simplest.

require(Option(path).isDefined, "Must have a real Path")
like image 24
jwvh Avatar answered Dec 09 '22 00:12

jwvh


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

like image 43
pedrorijo91 Avatar answered Dec 09 '22 00:12

pedrorijo91


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.

like image 43
zcourts Avatar answered Dec 08 '22 23:12

zcourts