assert vs. null in Java

1

Often when I'm clicking code the Android Studio warns me that such a function can return @nullable, its recommendations are two: Enclose everything in an if you check if the object, variable, function is null or use the statement assert (except for what it is used for)

Example

final LinearLayout img = (LinearLayout) SplashActivity.this.findViewById(R.id.splashscreen);

//con checkeo si es nulo
if (img != null )
    img.setVisibility(View.GONE);

//con assert
assert img != null;
img.setVisibility(View.GONE);

What is best practice and if it affects performance using one or the other.

    
asked by Webserveis 22.03.2016 в 18:04
source

3 answers

3

The reserved word assert is used in Java to perform validations of the code at run time. You must be careful when using assert since if an evaluation fails a Error will be thrown (more serious than a Exception ), specifically a AssertionError and when this happens the program, if the error is not controlled the application (or thread) will be stopped. By default, the asserts are ignored by the JVM, you can activate them by adding the parameter -enableassertions or -ea . As a personal recommendation, only use assert during debug or execution time in development environments, do not enable the evaluation of assert s in production. Important: do not confuse the reserved word assert with assertXyz methods of test frameworks such as JUnit or TestNG, this last group fulfill a different functionality.

Now, about whether you should always validate whether the variable is null or not, it's a design issue. You can do what is called defensive programming, which is to defend your code of anything that may affect it such as validating if there are variables null and replace them with default values that will not do anything, or design your application to handle the NullPointerException at a higher level than your method. Depending on what you do, you must choose between one or the other.

About the example code that you put in the question and based on the previous explanations, I would recommend that you use the first form, the verification of null since your code will be protected against any error:

if (img != null )
    img.setVisibility(View.GONE);
    
answered by 22.03.2016 / 18:15
source
2

If the variable can be null at runtime:

You must validate that it is not null before trying to access a member of it or else it will throw an exception.

But not only is it enough to wrap the code in a if , you have to reason what to do if the variable can come in null and do something else or throw your own exception with a personalized error message.

Assuming that the variable can not be null.

Then the fact that a null value has been cast represents a bug. The assert , what it does is verify this and launch a AssertionError if it is not met. Upon encountering this error you should immediately assume that it is a programming error and try to fix the code so that this does not happen again.

    
answered by 22.03.2016 в 18:18
1

A assert is a reserved word whose instruction serves to validate in our code that when the execution of the application occurs, certain condition must be evaluated to true.

In the case of Android the assert is not very used, in fact you can see it in the SDK classes, the reason is simple in the case of the Dalvik virtual machine (which is still very used) is set to ignore by default assert. Also remember that in the emulators the Assets are disabled by default.

Based on your question:

  

Enclose everything in an if you check if the object, variable, function   is null or use the assert statement.

I suggest in the specific case of Android perform the check is it nil?

if (obj==null) {
 throw new CustomError("Object cannot be null");
}

in your example I suggest using:

if (img != null )
    img.setVisibility(View.GONE);
    
answered by 22.03.2016 в 18:34