|
|
|
#1 (permalink) | |||||||||||||
|
Off By 340 Undecillion
|
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
}
Code:
if(condition)
{
return value;
}
rest of function involving more return statements
__________________
Congratulations! You have found the secret text! You get a cookie.
Last edited by The Bartender Paradox : 08-15-08 at 02:10 AM. Reason: Now with 5% more clarity |
|||||||||||||
|
|
|
|
#2 (permalink) | |||||||||
|
Programmer
|
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.
|
|||||||||
|
|
|
|
#3 (permalink) | |||||||||||||
|
Off By 340 Undecillion
|
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.
__________________
Congratulations! You have found the secret text! You get a cookie.
Last edited by The Bartender Paradox : 08-15-08 at 02:10 AM. |
|||||||||||||
|
|
|
|
#4 (permalink) | |||||||||||||
|
Every base is base 10
|
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.
|
|||||||||||||
|
|
|
|
#5 (permalink) | |||||||||||||||
|
Windows Wrangler
|
Quote:
Quote:
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!
Last edited by mrtn400 : 08-15-08 at 02:59 AM. |
|||||||||||||||
|
|
|
|
#6 (permalink) | ||||||||||||||
|
Photography nut
![]() |
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:
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
|
||||||||||||||
|
|
|
|
#8 (permalink) | |||||||||||||
|
Every base is base 10
|
Yeah, but switch only takes constant expressions. You can't do < > conditionals cleanly
|
|||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|