[.net] C# Symantic Question

Started by
12 comments, last by BradSnobar 18 years ago
Quote:Original post by Nypyren
return (gpa >= 3.5);


That's my opinion.


I like that.. and

if(gpa >= 3.5)     return true;return false;
Advertisement
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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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.
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.

This topic is closed to new replies.

Advertisement