8

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.

2
  • 6
    The XOR operator is rather rare in high-level programming in general.
    – Vache
    Commented Mar 11, 2012 at 13:10
  • The functionality provided by both is frequently implemented as IF-ELSE statements.
    – user1249
    Commented Mar 11, 2012 at 13:31

4 Answers 4

8

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?

5
  • 7
    I think that we don't see the logical XOR because people find '!=' more readable.
    – Nettogrof
    Commented Mar 11, 2012 at 15:59
  • 1
    It would be a great trivia question, but it would be useless in an interview. Knowing something that no one has seen in 10 years and will likely never use won't tell me if you can write good, readable code and launch products. :)
    – jmort253
    Commented Mar 11, 2012 at 17:13
  • 1
    @jmort253: If have seen this kind of code more than once. But it was invalid code in each case. So an interview question like this would tell me whether or not the supplicant knows how to spot smelly code.
    – A.H.
    Commented Mar 11, 2012 at 17:19
  • @A.H - Ok, that makes sense. You definitely want people who can spot shaky logic. :)
    – jmort253
    Commented Mar 11, 2012 at 17:34
  • Also, I agree with @Nettogrof - I've used != on booleans a couple of times, and logical XOR never even crossed my mind.
    – Izkata
    Commented Mar 12, 2012 at 1:00
7

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.

4
  • 3
    I will say I use the ternary operator a lot, and I find it quite readable. It's just a matter of getting used to seeing it, and as you said, knowing when to use it. IMHO, if it can't fit on one line, then use if-else.
    – jmort253
    Commented Mar 11, 2012 at 17:09
  • I think the when is very important. At review time, I've knocked back code along the lines of foo.setParam( bool ? ClassA.method() : ClassB.method ). It works, but it's a bit of a smell, IMO.
    – chooban
    Commented Mar 11, 2012 at 17:14
  • Not sure I agree. Although if you and I were working together I would do my best to play nice and be conservative in my use of that. :)
    – jmort253
    Commented Mar 11, 2012 at 17:16
  • It's all down to context. The example I was thinking of (which, of course, I don't have to hand/cannot post) was where business logic about where to pull data from was being obscured. An example of somewhere that a few extra lines of code and a comment would help the next developer in the area. Just don't get me started on unreadable perl code that uses unless ( !bool ) {. :)
    – chooban
    Commented Mar 11, 2012 at 17:23
3

This is more of an opinion-based answer but in my case:

  1. Indeed, for some reason I find the ternary operator less readable than an if-else construct.

  2. 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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.