Sign in to follow this  

[.net] C# Symantic Question

This topic is 4270 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
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
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
This is an old argument, briefly:

The side against multiple returns are a group of old fellows from the ancient days. They feel that such things are "bad". They feel that multiple returns are confusing, that they are unwarrented, and that they clutter the code up with exit points.

The side for multiple returns are a group of old fellows from the not so ancient days. They feel that multiple returns can simplify your code, reduce special cases, and allows for tracing the code to procede in a much more logical fashion.

Side A has it wrong. By requiring only a single exit point, you end up having to introduce a variety of hacks to get out of loops, and conditionals. It also makes the code much harder to follow, as you must constantly be looking for those special cases to realize that a function should terminated (instead of it just terminating through a return).

In some trivial cases, it can appear that having only a single exit point is simpler, and it may very well be. But in non-trivial cases it is rarely the case that a single return makes the code simpler.

Share this post


Link to post
Share on other sites
Why is that method called "MyStringMethod" ... ?

Multiple return points aren't bad by definition. What is bad is unnecessary gunk that obscures the actual functioning code, and any statement of the form
if(something) return true;
else return false;

... is wasting space (why not just return something?).

I don't think there's anything wrong with
if(something) return expression1;
else return expression2;

... as long as they aren't bools, although there are people who would suggest that
return something ? expr1 : expr2;

... is better. I think it depends on how trivial the expressions are, and I don't tend to use ?: because it confuses me.

People that tell you things are bad without giving you a decent reason why should probably be ignored. As Washu says, often multiple return points are the only way to write sane code to solve a complex problem, and many complex mathematical problems have an 'easy out' that avoids all the complex processing – not to use a return there would be plain silly.

Share this post


Link to post
Share on other sites
The reason that I prefer extracting this logic out into a bool is that it is slightly over engineered this way. It is easier to maintain in the future, since statements can just be added into the braces. After you maintain someone elses badly written code for a while, you learn to like it when people practice good coding techniques.

My opinion is that if the code exits from a function in more than one location then it should be a special case that was profiled and can benefit the whole program by doing it that way. It should also have some comments describing why, and how, and also hopefully the old easier to understand code fragment should be commented out and left nearby, so it can be easily reverted to after your optimization becomes meaningless. This is especially true if the code is lowlevel assembly or something else that is lowlevel.

Just my 2 cents.

Share this post


Link to post
Share on other sites

This topic is 4270 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this