03-24-2010, 12:18 PM
|
#1
|
It's not easy being green!
Join Date: Oct 2001
Location: In the tubes to Vancouver Island
|
Best Practices
A question for the developers out there.
Do you use the conditional operator in your code?
__________________
Who is in charge of this product and why haven't they been fired yet?
|
|
|
03-24-2010, 12:23 PM
|
#2
|
The new goggles also do nothing.
Join Date: Oct 2001
Location: Calgary
|
Uh oh
__________________
Uncertainty is an uncomfortable position.
But certainty is an absurd one.
|
|
|
03-24-2010, 12:24 PM
|
#3
|
It's not easy being green!
Join Date: Oct 2001
Location: In the tubes to Vancouver Island
|
Hahaha.. yeah. I'm gonna kill someone.
__________________
Who is in charge of this product and why haven't they been fired yet?
|
|
|
03-24-2010, 12:27 PM
|
#4
|
The new goggles also do nothing.
Join Date: Oct 2001
Location: Calgary
|
You're not one of those "you have to write a 1 page document justifying why you used an if statement" guys are you?
I mean I like OO and inheritance and wish I was a Smalltalk guru as much as the next guy, but still...
__________________
Uncertainty is an uncomfortable position.
But certainty is an absurd one.
|
|
|
03-24-2010, 12:28 PM
|
#5
|
It's not easy being green!
Join Date: Oct 2001
Location: In the tubes to Vancouver Island
|
No one ever questions the use of an if statement.
__________________
Who is in charge of this product and why haven't they been fired yet?
|
|
|
03-24-2010, 12:29 PM
|
#6
|
The new goggles also do nothing.
Join Date: Oct 2001
Location: Calgary
|
Oh wait, or do you mean the specific ( a > b ? a : b ) thing?
__________________
Uncertainty is an uncomfortable position.
But certainty is an absurd one.
|
|
|
03-24-2010, 12:30 PM
|
#7
|
The new goggles also do nothing.
Join Date: Oct 2001
Location: Calgary
|
Quote:
Originally Posted by kermitology
No one ever questions the use of an if statement.
|
Oh I've dealt with guys that want as few if statements as possible.
"if there's an if statement, that means the object model isn't correct."
__________________
Uncertainty is an uncomfortable position.
But certainty is an absurd one.
|
|
|
03-24-2010, 01:57 PM
|
#8
|
It's not easy being green!
Join Date: Oct 2001
Location: In the tubes to Vancouver Island
|
Quote:
Originally Posted by photon
Oh wait, or do you mean the specific ( a > b ? a : b ) thing?
|
This.
__________________
Who is in charge of this product and why haven't they been fired yet?
|
|
|
03-24-2010, 02:09 PM
|
#9
|
The new goggles also do nothing.
Join Date: Oct 2001
Location: Calgary
|
Ah ok, I've always called that a ternary operator.
Yeah I'm not much of a fan either. Code readability is important in my opinion and using that instead of something more explicit hurts that, though that kind of is a self defeating argument because if it was used more it would be more readable.
But I'm one who'll do:
Code:
if (a > b)
{
doStuff();
}
else
{
// do nothing
}
just for readability...
__________________
Uncertainty is an uncomfortable position.
But certainty is an absurd one.
|
|
|
03-24-2010, 02:20 PM
|
#10
|
Lifetime Suspension
Join Date: Apr 2008
Location: 51.04177 -114.19704
|
and then I ate the bowl
|
|
|
The Following User Says Thank You to amorak For This Useful Post:
|
|
03-24-2010, 02:55 PM
|
#11
|
Redundant Minister of Redundancy
Join Date: Apr 2004
Location: Montreal
|
Quote:
Originally Posted by photon
Ah ok, I've always called that a ternary operator.
Yeah I'm not much of a fan either. Code readability is important in my opinion and using that instead of something more explicit hurts that, though that kind of is a self defeating argument because if it was used more it would be more readable.
But I'm one who'll do:
Code:
if (a > b)
{
doStuff();
}
else
{
// do nothing
}
just for readability...
|
Yes, its called a ternary operator. Some people definitely overdo it but I find it extremely useful for assignment.
Instead of
if ( y > z ) {
x = 1;
}
else {
x = 2;
}
you can just do
x = y > z ? 1 : 2;
To me, both are just as readable, but one takes 6 lines where the other takes one.
Even if you don't use it you should still know it since you'll be reading other people's code at some point and a lot of people do use it a lot, especially the old school guys.
|
|
|
03-24-2010, 03:02 PM
|
#12
|
The new goggles also do nothing.
Join Date: Oct 2001
Location: Calgary
|
Yeah it's not nearly as readable to me, just cause I've never used it that much.
Plus more lines of code means my boss thinks I'm more productive!
__________________
Uncertainty is an uncomfortable position.
But certainty is an absurd one.
|
|
|
03-24-2010, 03:03 PM
|
#13
|
It's not easy being green!
Join Date: Oct 2001
Location: In the tubes to Vancouver Island
|
It IS a ternary operator, but that just means that it's an operator that takes three parameters.
The actual operator itself is the conditional operator.
It's far more rare to find someone who thinks that an if-else branch is bad practice than the use of that operator.
__________________
Who is in charge of this product and why haven't they been fired yet?
|
|
|
03-24-2010, 03:26 PM
|
#14
|
Redundant Minister of Redundancy
Join Date: Apr 2004
Location: Montreal
|
Quote:
Originally Posted by photon
Yeah it's not nearly as readable to me, just cause I've never used it that much.
Plus more lines of code means my boss thinks I'm more productive! 
|
I see it several times a day, so I guess I'm just used to it.
I don't see why its bad practice to use it though. I actually prefer it to if/else in simple situations. It gets ridiculous for readability though when people try to do something complicated with it:
return x ? ( x > y ? 1 : 2 ) : ( x == y ? 3 : 4)
|
|
|
03-24-2010, 03:45 PM
|
#15
|
It's not easy being green!
Join Date: Oct 2001
Location: In the tubes to Vancouver Island
|
Quote:
Originally Posted by BlackEleven
I see it several times a day, so I guess I'm just used to it.
I don't see why its bad practice to use it though. I actually prefer it to if/else in simple situations. It gets ridiculous for readability though when people try to do something complicated with it:
return x ? ( x > y ? 1 : 2 ) : ( x == y ? 3 : 4)
|
OMFG.. If I EVER see a nested conditional I will seek out and destroy the person who wrote it.
It's not bad practice, but it's not good practice. I'm an extremely explicit coder. Prevents someone from doing something stupid, makes my code far more robust and clear.
__________________
Who is in charge of this product and why haven't they been fired yet?
|
|
|
03-24-2010, 03:49 PM
|
#16
|
Franchise Player
Join Date: Aug 2005
Location: Memento Mori
|
Quote:
Originally Posted by BlackEleven
Yes, its called a ternary operator. Some people definitely overdo it but I find it extremely useful for assignment.
Instead of
if ( y > z ) {
x = 2;
}
else {
x = 1;
}
you can just do
x = y > z ? 1 : 2;
To me, both are just as readable, but one takes 6 lines where the other takes one.
Even if you don't use it you should still know it since you'll be reading other people's code at some point and a lot of people do use it a lot, especially the old school guys.
|
Gak. I don't like that either.
int x = 1;
if (y > z) x = 2;
I always say, if you want to stuff as much as you can into one line, use PERL. Then, after you have regained your sanity, be verbose instead.
__________________
If you don't pass this sig to ten of your friends, you will become an Oilers fan.
|
|
|
03-24-2010, 03:56 PM
|
#17
|
The new goggles also do nothing.
Join Date: Oct 2001
Location: Calgary
|
**** perl.
That is all.
__________________
Uncertainty is an uncomfortable position.
But certainty is an absurd one.
|
|
|
The Following 3 Users Say Thank You to photon For This Useful Post:
|
|
03-24-2010, 04:04 PM
|
#18
|
First Line Centre
Join Date: Mar 2009
Location: Brisbane, Australia
|
Quote:
Originally Posted by BlackEleven
Instead of
if ( y > z ) {
x = 1;
}
else {
x = 2;
}
you can just do
x = y > z ? 1 : 2;
To me, both are just as readable, but one takes 6 lines where the other takes one.
|
Not all languages support it, but another way to do a very short if / else is
x = IIf(y>z, 1, 2)
A downfall in .NET is that it does evaluate both possible assignments, so both have to evaluate without error.
Personally for readability, I like Select...Case statements when they're appropriate.
Quote:
Originally Posted by BlackEleven
I don't see why its bad practice to use it though. I actually prefer it to if/else in simple situations. It gets ridiculous for readability though when people try to do something complicated with it:
return x ? ( x > y ? 1 : 2 ) : ( x == y ? 3 : 4)
|
In SQLServer Integration Services built in objects, you have to do things this way. I frequently give myself a headache.
|
|
|
03-24-2010, 04:15 PM
|
#19
|
Had an idea!
|
I don't know whether to cry or laugh at the fact that I'm actually getting what you guys are talking about.
|
|
|
03-24-2010, 04:21 PM
|
#20
|
Redundant Minister of Redundancy
Join Date: Apr 2004
Location: Montreal
|
Quote:
Originally Posted by kermitology
OMFG.. If I EVER see a nested conditional I will seek out and destroy the person who wrote it.
|
I've seen it. It's annoying, but its just readability. I'm worried about people that use memcopy, mutable variables, and non-volatile variables in threads...
|
|
|
Thread Tools |
Search this Thread |
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -6. The time now is 07:58 AM.
|
|