Scala avoid using null

I have a github project that is being analyzed by codacy . The analysis suggests "Avoid Using Zero" 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 fix it?

+5
source share
5 answers

I would keep it as it is. You don't actually use null here, just protecting it. An alternative would be to simply delete this line as a whole and decide not to process zero at all. Ignoring the possibility of null may be good in a well-built code base, where it should not appear in any case, and null will be a mistake, but a simple defender to catch it can prevent the appearance of more subtle errors if something goes wrong, and null actually happens.

+5
source

If path can be null , perhaps this is the easiest.

 require(Option(path).isDefined, "Must have a real Path") 
+4
source

the most idiomatic way would be to avoid the need (not sure, but I have an idea that it might throw an exception - something Scala strongly recommends against)

 def doSomethingWithPath(path:Path): Option[stuff] = { // will return an option of the type you were returning previously. Option(path).map { notNullPath => ... } } 

Now a possible case of null will be returned to the caller, who can / will propagate the returned parameter to a level that knows how to handle it correctly.

Note: it is possible that the best place to handle the null case is inside your function. In this case, you should do something like

 Option(path).map { notNullPath => ... }.getOrElse(/* take care of null case */) 

If you want to keep require , then jwvh answer will be my choice as well

+4
source

There is no need to explicitly check for null or wrap path in Option . You can do it:

  path match { case p: String => Option(doSomethingWith(p)) case _ => None //if path is null this will be returned } 

This will return an Option , which may not be what you want, but in this case, throw an exception instead of creating None . require will throw an exception in your example anyway, so if this is what your subscriber expects, just do it explicitly.

+1
source

Problem

Obstacle not to use zero is the best practice. This article explains why and Guava : Declaring -hoc Error Handling, Ambiguous Semantics, Slow Failure, etc.

Writing a request occurs because of the need for a quick and clean failure when the prerequisites for calling the method are not met. The problem is that it replaces NPE with another exception (nonetheless more descriptive), as @puhlen explained.

The perfect solution

In an ideal world, path:Path would be identical to number:Int , and a test for the existence of an object would not be needed. The problem is that scala (like many other languages) allows zero violation of a purely object-oriented approach.

Interim solution

The java / scala compiler must force the Optional type as the only code that manages null and forces null to exist in the set system. In such cases, any use of null may be considered a compilation error. I do not know if this is completely possible.

Using @ NotNull / @ Nullable Annotations

Since there is no default behavior for the language / compiler, you will have an impedance mismatch between libraries.

Practical solution

Define your class with Predef2 with minimal template logic. I still get only one "Avoid using zero" or use guava Preconditions.checkNotNull

 object Predef2{ def requireNotNull(object:AnyRef) = require(path != null, "Some object should not be null") } def doSomethingWithPath(path:Path) = { requireNotNull(path) ... } 
0
source

Source: https://habr.com/ru/post/1266809/


All Articles