Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: Make check and return from method in one line #7762

Closed
JohnyL opened this issue Jan 4, 2016 · 7 comments
Closed

Suggestion: Make check and return from method in one line #7762

JohnyL opened this issue Jan 4, 2016 · 7 comments

Comments

@JohnyL
Copy link

JohnyL commented Jan 4, 2016

We often write code similar to this:

private void SetList()
{
    var list = GetList();
    if (list == null) return;
}

private List<int> GetList()
{
    return null;
}

I suggest to make it more concise and make decision in one line with new keyword returnif:

private void SetList()
{
    returnif(var list = GetList()); //Exit when list is null
}

By default, it would check variable list for null. But when you know that returned value won't be null, then it could be expanded like this:

private void SetList()
{
    returnif(var list = GetList()).Count == 0; //Exit when list exists but has no items
}
@HaloFour
Copy link

HaloFour commented Jan 4, 2016

With any discussion as to variable scoping and declarations that has occurred with other feature proposals this syntax would not keep list in scope after this returnif construct. Beyond that I find it would be quite confusing to have a single keyword that would be both a conditional and a terminating keyword, particularly given that more often than not return is used to return a value and there seems to be no capacity for that in your proposal. I'd rather keep this to two operations.

@JohnyL
Copy link
Author

JohnyL commented Jan 4, 2016

Well, I don't think it's impossible for the compiler to re-write this statement into two. Compiler re-writes lots of things - async, lambdas, using, delegate. This one-liner could really save some key strokes.

@HaloFour
Copy link

HaloFour commented Jan 4, 2016

Indeed, the compiler can do a great many things. But whether or not it should is a completely different ballgame.

I'd say that the biggest problem with this proposal is how variables are declared. Leaking the scope out of the construct is in direct opposition to all conversations regarding inline variable declaration. See #254 or #6183.

Given that the rest of the proposal doesn't make much sense as you lose the ability to collapse the declaration/condition/termination to a single expression, and returnif on its own is certainly less clearer than a simple if and return.

@gafter
Copy link
Member

gafter commented Jan 4, 2016

This is already possible with #6400 which is implemented in the branch features/patterns

let List<int> list = GetList() else return;

Though, to be honest, I don't really see any problem with the original code you're hoping to "simplify".

@HaloFour
Copy link

HaloFour commented Jan 4, 2016

@gafter Good point. Also:

let List<int> list = GetList() when list.Count > 0 else return;

I thought that the semi-colon before else was removed?

@gafter
Copy link
Member

gafter commented Jan 4, 2016

@HaloFour Yes, thanks, I've surgically removed the semicolon.

@gafter
Copy link
Member

gafter commented Mar 20, 2017

We are now taking language feature discussion on https://github.com/dotnet/csharplang for C# specific issues, https://github.com/dotnet/vblang for VB-specific features, and https://github.com/dotnet/csharplang for features that affect both languages.

@gafter gafter closed this as completed Mar 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants