Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 08-15-08   #1 (permalink)
Off By 340 Undecillion
 
The Bartender Paradox's Avatar
 
amd nvidia

Join Date: Oct 2004
Location: Portland, Oregon
Posts: 2,338

Rep: 306 The Bartender Paradox is a proven memberThe Bartender Paradox is a proven memberThe Bartender Paradox is a proven memberThe Bartender Paradox is a proven member
Unique Rep: 234
Folding Team Rank: 448
Hardware Reviews: 1
Trader Rating: 3
Default If-else and return

This a simple thing that has been bothering me for a bit in C++.

Say I have a function with multiple return points, controlled by an If statement. Which is better, or is it just a matter of style:


Code:
if(condition)
{
    return value;
}
else
{     
    rest of function involving more return statements
}
or

Code:
if(condition)
{
    return value;
}

rest of function involving more return statements
They both do the same thing, but the second option saves me a couple of lines of code and from having to indent the rest of the function. Any reason not to do it?
__________________

A rocket powered land shark attached to a giant freakin' laser beam.
Congratulations! You have found the secret text! You get a cookie.

System: My System
CPU
AMD A64 3500+ Winchester
Motherboard
DFI nF4 SLi-DR
Memory
OCZ 4000VX
Graphics Card
EVGA 7800GT
Hard Drive
Maxtor 300Gb 16Mb Buffer
Sound Card
computers make sounds?
Power Supply
OCZ PowerStream 520W
Case
None
CPU cooling
Big
GPU cooling
Bigger
OS
XP Pro
Monitor
SOYO LCD

Last edited by The Bartender Paradox : 08-15-08 at 02:10 AM. Reason: Now with 5% more clarity
The Bartender Paradox is online now I fold for Overclock.net Overclocked Account The Bartender Paradox's Gallery   Reply With Quote
Old 08-15-08   #2 (permalink)
Programmer
 
JoBlo69's Avatar
 
intel nvidia

Join Date: Jan 2007
Posts: 3,363

Rep: 170 JoBlo69 is acknowledged by manyJoBlo69 is acknowledged by many
Unique Rep: 146
Folding Team Rank: 779
Trader Rating: 15
Default

unless you have an else statement, there is no need to put it there...

If is true, return value... Else return another value. If you only have one value to return, you can ignore the else part...

Question:

Why use on if then if you only have one return value?
__________________
My Lego case thread. With PICS!!!
-----------------------------------------------------------------------
Video card RMA database thread. I am working on an application that allows users to input their cards issues into a database, to build a knowledge base for what types of cards have a lower fail rate.

System: The "hold-me-over-until-i-can-get-a-i7" PC
CPU
E7200
Motherboard
Gigabyte GA-EP45-DS3L
Memory
Nothing!!! (new ram pending)
Graphics Card
8800GT
Hard Drive
2 x 74gb raptor raid0
Case
craptastic!!!
OS
64x vista ult.
Monitor
24" samsung
JoBlo69 is offline I fold for Overclock.net JoBlo69's Gallery   Reply With Quote
Old 08-15-08   #3 (permalink)
Off By 340 Undecillion
 
The Bartender Paradox's Avatar
 
amd nvidia

Join Date: Oct 2004
Location: Portland, Oregon
Posts: 2,338

Rep: 306 The Bartender Paradox is a proven memberThe Bartender Paradox is a proven memberThe Bartender Paradox is a proven memberThe Bartender Paradox is a proven member
Unique Rep: 234
Folding Team Rank: 448
Hardware Reviews: 1
Trader Rating: 3
Default

Quote:
Originally Posted by JoBlo69 View Post
unless you have an else statement, there is no need to put it there...

If is true, return value... Else return another value. If you only have one value to return, you can ignore the else part...

Question:

Why use on if then if you only have one return value?
Sorry, there are more return statements later on in the function, I'll try to make that more clear in the OP. Like for example, if there are invalid arguments supplied to the function return -1 otherwise continue on with the function normally.
__________________

A rocket powered land shark attached to a giant freakin' laser beam.
Congratulations! You have found the secret text! You get a cookie.

System: My System
CPU
AMD A64 3500+ Winchester
Motherboard
DFI nF4 SLi-DR
Memory
OCZ 4000VX
Graphics Card
EVGA 7800GT
Hard Drive
Maxtor 300Gb 16Mb Buffer
Sound Card
computers make sounds?
Power Supply
OCZ PowerStream 520W
Case
None
CPU cooling
Big
GPU cooling
Bigger
OS
XP Pro
Monitor
SOYO LCD

Last edited by The Bartender Paradox : 08-15-08 at 02:10 AM.
The Bartender Paradox is online now I fold for Overclock.net Overclocked Account The Bartender Paradox's Gallery   Reply With Quote
Old 08-15-08   #4 (permalink)
Every base is base 10
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 5,009
Blog Entries: 1

Rep: 566 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 338
FAQs Submitted: 6
Trader Rating: 5
Default

I would do the latter. They will result in identical assembly, but the second option reduces the amount of indentation. If you have a lot of nested conditionals/loops it can be easy to lose track of what level you're on (as somebody reading the code), so the latter is a bit more readable.
__________________
BIG BROTHER
Apple doesn't love you

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 08-15-08   #5 (permalink)
Windows Wrangler
 
