How to handle "not all code paths return a value" when function logic provides a return

I suppose the easiest way to explain is a far-fetched example:

public static int Fail() { var b = true; if (b) { return 0; } } 

This code will not compile and gives the error "not all code paths return a value", while we humans can clearly see that it is. I understand why. My question is what needs to be done to rectify the situation. It could be something like this:

 public static int Fail() { var b = true; if (b) { return 0; } throw new ApplicationException("This code is unreachable... but here we are."); } 

But all this just seems stupid. Is there a better way? Again, this code is a contrived example (and can be reduced to return 0 ). My actual code is massive and complex, but logically (using mathematical proof) returns a value before trying to exit.

+6
source share
6 answers

The analysis of C # code flow is limited, and as your example shows, there are cases where all paths are returned, but the compiler cannot detect it. Throwing an exception is an acceptable remedy in these circumstances.

I would not use the default return to fix this error. You work on the assumption that the line never gets on the advice of the compiler. Consider for a second that your analysis is incorrect, and execution may go to the end of the method. If you return the default value, you will not have any indication of a problem. The method will simply return bad data. Throwing an exception will make it very obvious that there is a problem.

However, in those cases, I prefer to simply rewrite the code so that the compiler can see that all the paths end. In general, I found that if the method is so complex that the compiler cannot follow it, then the guy who takes the code after me will not be able to execute it.

+8
source

The compiler does not perform complex mathematical processes to ensure that you return a value; it is a relatively dumb beast in this regard.

If you are confident in your assumptions, just add return 0; at the end with a comment declaring reality that it will never be achieved.

Of course, any code that can guarantee that the final condition is true can simply remove the condition, as you said. This is mathematically true even for complex cases. Thus, you can reorganize your code to take this into account without unnecessary return.

+1
source

I think your current approach is correct. Returning the default value at the end of the method is risky, because if there is a thread in your logic, this instruction can be reached, which can have unintended consequences. If you throw an exception, this will give you the opportunity to detect an error.

+1
source

Usually there are several classes of programmers:

  • Those who return in all.
  • Those who return only at the end of the funcion / method.

First approach:

 public static int Fail() { var b = true; if (b) return 0; return 1; } 

I usually prefer the second approach, as it is easier to quickly see where the function / method is coming from.

 public static int Fail() { var b = true; var returnValue = 1: if (b) returnValue = 0; return returnValue; } 
0
source

A really late answer, but I can imagine a scenario in which the function will not know what to return:

1) Perform step by step using the debugger

2) Step over var b = true;

3) When if (b) { reached, put b = false in the clock. It will return false and assign b = false

4) Step (more if )

5) The end of the function is reached, return is not possible

From this point of view, return is explicitly required.

0
source

I had code like this and fixed it this way. Looking at your example:

 public static int Fail() { var b = true; if (b) { return 0; } throw new ApplicationException("This code is unreachable... but here we are."); } 

will become

 public static int Fail() { var b = true; if (!b) { throw new ApplicationException("This code is unreachable... but here we are."); } return 0; } 

This will get rid of the compiler warning, it will still be logically the same, and in fact there will be slightly cleaner code.

0
source

Source: https://habr.com/ru/post/952439/


All Articles