• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

If Statements

 
Jim Ball
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am wondering if using two if statements back-to-back is bad java coding practice. Below, is an example of when you might want to use the aforementioned if statement setup. Would a switch statement be a better way to go, or is the below type of coding design not a problem?

int x = 2; //Default value or else compiler might complain
if (x = 0){
do something;
}
if (x = 1){
do something else;
}
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65342
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, back to back ifs aren't necessarily any problem. But in the case you show -- in which the conditonals are the same execpt for a target value -- I would probably have opted for a switch.

It's "tidier" and there's less chance of transcription error as the variable x would be referenced once with the value being the only variable. This more closely matches the intention of your construct than the serial ifs would.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65342
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, that all assumes that you meant:


[ December 07, 2006: Message edited by: Bear Bibeault ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12267
36
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
of course, it's possible in this situation that the "do something" has a side effect of changing the value of x to 1, in which case the second if would ALSO run. Again, if that's what you want, you're fine.
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An alternative to the switch would be



The choices would then be mutually exclusive.
 
Neerav Narielwala
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes you must use :
if()
{
}
else if()
{
}

It's a good programming practice...
 
M. Lee Smith
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is bad coding when there is a cleaner method using the if--if else, but they both work regardless.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the above case I would probably just use the if / else if structure. But for anything more than two consecutive ifs, I would strongly consider alternatives. A switch statement is a possibility, but very often I would prefer to look up something in a HashMap or array instead. The thing being looked up may even be a command object, representing an action to be taken once the execute() method is called. (Or perform() or run() or whatever you want to call it.) That may be a bit advanced for the beginner forum, so don't worry if it doesn't make a lot of sense right now - but be aware that there are other alternatives that you may wish to investigate later.
 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it bad practice to use the =()?: kind of statement?


And if it is acceptable to use that kind of statement, can I pass it a more beefy argument like
 
Paul Clapham
Sheriff
Posts: 21588
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, those are okay. In my opinion that is. But what about this:Here we have a nested tertiary operator, which many might think was going too far. All things are good in moderation but can be taken too far.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moderation, schmoderation! The duoternary operator works great in Comparable / Comparator implementations:

You should code like this as much as possible so that it becomes more well-known. Because I'm tired of people looking at me funny when they see it.
 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could I take it as far as this without violating any style rules?

varA = total < 5 ? 0 : total < 8 ? 1 : total < 11 ? 2 : 3;

It seems to be the same as



Not asking to be smart, but it compiles fine and does what I want, and it really seems like an easy way to say what I want without typing a lot of lines of code.
[ December 14, 2006: Message edited by: Nathan Leniz ]
 
Marilyn de Queiroz
Sheriff
Posts: 9068
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nathan Leniz:
Could I take it as far as this without violating any style rules?

I suppose it depends on the style rules of the company you are working for. Personally, I remember my first job programming professionally, I ran into something similar to Jim's example and it took me quite a while to figure out what the author was trying to do. I prefer to read code without working quite so hard to understand it.

Martin Fowler said, "Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Nathan]: Could I take it as far as this without violating any style rules?

That depends what style rules you're using. A few guides won't want you to use the ternary operator at all. Or maybe you're only allowed one per statement. Or no nested ternary operators (and it's arguable whether your example could be considered nested). There's no definitive answer to this.

Consider, also: the first time someone sees something like this, it may be harder for the read. The second time, it's easier. So if you're just going to do this once, it's probably not worth it. Instead, try to use this style as frequently as possible. Just for Marilyn.

On a slightly more serious note, when I do write code like this, I often insert line breaks for readability. These group the alternatives more readably than parenthesis, in my opinion.

This sort of thing most often seems to appear in Comparators:

I actually do find it's advantageous to write Comparators like this - by grouping the alternatives right next together, it's easier for me to see the overall pattern of the code. If you just have to write one Comparator, it may not be worth it, as it's too nonstandard for some people. But if you've got a lot of different Comparators in your code, the style will quickly become familiar, and the benefits become more significant.
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should code like this as much as possible so that it becomes more well-known. Because I'm tired of people looking at me funny when they see it.


I too, use the ternary operator often -- and whole heartily agree with Jim on this.

However, you better be confident with this tact, because quite frankly... it is very likely, that you will be challenged in your next code review. (There are too many anti-ternary operator developers out there)

Henry
 
Russell Peters
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:


I too, use the ternary operator often -- and whole heartily agree with Jim on this.

However, you better be confident with this tact, because quite frankly... it is very likely, that you will be challenged in your next code review. (There are too many anti-ternary operator developers out there)

Henry


I agree..

As a New Year's Wish ...I would want all code reviewers to be programmers and coders ...as in "hands on" so that they would understand what we do without us having to explain such simple things to them.

Well..that could be a new thread in the Meaningless Drivel section which explores the gaffes of the coding world!
 
Dan Fowler
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing to take into consideration using the ternary operators is if your code will be ever be inherited by other developers. The ternary operators may be 'slicker', but because many people don't understand them it may really throw them off if they end up maintaining your code some day. So it may cause a hiccup on some teams. Just something to think about. Good luck.
 
Doc Ahrens
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was asked in an interview how to optimize:

if( x == 0 ){
// do something
}else if( x == 2 ){
// do something else
}else if( x == 5 ){
// do another thing
}else if( x == 7 ){
// do yet another thing
}

And he said the answer was that switch would be better because it would only take two steps, rather than having to evaluate through each item, but wouldn't a switch have to evaluate through each item in the switch (like the "if else" would) and therefore take just as much evaluation time? I would like to know.

[ February 17, 2007: Message edited by: Tim Ahrens ]
[ February 17, 2007: Message edited by: Tim Ahrens ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic