Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Mar 11 '12 at 13:15

This question came from our site for professional and enthusiast programmers.

5  
The XOR operator is rather rare in high-level programming in general. –  Vache Mar 11 '12 at 13:10
    
The functionality provided by both is frequently implemented as IF-ELSE statements. –  user1249 Mar 11 '12 at 13:31

3 Answers 3

up vote 5 down vote accepted

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?

share|improve this answer
5  
I think that we don't see the logical XOR because people find '!=' more readable. –  Nettogrof Mar 11 '12 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 Mar 11 '12 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. Mar 11 '12 at 17:19
    
@A.H - Ok, that makes sense. You definitely want people who can spot shaky logic. :) –  jmort253 Mar 11 '12 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 Mar 12 '12 at 1:00

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.

share|improve this answer
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 Mar 11 '12 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 Mar 11 '12 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 Mar 11 '12 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 Mar 11 '12 at 17:23

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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