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?

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
Pradeep D
  • 1
  • 2
  • 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`. – Carcigenicate Apr 16 '20 at 14:25
  • 1
    Also, an `isAllowed` function returning `null` seems weird. That sounds like it should only return a Boolean value. – Carcigenicate Apr 16 '20 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. – Pradeep D Apr 16 '20 at 14:27
  • 1
    You should not care about efficiency at such places. If you care then check out [this](https://stackoverflow.com/a/35189815/12323248) answer. What you should care about is readability of your code. Do whatever is more readable. – akuzminykh Apr 16 '20 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. – VGR Apr 16 '20 at 15:45

3 Answers3

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.

Jason
  • 5,154
  • 2
  • 12
  • 22
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).

S.Mandal
  • 172
  • 1
  • 10
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

Zain Ul Abideen
  • 1,617
  • 1
  • 11
  • 25