Primitive Zeros and Java 8

I know that the best practice for handling null primitives was to use a wrapper in a box, for example Integer instead of int , as discussed here

Zero for primitive data types

However, this is still valid today with Java 8, which introduced optional primitives such as OptionalInt , whose OptionalInt.empty() effectively represents null ? I understand that Optionals should be used only for return types of methods, and not as types for the properties themselves. Should null primitives still be stored as nested properties? Should they be Optional in Method Return Methods? Or can I save as OptionalInt in the property itself?

+5
source share
2 answers

Suppose you have a method

 public void logRequest(Integer userID, String content){ //log content locally or in db or a rest call } 

Suppose you got the userID from the database for a specific user. The following options are possible:

  • DB returns the user id.
  • Identifier not found.

So, if the id was not found, what do you go to logRequest ?

  • Are you null ? What if the method does not have a null check. What should he do null ?
  • Do you have to pass 0 or -1 ? If null means 0 , then this may conflict with the actual user with id 0 . How do you distinguish between null and 0?

Of course, the above problem can be solved by correct documentation and correcting the rules, but this leads to higher service and error pricing. And correctly written code should be its own documentation.

Now let's say that the method was declared as:

 public void logRequest(OptionalInt userID, String content) 

This method shows very clearly that it expects an argument, which is optional. Thus, only 2 cases are processed: the corresponding argument is sent (which means that there is no traversal with 0 ). If the option was empty, then the method correctly knows how to handle it. (Unlike where we had to read its documentation to predict behavior).

Thus, in the method declaration, he himself explains the behavior, not the documentation that performs it for you, and relies on documentation and wild crap. Options in this case are very useful.

PS: I always feel a pinch go null to a method waiting for Integer . This is definitely not the case! Gosh, in Java, the subconscious mind always sees ints as primitives. They should not be null.

+6
source

The answers to the question you linked explain why primitive values โ€‹โ€‹cannot be null . There is not the slightest assumption that using values โ€‹โ€‹in a box to represent null is "best practice", in fact, it has not been said that using null is good practice in general.

Instead of trying to emulate null values โ€‹โ€‹for primitive types, you should take a step back and think about what you are going to express. This semantics can be expressed using many options, using null , not necessarily being one of the best options. One easy way to express uninitialized or "cleared" values โ€‹โ€‹is to have the optional boolean fooPropertyInitialized variable. Its much clearer than using null and its overhead is not necessarily more than boxing primitive values.

This also applies to the API. Do not use Optional for a property. Just specify the clearFooProperty() method if you want to support such an operation, plus isFooPropertyInitialized() . Its much cleaner so that getFooProperty() throws an exception if the caller failed to check the initialized state.

Note that you can also simply use an out-of-range value, valid for a specific context, to express special conditions. This is common practice, for example, InputStream.read() methods that return -1 to reach the signal to the end.

Optional is the ideal return type for an operation, while there is no storage for the result, so you cannot split the request for presence and value into two method calls, and the range of values โ€‹โ€‹of existing values โ€‹โ€‹is unlimited.

But, as said, without knowing what semantics you want to express, we cannot make a recommendation, and there can be more than one possible solution without "best practice" ...

+3
source

All Articles