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