Recommendations: Returning from a method if the switch statement is blocked?

What is the preferred way to return a bool from a switch statement? I understand that this can be subjective, but I believe that it is important for our profession to delve into the best practices :).

public bool foo(param) { switch (param) { case 1: if (something) { return true; } return false; default: return false; } } 

- OR -

 public bool foo(param) { bool flag = false; switch (param) { case 1: if (something) { flag = true; } break; default: break; } return flag; } 
+4
source share
8 answers

The difference here is between one return point and multiple return points.

Typically, a code with a single point of return tends to do a lot of bookkeeping (temporary variables, check these variables in loops) and can get hairy as the logic becomes more complex. I saw code that goes to great lengths to use the while (flag) ... flag = false; pattern while (flag) ... flag = false; instead of the while (true) ... break; pattern while (true) ... break; and it’s not interesting to read.

I prefer several return points, as they return as early as possible (no extra work is done) and do not require local residents to help keep track of the current return value. Also, I don't find them harder to read than code with a single return point.

There's a nice divergence of this on c2 ( SingleFunctionExitPoint ).

I found that keeping the methods as "functional" as possible is good (I say "functional" here because I usually do C #, which outside LINQ is not considered functional). By “functional,” I mean that I try to avoid the mutating state as much as possible. Introducing more states in the form of local residents or members makes it difficult to understand, since now you have to consider their values, and you have to consider how these variables can be set (from another method, from another class, ...).

In general, a state is evil, but sometimes it is a necessary evil.

There is an interesting conversation about this particular issue here on SO .

+12
source

I prefer the second method.
This is much more adaptable, you can, for example, add some work at the end of the function, which will depend on flag

Also, if you have only one case, you can go to something other than a switch

+5
source

I prefer an early return, but in general, I really try to avoid switching, if at all possible, if he does not choose between no more than three parameters. And I try to isolate the switch as much as possible, so the switch method only defines the variable - everything else is handled by the calling code.

IMO, early return is easier to read, since I don’t need to “read” further when I encounter a return.

+3
source

It looks like a really complicated trick to avoid the && & operator. Boolean expressions - coolio:

 public bool foo(int param) { return (param == 1) && something; } 
+2
source

Given the two options, I am not saying that they are too nested too much - you must call a function that returns a boolean in your examples.

Given the question between one return point and a multiple return point (and my previous point), I take a multiple return point, because you can simply return the value returned by your function for this case.

As an example:

 public bool foo(param) { switch (param) { case 1: return MethodForParamEqualOne(); default: throw new ArgumentException("Param value=" + param + " is not supported."); } //If you're doing stuff here, the method is likely doing too much. } 

In the end, these switch statements are refactored into method calls from objects (which will improve the settings for the awful nesting switches). If you're really smart, you might not even need the switch statement in your factory object (xml configs, reflection, database, ...).

+2
source

It all depends on what I'm doing. If I do this, and then I finish, I will return - why run the unnecessary code? If I want to do more work, then I set a flag or something else. Use the right tool for the right job.

+1
source

I would generally disable the switch statement:

 public bool foo(param) { if(param != 1) { throw new ArgumentException("Param value=" + param + " is not supported."); } return MethodForParamEqualOne(); } 
+1
source

When it comes to returning and selecting conditional blocks, it might be a good idea to always be in the back of the head to comment on critical code. If you use

while (true)

don't be afraid to quit

// WARNING + reason!

on top of the interrupt code. This is usually overkill most people will tell you, but as soon as it gets a little smoother with

 for (int x = 0; /*external variable for condition*/ ; x ++) 

I found that it helped me prevent frequent code failures when you quickly tried to test small changes in many codes that you really don't need to read, since the code gives a small bonus to others more.

A bit of peanuts, to be honest, but I just think that I will share what helped me, so that it helps others.

bool a = false;

 while(true) { ReDraw(); ReUpdate(); //WARNING! Critical breakpoint <--- Edit , nobody likes a game you can't win :P a = CheckForWinGame(); if (a) { break; } } 

In this example, I hope to show that the warning prompted me now in no time, I can interfere with the methods, but not with the latter. It is not waterproof, but quick

+1
source

All Articles