I have read in a recent code review that both ternary operator (condition ? foo : bar)
and the XOR operator ^
are rarely used in Java. Is it true?
If yes, is this because they are less readable? or some other reason.
I have read in a recent code review that both ternary operator (condition ? foo : bar)
and the XOR operator ^
are rarely used in Java. Is it true?
If yes, is this because they are less readable? or some other reason.
The ternary operator is well used, especially for short null-checks / defaults:
System.out.println("foo is "+(foo==null) ? "not set" : foo);
Some people consider this not as readable as an if/else, but that was not the question.
The XOR bitwise operator is only used in bit-processing. If you need a bitwise XOR, then there is no way around it.
The XOR logical operator is indeed so rare, that I did not see it in the last ten year in Java in valid cases. This is also due to the fact, that the boolean XOR "does not scale" like ||
or &&
. What I mean:
if( a && b && c && d ) .... // it's clear what the intention is
if( a || b || c || d ) .... // here also
if( a ^ b ^ c ^ d ) .... // ???
In the last case I would guess, the coder meant "only one should be true". But XOR is a beast. The coder got the beast and not what (s)he wanted.
That would be an interesting interview question: What is the result of the last if
?
!=
on booleans a couple of times, and logical XOR never even crossed my mind.
I used to find the ternary operator difficult to parse, but I've since found that there are some places where it is very useful. These don't tend to be application logic, but logging.
For example:
log.info( "Object foo is " + ( foo.isEnabled() ? "" : "not " ) + "enabled" );
To me, that's a lot neater than either
if ( foo.isEnabled() )
log.info( "Foo is enabled" );
else
log.info( "Foo is not enabled" );
Or
log.info( "Foo is enabled : " + foo.isEnabled() );
In summary, it's a question of where it's being used and for that.
As for bit-wise, I still struggle with those but that's because I'm used to working at a high level of abstraction, as other commenters have suggested. If I do come across somewhere in code that someone's decided to use that to be "efficient", the time wasted while I figure it out negates benefits.
foo.setParam( bool ? ClassA.method() : ClassB.method )
. It works, but it's a bit of a smell, IMO.
unless ( !bool ) {
. :)
This is more of an opinion-based answer but in my case:
Indeed, for some reason I find the ternary operator less readable than an if-else construct.
As for XOR, probably most Java programs have no use for such low-level bitwise manipulations. For the most part, it seems to be just a relic inherited from C.
Exclusive-or is rare in any programming language.
The ternary operator isn’t unusual at all. Some abuse it to win competitions in “who can write the longest and most obscure line of code”, that’s equally awful in any language. You have to figure out how to use it in the most readable way. For example
let result = ( condition1 ? Value1
: condition2 ? Value2
: condition3 ? Value3
. . .
: condition10 ? Value10
: value11)
Is perfectly readable.