This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

ternary operator

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following ternary operator:
int min = x < y ? x < z ? x : z : y < z ? y : z;

If I write it to if else statement should look like this?


 
Rancher
Posts: 3410
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add ()s to the ternary statement to make it easier to see the nesting.  That should help you rewrite it with if/else if statements
 
Saloon Keeper
Posts: 3411
149
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Afke,

if you have a ternary statement like that, even parentheses won't make it easier to read.
It is best to transform your ternary such that after each question mark, a specific value follows. That will make it readable as if it were a switch statement. For instance:

That is way easier to follow than writing it as: x = y <= 10 ? y >= z ? z <= 3 ? .... et cetera.

Of course, if you are doing the OCAJP, you can expect such nightmare-ternaries. So, practise these as much as you can, and using parentheses is a good idea.

By the way: there is a small error in your if-else translation.
 
Norm Radder
Rancher
Posts: 3410
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

parentheses won't make it easier to read.


I suppose it depends on the person.  It did make it easier for me when I added parans and some comments.
 
Piet Souris
Saloon Keeper
Posts: 3411
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doing it like this is indeed very useful.
 
Afke Kingma
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

parentheses won't make it easier to read.


I suppose it depends on the person.  It did make it easier for me when I added parans and some comments.



That is very helpful way to break down ternary operator like this, thanks
 
Marshal
Posts: 65050
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All those strange‑looking formatting tricks and () work in this instance because the ?: operator is one of the three kinds of operator to associate to the right. That means that the nested ?: expressions are regarded as being parsed before their surroiunding expressions; there is a simple example in the Java┬« Language Specification (=JLS), where you will also find that ?: is officially called the conditional operator. You will also find that the rules about the type of the whole expression are complicated, so I suggest you always use the same type for both x and y in expressions of the form b ? x : y.
The other kinds of operator that associate to the right are unary (prefix) and assignment (simple and compound).
 
Marshal
Posts: 6970
471
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A bit weird formatting, but I personally find this way considerably easy to understand:
 
Liutauras Vilda
Marshal
Posts: 6970
471
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For my brains also is easier if I use in translation only if-else, meaning, avoiding elsif.
 
Bartender
Posts: 2326
100
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my suggestion for brackets:
If I am only interested in the value, I'd simply write my code as :
 
Rancher
Posts: 3302
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, while I find Liutaras's formatting very readable, if it's been written by someone else I unfortunately can't be sure that the formatting is actually correct.  Well, for Liutaras I would of course be confident it was correct - but not for most of the random people I've worked with.   . So I'd prefer some more parens to make things explicit.  Salvin's solution would be my preference... but I'd also go for something like this, especially if we add terms to make the expression even more complex:

Then again,in this particular case I'd probably prefer to replace the whole thing with something like this:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!