Calgarypuck Forums - The Unofficial Calgary Flames Fan Community

Go Back   Calgarypuck Forums - The Unofficial Calgary Flames Fan Community > Main Forums > The Off Topic Forum > Tech Talk
Register Forum Rules FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Search this Thread
Old 03-24-2010, 12:18 PM   #1
kermitology
It's not easy being green!
 
kermitology's Avatar
 
Join Date: Oct 2001
Location: In the tubes to Vancouver Island
Exp:
Default 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?
kermitology is offline   Reply With Quote
Old 03-24-2010, 12:23 PM   #2
photon
The new goggles also do nothing.
 
photon's Avatar
 
Join Date: Oct 2001
Location: Calgary
Exp:
Default

Uh oh
__________________
Uncertainty is an uncomfortable position.
But certainty is an absurd one.
photon is offline   Reply With Quote
Old 03-24-2010, 12:24 PM   #3
kermitology
It's not easy being green!
 
kermitology's Avatar
 
Join Date: Oct 2001
Location: In the tubes to Vancouver Island
Exp:
Default

Hahaha.. yeah. I'm gonna kill someone.
__________________
Who is in charge of this product and why haven't they been fired yet?
kermitology is offline   Reply With Quote
Old 03-24-2010, 12:27 PM   #4
photon
The new goggles also do nothing.
 
photon's Avatar
 
Join Date: Oct 2001
Location: Calgary
Exp:
Default

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.
photon is offline   Reply With Quote
Old 03-24-2010, 12:28 PM   #5
kermitology
It's not easy being green!
 
kermitology's Avatar
 
Join Date: Oct 2001
Location: In the tubes to Vancouver Island
Exp:
Default

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?
kermitology is offline   Reply With Quote
Old 03-24-2010, 12:29 PM   #6
photon
The new goggles also do nothing.
 
photon's Avatar
 
Join Date: Oct 2001
Location: Calgary
Exp:
Default

Oh wait, or do you mean the specific ( a > b ? a : b ) thing?
__________________
Uncertainty is an uncomfortable position.
But certainty is an absurd one.
photon is offline   Reply With Quote
Old 03-24-2010, 12:30 PM   #7
photon
The new goggles also do nothing.
 
photon's Avatar
 
Join Date: Oct 2001
Location: Calgary
Exp:
Default

Quote:
Originally Posted by kermitology View Post
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.
photon is offline   Reply With Quote
Old 03-24-2010, 01:57 PM   #8
kermitology
It's not easy being green!
 
kermitology's Avatar
 
Join Date: Oct 2001
Location: In the tubes to Vancouver Island
Exp:
Default

Quote:
Originally Posted by photon View Post
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?
kermitology is offline   Reply With Quote
Old 03-24-2010, 02:09 PM   #9
photon
The new goggles also do nothing.
 
photon's Avatar
 
Join Date: Oct 2001
Location: Calgary
Exp:
Default

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.
photon is offline   Reply With Quote
Old 03-24-2010, 02:20 PM   #10
amorak
Lifetime Suspension
 
Join Date: Apr 2008
Location: 51.04177 -114.19704
Exp:
Default

and then I ate the bowl
amorak is offline   Reply With Quote
The Following User Says Thank You to amorak For This Useful Post:
Old 03-24-2010, 02:55 PM   #11
BlackEleven
Redundant Minister of Redundancy
 
BlackEleven's Avatar
 
Join Date: Apr 2004
Location: Montreal
Exp:
Default

Quote:
Originally Posted by photon View Post
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.
BlackEleven is offline   Reply With Quote
Old 03-24-2010, 03:02 PM   #12
photon
The new goggles also do nothing.
 
photon's Avatar
 
Join Date: Oct 2001
Location: Calgary
Exp:
Default

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.
photon is offline   Reply With Quote
Old 03-24-2010, 03:03 PM   #13
kermitology
It's not easy being green!
 
kermitology's Avatar
 
Join Date: Oct 2001
Location: In the tubes to Vancouver Island
Exp:
Default

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?
kermitology is offline   Reply With Quote
Old 03-24-2010, 03:26 PM   #14
BlackEleven
Redundant Minister of Redundancy
 
BlackEleven's Avatar
 
Join Date: Apr 2004
Location: Montreal
Exp:
Default

Quote:
Originally Posted by photon View Post
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)
BlackEleven is offline   Reply With Quote
Old 03-24-2010, 03:45 PM   #15
kermitology
It's not easy being green!
 
kermitology's Avatar
 
Join Date: Oct 2001
Location: In the tubes to Vancouver Island
Exp:
Default

Quote:
Originally Posted by BlackEleven View Post
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?
kermitology is offline   Reply With Quote
Old 03-24-2010, 03:49 PM   #16
Shazam
Franchise Player
 
Shazam's Avatar
 
Join Date: Aug 2005
Location: Memento Mori
Exp:
Default

Quote:
Originally Posted by BlackEleven View Post
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.
Shazam is offline   Reply With Quote
Old 03-24-2010, 03:56 PM   #17
photon
The new goggles also do nothing.
 
photon's Avatar
 
Join Date: Oct 2001
Location: Calgary
Exp:
Default

**** perl.

That is all.
__________________
Uncertainty is an uncomfortable position.
But certainty is an absurd one.
photon is offline   Reply With Quote
The Following 3 Users Say Thank You to photon For This Useful Post:
Old 03-24-2010, 04:04 PM   #18
Mad Mel
First Line Centre
 
Mad Mel's Avatar
 
Join Date: Mar 2009
Location: Brisbane, Australia
Exp:
Default

Quote:
Originally Posted by BlackEleven View Post
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 View Post
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.
Mad Mel is offline   Reply With Quote
Old 03-24-2010, 04:15 PM   #19
Azure
Had an idea!
 
Azure's Avatar
 
Join Date: Oct 2005
Exp:
Default

I don't know whether to cry or laugh at the fact that I'm actually getting what you guys are talking about.
Azure is offline   Reply With Quote
Old 03-24-2010, 04:21 PM   #20
BlackEleven
Redundant Minister of Redundancy
 
BlackEleven's Avatar
 
Join Date: Apr 2004
Location: Montreal
Exp:
Default

Quote:
Originally Posted by kermitology View Post
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...
BlackEleven is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 09:48 PM.

Calgary Flames
2024-25




Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright Calgarypuck 2021 | See Our Privacy Policy