[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • Last Modified:

Catching - "not all code paths return a value"

Hi, In converting over to C# from VB I notice that I start to get a few messages on my functions saying that not all code paths return a value.
Now I believe that this is due to some levels of a conditional statement not having a return value (is that a correct assumption?).
In more complicated conditional staements are there any ways in ASP.NET to identify which code paths are producing this error. or a simple method to resolve this.
0
Simon Cripps
Asked:
Simon Cripps
  • 2
  • 2
  • 2
  • +2
3 Solutions
 
AndyAinscowFreelance programmer / ConsultantCommented:
Now I believe that this is due to some levels of a  conditional statement not having a return value (is that a correct  assumption?).

Yes - it is a return without supplying a value

Simple method to resolve - none that I know of.
0
 
Éric MoreauSenior .Net ConsultantCommented:
you surely have IF statements without a ELSE statement.
or you have SELECT CASE (SWITCH) statements without the ELSE counterpart.
0
 
lazyberezovskyCommented:
AFAIK VB allows not to assign return value to functions - default value will be used in this case. Maybe when you are converting your code those execution passes resulted in such case. If so, you can fix your logic by adding return of default value to the end of function. E.g. false for bool and 0 for int.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
ZippitCommented:
You must be using the 'return' keyword to cause a procedure to 'return' from inside a conditional.  So make sure all your returns are actually returning a value (not just a blank return to exit the function).  Other then that, put a default 'return' at the end of your proc to catch all other cases.
0
 
ToddBeaulieuCommented:
This is a funamental clean coding issue. What would you rather debug, a routine that has 27 seperate exit points, or a single exit point? What if you needed to add a line of code that gets executed in addition to each of those exit points? You wouldn't want to seek out all exit points, inserting duplicate code into each.

I understand that you inherited this, but consider cleaning up any code that takes more than a a minute to figure out what the heck it's doing.

Consider creating a local return variable, possibly with a default value.

Then, in your conditional code, rather than exiting, you would assign values to the return variable.

Finally, there would be a single exit point at the end of the method.

This philosophy goes back to the stone ages, and continues to be a sound one.
0
 
lazyberezovskyCommented:
ToddBeaulieu,
Single exit point is an obsolete style of programming. Passing result value over all method body (knowing that it will not be changed any more) makes code less readable.
You better to read here http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement

BTW Martin Fowler prefers guard conditions for exiting method.
0
 
ToddBeaulieuCommented:
Oh boy, I'm not getting sucked into that debate!
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
Personally I prefer to explicitly state the return value where needed - readability - with a default value at the end of the function.

eg.
int Foo()
{
  if(x)
  {
 return 1;
  }
  else if(y)
{
 return 2;
}

return 0;  //no matches found.
}


As I initially said somewhere you have a missing return, it is likely to be something like

int Foo()
 {
   if(x)
   {
  return 1;
   }
   else if(y)
 {
  return 2;
 }
 
 //function exits WITHOUT a return value being specified
 }

0
 
ZippitCommented:
Hahaha.  Couldn't agree more ToddBeaulieu
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now