0

Is Reassigning false to primitive boolean a good use of ternary operator?

boolean a = false; 

Way 1 :

a = object.isAllowed() != null ? object.isAllowed() : false; 

Way 2 :

if(object.isAllowed() != null) { a = object.isAllowed(); } 

Kindly suggest which is a better way and why?

5
  • Those aren't entirely equivalent. The first will always overwrite a, whereas the second will only overwrite the value of a if object.isAllowed() != null. Commented Apr 16, 2020 at 14:25
  • 1
    Also, an isAllowed function returning null seems weird. That sounds like it should only return a Boolean value. Commented Apr 16, 2020 at 14:27
  • Cool. The variable can be overwritten with true since it already holds false. So is it good to use the Way 2 as above ? isAllowed will return a Boolean. Commented Apr 16, 2020 at 14:27
  • 1
    You should not care about efficiency at such places. If you care then check out this answer. What you should care about is readability of your code. Do whatever is more readable. Commented Apr 16, 2020 at 14:35
  • isAllowed() should return a primitive boolean value. Note in particular that a method that returns java.lang.Boolean must get named getAllowed rather than isAllowed in order to be Java Bean compliant. However, it can be named isAllowed if its return value is primitive boolean. Commented Apr 16, 2020 at 15:45

3 Answers 3

1

No, the simple solution for this would be;

boolean a = object.isAllowed() != null && object.isAllowed(); 

This is assuming that object.isAllowed() returns a Boolean object that is nullable.

Sign up to request clarification or add additional context in comments.

1 Comment

Or omit the null check and use Boolean.TRUE.equals()
0

As per your question, object.isAllowed() returns either true or null. Since already you have assigned value of a as false, it's better not to assign the same value to the same variable (if you consider the efficiency of your code).

So way 2 is better in that case. Or you can also go with Jason's answer (it is more clear and readable).

Comments

0

Is Reassigning false to primitive boolean a good use of ternary operator?

In general, nothing wrong with reassigning value to a variable (if necessary), in fact variable's literal meaning is changeable.

One should just keep in mind that inefficient re-assigning of variable increases space complexity, ultimately affects program performance.

It does not related to ternary operator.

Kindly suggest which is a better way and why?

Both have minor pros and cons Like:

Way 1:

Pros

1) Compact syntax

2) More professional

Cons

1) Inefficient reassigning of variable every time. Takes a bit more memory than traditional if else.

2) If multiple conditions involved, readability of code gets affected.

3) You must have to implement else part (:) of ternary operator

Way 2:

Pros

1) Takes efficient amount of memory. (Depending on how you wrote condition)

2) More readable where multiple conditions involved.

3) else part not mandatory

Cons

1) Lengthy syntax

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.