• Post Reply Bookmark Topic Watch Topic
  • New Topic

Use of conditionals  RSS feed

 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm wrapping up a project at the client and making sure all of my code has been cleaned-up, has comments, and I have everything documented.

I noticed that I use this convention every so often -



This has gotten me complaints at a previous client as being difficult to read. Something I am now a little concerned about since I am not coming back to this client anytime soon.

I never use this convention in place of a regular if/else check. I only do it as a one-line determiner to set a value for a specific variable. This seems like a good practice to me because it keeps the code clean and minimal. The same code written as an if/else would be -



In my mind the original is cleaner and easy to read if you understand what it means. The second example adds unnecessary clutter. Still, I wonder if I am being a curmudgeon about this.

Opinions?

Java newbies, your input would be especially valuable.
[ September 29, 2006: Message edited by: Jason Cox ]
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the java world using ?: is uncommon, not sure why, it is used extensively in other languages.

I don't think it hinders readability, but if the people you are writing code for don't like it, don't use.
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about



I often use method names this way to document tiny but possibly obscure bits of code.
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Robert Hill:
In the java world using ?: is uncommon, not sure why,


I haven't really found this to be the case. I use it liberally when deciding between two choice for an assignment operation and find that it is far more readable that the equivalent if-then-else structure.

I also use the method idiom that EFH posted when I feel a need to abstract it a bit more.

but if the people you are writing code for don't like it, don't use.


Yup, "he who has the gold makes the rules"
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Robert]: In the java world using ?: is uncommon, not sure why,

[Bear]: I haven't really found this to be the case


I think the ternary operator is a bit less common in Java than in, say, C/C++, but there are still many of us who use it fairly often. People who find it "difficult to read" probably need to learn to read code better, in my opinion. Unless of course they're paying you to do that for them. Now nested ternary operators can be evil - but a single ternary ought to be quite legible to anyone claiming to be a Java programmer.

[EFH]: I often use method names this way to document tiny but possibly obscure bits of code.

Yes, this is good. Though in this case, I'd still have used a ternary within the refactored method. The additional clarity comes from adding a meaningful name, not from using four or more lines where one will do. Also, in a real situation the variable "importantValue" would (or should) have a more meaningful name, which could in turn eliminate the need for a specially-named method to initialize it.
[ September 29, 2006: Message edited by: Jim Yingst ]
 
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By coincidence, somebody else on a completely different forum had a problem trying to use that idiom in PHP.

PHP has the terniary operator (? , and PHP supports boolean expressions ... but PHP gagged (with an inscrutible message) trying to do what you're doing.

Personally, I don't see anything wrong with it ...
... but I think an "if/else" conditional is probably clearer (especially if you expect other people to be reading the code).
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Bear, though I may perhaps be a bit hesitant to extract it to a method. If it's simple but would be too long to be readable with ?: then I may use an if/else. If it's not particularly simple or isn't easy to read with if/else I'll probably extract it to a method. Otherwise I use ?:. It's not uncommon in my code for assignment, parameter passing and return values.
 
Jason Cox
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ken,

You actually bring up a good point.

Due to indentations and variable name lengths, some of the conditionals I was using at the previous client with the tenary operator were quite long and it was pretty unreadable. Unfortunately, the conclusion they drew was to take all of them out completely. This was also because it was confusing some of the junior developers.

This last team had a senior and mid-level developer, so I didn't think much about it. I broke one of my rules at this client about "coding with a plan" and the result was that I went back to what I was comfortable with.

I can see how overly long statements of any kind can be confusing, so I agree that it may not be as "clean" but it would certainly be more readable to break it up into a normal if statement.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!