Skip to main content
gamedev.net
🔒 Locked

If(...) return inside void functions/methods

Started by xissburg Apr 13, 2009 at 2:24 PM 16 replies 2.4k views
Original Post
xissburg
xissburg
What do you think about

void f(int x)
{
    if(x > 10)
        return;

    //Do something. . . 
    //.  . .. .. . .... . .    . . 
}

? (no specific language)
.
Driv3MeFar
Driv3MeFar
I think you should ask a more specific question.
xissburg
xissburg
Uh? What you mean? Isnt this specific?

Oh, and I think you should had given your opinion...
.
Mad_Coder
Mad_Coder
Uhmm, sure! It's.. Brilliant? No really what do you mean "what do you think?" Is it relevant to some languages? Uses concepts of programming? If that's what you're asking, then yes, is does/is!
ApochPiQ
ApochPiQ
I think it looks like Santa Claus after being hit by a freight train.


I also think I have no idea what you are trying to ask. Are you asking if this is a good practice? If it affects performance? Whether or not it's a magical incantation that will make you rich and famous if you chant it backwards 3 times?
alvaro
alvaro
I think he is asking if exiting early from a function is considered good or bad coding style.

My opinion is that it is perfectly OK. If it makes your code more clear (and it usually will), go for it.
xissburg
xissburg
I want advice about performance and coding style and whatever. If I go to specify few languages then C, C++, C#, Java and such.

And thanks for the advice alvaro.
.
Captain P
Captain P
Quote:
Original post by xissburg
I want advice about performance and coding style and whatever.

Then mention that in your question. The given code gives no description of what you were trying to ask, neither did your post. It's pretty hard to judge code without a context. Having that said, it's still hard to judge style and performance (performance should be measured anyway, not judged) based on so little code.

For example, a slow, unoptimized function that is only called a few times is much less of a problem than a slow, unoptimized function that is called thousands of times per frame. And something that feels slow may not be the actual bottleneck.
Antheus
Antheus
Google for "multiple return statements".

It depends. There are pro and contra arguments for single as well as multiple return statements. At the end of the day, they are all completely irrelevant, and merely serve as entertainment for bored coders and managers which like to run code metric tools.

Also: I claim that only correct value for tab is exactly 5 spaces.
c_olin
c_olin
That is how I normally do it. However in most cases the function needs to communicate that an error happened or that it was unable to execute on the given input. Whether it was returning an error code or printing to standard output.
xissburg
xissburg

I also don't think this is a big thing...but:

Quote:
Original post by Antheus
Also: I claim that only correct value for tab is exactly 5 spaces.


Why?!? I like 4..

.
NotAYakk
NotAYakk
Because anything else would be wrong.

I mean, 4 factors in to 2 ... and 2! How confusing.

5 is pure. 5 is prime. 5 is the one true tab width.

(The previous poster is trying to indicate that what you are asking about is a religious issue -- one for which there are many answers, all of which differ only by the chosen dogma of the particular programmer.)
Wan
Wan
I like the Morse code in your comments!
DevFred
DevFred
You seem to do parameter checking. In that case, I'd use exceptions instead. Returning at the start of the function is something I don't have a problem with.
Bregma
Bregma
Quote:
Original post by NotAYakk
5 is pure. 5 is prime. 5 is the one true tab width.


Duh, 7 is prime, too, and will give you a longer nonrepeating sequence of tabs and spaces if you like to mix both in your indentation style, as the ghodz of programming dictated. If they did not intend this, they would not have given you both a tab key and a space bar.
Stephen M. Webb
Professional Free Software Developer
xissburg
xissburg
Quote:
Original post by WanMaster
I like the Morse code in your comments!

lol I did that accidentally =P

Tab with 4 spaces is the maximum I would do otherwise the code goes too much to the right in some sections of the code...
.
Zahlman
Zahlman
I do use this technique. Try also Googling for "guard clause".
Mantear
Mantear
It has its uses. Using your extremely simple example, I would have done:

void f(int x){   if (x <= 10)   {      // Do stuff.   }}


However, if there are conditions that would cause a function to not want to exectute (bad input parameters, given parameters result in meaningless work done, etc) and it doesn't read well to do what I showed above, I'll make a check at the top of the function like you wrote and expliticly terminate the function by returning.

Your example is too simplistic to provide proper comments on.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.