null return / null continue... foo ?? return;

Topics: C# Language Design
Apr 4, 2014 at 5:07 PM
Rather than writing something like:
if (foo == null)
{
    return;
}
(replace return with continue/break)

Wouldn't it be nice to be able to write something like:
foo ?? return;
Which would return if null or skip over if not. Basically working the same as the if-statement.
Apr 7, 2014 at 2:15 AM
Personally I like to see return statement at the beginning of the line, this makes reading easier. And what if I want to check something more than null?
public string ReadSomething(IReader reader, DateTime ticketDate)
{
    string result = "default";
    reader ?? return result; // this code reads hard
    DateTime.UtcNow.AddDays(7) < ticketDate ?? return; // this is even harder
    result = reader.Read();
    return result;
}
Having special operator for specific check seems like unnecessary overhead. I would prefer to stick to old style in this:
public string ReadSomething(IReader reader, DateTime ticketDate)
{
    string result = "default";
    if(reader == null)
        return result;
    if(DateTime.UtcNow.AddDays(7) < ticketDate)
        return;
    result = reader.Read();
    return result;
}
Apr 7, 2014 at 4:18 AM
foo ?? return;
Isn't much less than
if (foo==null) return;
And the second reads MUCH better in my opinion.

(If you're workplace doesn't allow syntax of the second one, I very much doubt they'll allow the first one)
Apr 7, 2014 at 4:32 AM
if (foo==null) return;
This syntax is absolutely horrible.

1) It lacks braces, the ability to write if statements without braces should be removed from C#

2) It allows you to write multiple conditions which is just as bad.

Maybe something more readable:
return ifNull(foo);
The first suggestion just re-uses existing syntax, which is why I suggested it. I'm not saying that's the be-all-end-all solution. Just trying to prompt discussion.
Apr 7, 2014 at 4:37 AM
Edited Apr 7, 2014 at 4:40 AM
If "x ?? return" was supported, I'd expect the following to work too:
x ?? Foo(123); //Foo is a void method
x ?? y = 7;
x ?? break;
x ?? continue;
x ?? {
    var y = Y();
    Bar(y);
    if(MyCondition())
    {
       AnotherInvocation();
    }
}
I'd say no to this feature.
Apr 7, 2014 at 4:40 AM
If "x ?? return" was supported, I'd expect the following to work too:
So suggest a better syntax?
Apr 7, 2014 at 4:53 AM
Edited Apr 7, 2014 at 4:56 AM
It lacks braces, the ability to write if statements without braces should be removed from C#
That is very much your own opinion. Many programmers are perfectly fine without braces, especially for short lines like this. I in fact prefer using no braces for the sake of clarity. Using braces means that it's very easy to add more complexity to an if statement, without braces the complexity costs much more to add.

In my opinion the ability to write if statements with braces would be much more of a candidate to be removed from C#. It'd reduce the complexity of many programs. Either way it's ludicrous to make such a suggestion.

I really don't want this to devolve to the classic braces vs no braces, but suffice to say many programmers see a great deal of use in it in many scenarios. And TBH you're worry of this:
if (something)
    DoSomething();
    DoSomething2();
Should be something that static analysis tools can report. It'd actually be fairly easy to do this (they did something very similar on stage at //build). You could also have the files be auto-formatted whenever they are checked in, so it'd become apparent doing a code review. You could also have the developers just do smaller commits, and more reviews, and that should be easily caught.

But I digress.

Your new syntax:
return ifNull(foo);
Is even worse than the first one IMO, as it would very easily be confused with a function call, both by the programmer and the compiler.
It allows you to write multiple conditions which is just as bad.
I don't see what the problem is here. I don't think it's worthwhile to limit to just null checks, and once you open it up to boolean checks as well, then you might as well allow == and the like. And then you might as well allow && and the like. Otherwise you'd have a very odd subset of expressions. I'd assume if you have true checks, you want false checks, and ! is the way to do that.

If your solution is just handling nulls, then it's not very useful, it should probably handle more. It should probably handle abitrary expressions. Perhaps what you want is:
return if (foo==null);
which is basically the same thing, the only difference is that this could be restricted to just flow statements, or just a single statement maybe.

Honestly no matter the solution, I can see a lot more issues arising from the use of some new syntax (especially the ones suggested) than from just using the existing understood syntax of a single statement if block.


If you're following the convention of always having to have braces, why not follow the convention of a single return per method? That has a much more practical effect on code readability and bug avoidance (apple's bug was not really caused by the lack of braces, it was caused by the lack of proper function flow, and having multiple ways to break out of the method. All around poor software practices)
So suggest a better syntax?
I did suggest one, you just blindly shot it down without even discussing the pros and cons of them. if (foo==null) return; is a much cleaner, better understood version of what you want to accomplish.
Apr 7, 2014 at 6:42 AM
+1 to single return policy, it's much cleaner code to read!

With Roslyn out it's tempting to add more sugar to C#. It's also increases risk of going the wrong way.

Even today I'm trying to use LINQ in simplest way possible and the reason is that I need to create code that will be supported by less skilled/experienced developers. Not everyone is doing C# all night, some folks do it for living and they don't know half of the syntax we have (surely you have seen coders who have no idea of yield keyword).
Apr 7, 2014 at 6:46 AM
You say all this, yet we are making an absolute mess of constructors...