I suggest you ...

Functions with optional return statement

I would suggest to add support for functions with optional return statement. There would be two possible cases:

1. The function's return type can be null (class or Nullable<struct>):

The function would return null by default.

int? GetSomeValue()
{
if(some_condition)
{
return; // no value to return, will be null by default.
}

//no return statement written, return null by defalut.
}

2. The function's return type cannot be null (struct or enum):

The function would return the default value of the return type.

int GetSomeValue()
{
if(some_condition)
{
return; // no value to return, will be 0 by default.
}

//no return statement written, return 0 by defalut.
}

7 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Anonymous shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    2 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • PhilippePhilippe commented  ·   ·  Flag as inappropriate

        I don't think it would be a good idea as it make the code somewhat less explicit...

        To return the default value in generic code, you can use default keyword (http://msdn.microsoft.com/en-us/library/xwth0h0d(v=vs.110).aspx). Otherwise, you should explicitly write 0 or null as it make the code much more clearer.

        Also, this is an error-prone proposition. If someone while maintaining code decide to add a returns statement and did not realize that the function should return a value, instead of having a compile time-error, it will silently compile. C# being a type-safe language, this kind of hole are undesirable as it weaken the effectiveness of the compiler to be able to detect errors...

        Also if you refactor the code of a long function that a has a lot of returns statements so that the function now has a return value (instead of void) and you forget to update one of those, the compiler won't be able to catch the programmer error.

        In large projects, it is really helpfull to have a language that is strict enough so that when refactoring code, the compiler is able to catch most changes that the user forget to do...

      Feedback and Knowledge Base