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

Extend throw syntax to include a conditional check #14924

Closed
mrhuo opened this issue Nov 3, 2016 · 9 comments
Closed

Extend throw syntax to include a conditional check #14924

mrhuo opened this issue Nov 3, 2016 · 9 comments

Comments

@mrhuo
Copy link

mrhuo commented Nov 3, 2016

I would like to propose a new syntax, the existing similar code as follows:

public string queryOrderNo(string userId, string address, string name)
{
    if(string.IsNullOrEmpty(userId)){
        return "no userid";
    }
    if(string.IsNullOrEmpty(address)){
        throw new Exception();
    }
    if(string.IsNullOrEmpty(name)){
        throw new Exception();
    }
    //...other code
}

As you see, like more more if keywords..

Can we like this?

public string queryOrderNo(string userId, string address, string name)
{
    return string.IsNullOrEmpty(userId) ??? "no userid";
    throw string.IsNullOrEmpty(address) ??? new Exception();
    throw string.IsNullOrEmpty(name) ??? new Exception();

    //...other code
}

Is help for your?

@jveselka
Copy link

jveselka commented Nov 3, 2016

I think that throw expressions (coming in C#7) do what you want:

return string.IsNullOrEmpty(userId) ? "no userid"
    : string.IsNullOrEmpty(address) ? throw new Exception()
    : string.IsNullOrEmpty(name) ? throw new Exception()
    : //...other code (if you refactor it into expression)

@dsaf
Copy link

dsaf commented Nov 3, 2016

  1. The title should reflect the content e.g. "Alternative syntax for if-throw guard checks".
  2. There is a better solution for this type of problem Proposal: Method Contracts #119.

@DavidArno
Copy link

DavidArno commented Nov 3, 2016

If we rationalise the original code, then the three versions (including from #119) would be:

public string QueryOrderNo(string userId, string address, string name)
{
    if (IsNullOrEmpty(userId)) return "no userid";
    if (IsNullOrEmpty(address)) throw new Exception();
    if (IsNullOrEmpty(name)) throw new Exception();

    //...other code
}

public string QueryOrderNo(string userId, string address, string name)
{
    return IsNullOrEmpty(userId) ??? "no userid";
    throw IsNullOrEmpty(address) ??? new Exception();
    throw IsNullOrEmpty(name) ??? new Exception();

    //...other code
}

public string QueryOrderNo(string userId, string address, string name)
    requires !IsNullOrEmpty(userId) else return "no userid"
    requires !IsNullOrEmpty(address) else throw new Exception()
    requires !IsNullOrEmpty(name) else throw new Exception()
{
    //...other code
}

which begs the question, why? It can be argued that the requires version could be used as metadata for document generation, but beyond that, neither offers a compelling case for adopting it, over what the language already offers. Both get the 👎 from me.

@dsaf
Copy link

dsaf commented Nov 3, 2016

More like:

public string QueryOrderNo(string userId, string! address, string! name)
    requires address != string.Empty
    requires name != string.Empty
{
    if (string.IsNullOrEmpty(userId)){
        return "no userid";
    }

    //...other code
}

@AlgorithmsAreCool
Copy link

Hello @mrhuo,

That is a interesting idea to help make method argument validation easier. That is something that alot of people want to see in C# and it has been discussed for a long time. If you could please read through the discussion raised in #119 and see if you like the design made there.

If you think that you like your design better than please try to expand on it with some more specifics about how exactly the compiler should treat the new ??? operator. Take a look at this document that explains how to create a proposal.

As dsaf said, it helps everyone if you can give the issues you create here a name that is descriptive so that people can find it easily. And be sure to always search Area-Language-Design for other discussions that might have taken place already.

Thanks for contributing.

@mrhuo
Copy link
Author

mrhuo commented Nov 3, 2016

@zippec Thanks for reply to me, but your solution not for I want, because I want check condition first, and then run other code. but your code return outter to the method directly.

@mrhuo
Copy link
Author

mrhuo commented Nov 3, 2016

@DavidArno First, thank you reply, I'm sorry to have kept you so confused. I just want code shorter , more readable, more graceful, like a artwork.
In fact, coding is a very boring job, but I just want to make it more interesting, do you think?

@mrhuo
Copy link
Author

mrhuo commented Nov 3, 2016

@dsaf Thanks for your reply and your tip to me.
I think the code:

public int Insert(T item, int index)
    requires index >= 0 && index <= Count
    ensures return >= 0 && return < Count
{
    return InsertCore(item, index);
}

Destroy the beauty of the C# style, like "Gangnam style" song with “little apple”'s dance.
HaHa~All in all, thank you.

@CyrusNajmabadi CyrusNajmabadi changed the title want a new syntax... Extend throw syntax to include a conditional check Nov 8, 2022
@CyrusNajmabadi
Copy link
Member

Closing this out. We're doing all language design now at dotnet/csharplang. If you're still interested in this idea let us know and we can migrate this over to a discussion in that repo. Thanks!

@CyrusNajmabadi CyrusNajmabadi closed this as not planned Won't fix, can't repro, duplicate, stale Nov 8, 2022
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

7 participants