[JAVA] Prevent unnecessary null checking !: Method with double type argument

Overview

When I'm working on removing unnecessary sources while testing with JUnit in my business I noticed that there are quite a few null checks that I don't need, so it's a memo.

I'm checking for null even though it can't be null

For example, this method.

private static double doubleMethod(double value){

		String stringValue = String.valueOf(value);

		if(stringValue == null){
			return 0;
		}
		return value;

	}

It is a source that seems to have thought "Null may come if it is a String type, so let's check for null!". I don't notice it at first, I put null in the method argument and try to test with AssertEquals. .. .. For compilation errors.

Oh, the argument was double type ...

Since the double type is a "primitive type" variable, it cannot contain null, which means "there is nothing instead of putting an object reference". So, everything that comes in double type has some value. So converting it to a reference type like a String doesn't require a null check.

By the way, if you really want to indicate that there is no value in double type, use java.lang.Double.NaN. I don't think there are many situations where it is used ...

Recommended Posts

Prevent unnecessary null checking !: Method with double type argument
[ruby] Method call with argument