mrtn400's Avatar
 
intel ati

Join Date: Aug 2007
Location: Carmichael, CA
Posts: 3,212
Blog Entries: 2

Rep: 208 mrtn400 is acknowledged by manymrtn400 is acknowledged by manymrtn400 is acknowledged by many
Unique Rep: 178
Hardware Reviews: 1
Trader Rating: 0
Default

Quote:
Originally Posted by The Bartender Paradox View Post
This a simple thing that has been bothering me for a bit in C++.

Say I have a function with multiple return points, controlled by an If statement. Which is better, or is it just a matter of style:


Code:
if(condition)
{
    return value;
}
else
{     
    rest of function involving more return statements
}
or

Code:
if(condition)
{
    return value;
}

rest of function involving more return statements
They both do the same thing, but the second option saves me a couple of lines of code and from having to indent the rest of the function. Any reason not to do it?
I would still use else, because it would be easier to debug IMO.

Quote:
Originally Posted by rabidgnome229 View Post
I would do the latter. They will result in identical assembly, but the second option reduces the amount of indentation. If you have a lot of nested conditionals/loops it can be easy to lose track of what level you're on (as somebody reading the code), so the latter is a bit more readable.
They compile the same?

Shows how much of a C++ nub I am. :|

Edit: Ooohhh! I get it now. I kind of forgot that if the first if statement is ran, the program quits.

(Note: I haven't touched anything coding for a year or so, and I'm waaay out of my coding mindset.)
__________________
Je parle un peu le français, mais je préfère parler l'anglais. Parlez-vous le français aussi?

For you English speakers:

">" - greater than; "<" - less than
"there" - Hey, look over there.
"their" - I love their Mustang.
"they're" - They're so lucky!

System: Cataclysm (Main Rig)
CPU
Q6600 G0 1.325v @ 3296Mhz (412x8) 1.41v
Motherboard
Abit IP35 Pro
Memory
4x 1GB Patriot DDR2-800 @ 812Mhz 4-4-4-12 2.15v
Graphics Card
Sapphire 4850 @ 725Mhz/1050Mhz w/ Catalyst 8.7
Hard Drive
2x WD1600AAJS RAID0
Sound Card
Onboard 7.1
Power Supply
Antec True Power Trio 650w
Case
Antec 900
CPU cooling
Tuniq Tower 120 w/ Scythe SY1225SL12SH 120mm
GPU cooling
Thermalright T-RAD² w/ 2x Scythe DFS922512M-PWM
OS
Windows Vista Ultimate x64 SP1
Monitor
1x Acer 19" @ 1400x900, 1x Sceptre 19" @ 1280x1024

Last edited by mrtn400 : 08-15-08 at 02:59 AM.
mrtn400 is offline mrtn400's Gallery   Reply With Quote
Old 08-15-08   #6 (permalink)
Photography nut
 
dangerousHobo's Avatar
 
amd nvidia

Join Date: Dec 2005
Location: ~/
Posts: 3,484

FAQs Submitted: 7
Folding Team Rank: 451
Trader Rating: 0
Default

If it was me, I'd go the second way. Just cleaner IMO.
__________________
"UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." - Doug Gwyn

Try out the latest Programming Challenge
Quote:
Originally Posted by Melcar
Only one reasonable way to solve this... a dance off.

CPU-Z Validation
@ 2.97-prime95 stable 16 hours @ 1.48v Proof | CPU-Z Validation @ 3.15


Getting Mouse Side Buttons to work in Linux, Compile a custom Kernel, More

System: Anomaly
CPU
Athlon 3700 SD(KACAE)0546 @3.02ghz
Motherboard
DFI UT nF4 Ultra-D
Memory
G.Skill 2x512 UTT(BH-5)
Graphics Card
evga 6800gs
Hard Drive
Maxtor 300GB + WD 250GB
Sound Card
onboard
Power Supply
Ultra 500w V-series
Case
one from Ultra
CPU cooling
Big Typhoon
GPU cooling
80mm fan mounted on
OS
Arch64 & Slackware 12.1
Monitor
Acer AL2216W 22" WS LCD
dangerousHobo is offline I fold for Overclock.net Overclocked Account dangerousHobo's Gallery   Reply With Quote
Old 08-15-08   #7 (permalink)
Overclocker
 
Join Date: Dec 2007
Location: Florida
Posts: 198

Rep: 9 Drakemoore Unknown
Unique Rep: 9
Trader Rating: 0
Default

First version for debugging. Although switches seem to be a bit better if you need more branching.
Drakemoore is offline   Reply With Quote
Old 08-16-08   #8 (permalink)
Every base is base 10
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 5,009
Blog Entries: 1

Rep: 566 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 338
FAQs Submitted: 6
Trader Rating: 5
Default

Quote:
Originally Posted by Drakemoore View Post
First version for debugging. Although switches seem to be a bit better if you need more branching.
Yeah, but switch only takes constant expressions. You can't do < > conditionals cleanly
__________________
BIG BROTHER
Apple doesn't love you

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 02:19 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.30766 seconds with 8 queries