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?
I think that throw expressions (coming in C#7) do what you want:
C#
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)
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 :-1: from me.
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
}
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.
@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.
@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?
@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.
Most helpful comment
I think that throw expressions (coming in C#7) do what you want:
C# 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)