Jump to content
  • Advertisement
Sign in to follow this  
dokks

[.net] C# Symantic Question

This topic is 4464 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been reading Beginning C# Objects—From Concepts to Code by Jacquie Barker and one of the chapters talking about methods says that the following code while perfectly legal is poor grammer due to using multiple returns.
public bool MyStringMethod()
{
    if (gpa >= 3.5)
    {
       return true; // first return statement
    }
    else 
    {
      return false; // second return statement
    }
}

I do this all the time! I was curious if some of the more veteran .Net programmers have any opinions on this?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Calling something poor grammer just means "I don't like it but I don't know why/I don't want to explain why"

The former is just a poor statement, the latter leaves out the important information when to omit this "rule"

I think the idea behind this guideline is to make functions easier to verify.

Just don't have to much of them nested in very different conditions.

Share this post


Link to post
Share on other sites
I think this might be a better way to write the code:



public bool MyStringMethod()
{
bool gradeCheck;

if (gpa >=3.5)
gradeCheck=True;

else
gradeCheck=false;

return gradeCheck;
}




T!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
GPA doesn't have to be a bool for that to work.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Heh, Long Dog Studios had a post above mine, but I see that he quickly deleted it when he realized his error.

Share this post


Link to post
Share on other sites
Here's some helpful information about how C/C++/C# expressions work:

operator >= and similar operators work the same way as a function would; in this case, it takes two similar number arguments and returns a bool.

this means that when the return sees the expression, it will return the boolean.

Share this post


Link to post
Share on other sites
Some instructors in my school explained that the reason for having only one return statement is to make code verifiable by automated tools as well as reduce spagetti code. I suppose a mathematical proof of a function is easier to construct if only one return exists.

Share this post


Link to post
Share on other sites
Weird. Validation tools worth their salt should be able to track multiple returns. They have to track through loops and branches anyway which will affect the return result.


Anyway, I think that the book mentioned by the OP wasn't explicitly saying that multiple returns are bad, but that in that particular case it is unnecessary because your return value is the exact result of a comparison that you've already done.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!