-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Proposal: Add 'Unless' conditional statement, as seen in Ruby #560
Comments
We had that already in closed issue #157. |
You can write a custom tool function that fits your needs enough. class MyTools {
public static bool SeeAsUsing(bool expr) => !expr;
} and use it using MyTools;
if ( SeeAsUsing( faa || bar || baz ) )
DoSomething();
else
DoSomethingElse(); Or start reading to your mind "if not" or "unless" if you see |
PS: why are you using curly braces for single statements in your Besides, it's written |
Thanks for pointing #157 out, I didn't manage to find that while searching for a similar proposal. After reading I disagree that this is a duplicate, as #157 and all the debate surrounding it propose a solution which would not provide any semantic advantages over what we already have, and the style was much more appropriate for VB.Net. The debate became derailed by the choice of syntax, as opposed to the usefulness of the concept. Under the original proposal (and its discourse) you would still have to flip the logic in your head (read: "if not foo or bar or baz"), as opposed to writing a sentence which reads naturally (read: "unless foo or bar or baz") I would like to see this discussed again under this updated proposal |
You might want to point out, especially to @CyrusNajmabadi ( 👋 ) that there exist humans in the world who would have a much easier life if some keywords existed that reflect their usual (cultural?) way of thinking. |
@casperOne When you give 👎 you should at least explain why! 😞 |
You already have the options of inverting the condition or changing the order of the |
I prefer something like if !(obj is string s)
{
...
} as discussed in #7927 at roslyn. That discussion was moved to #157 (which was closed due to lack of support for the original proposal and not specifically lack of support for So I don't feel like |
I'd like to cite team mebmber @CyrusNajmabadi
Somewhere else (I don't find it at the moment) he stated more explicitly that the minor benefits, that changes like this proposal will bring, will never outweigh the [i.a. real money] costs of implementing it. Also there is simply no time to engage in features that "just add another way of doing something that can already be done in different ways". Especially when it is only about saving 2 characters. |
I'm glad they decided upon that policy after implementing expression-bodied members 😇 |
@DavidArno Though probably used most times for shortening the appearance of methods, expression bodies have an actually different concept behind it, namely allowing expression-syntax instead of statement-syntax. That's why |
We were still following that policy. In this case though, expressoin-bodied members provided enough value to make us feel that it was worthwhile. |
@CyrusNajmabadi How so? Do expression-bodied members really provide value? I always thought they were just a shorter way of saying the same thing. What can I do with expression-bodied members that aren't easily done with previous constructs? |
Yes. There is a substantial reduction in language ceremony for exceptionally common patterns. With the dual benefit of greatly decreasing code and having that be very widespread, this sugar was deemed worth it. |
I very much disagree with the earlier justifications against this keyword (both in this thread and in #157) Are there other ways to achieve the result? Sure. Are they better? No. The whole logic of "We can already do this with xyz" makes no sense to me. If that was the case, why create C# at all? We could do everything in Visual Basic. Why create the In fact, why are we using high level languages at all, give me back my punch cards and let me code like a man. It's nonsense. The fact is that an I'm going to particularly single out "Especially when it is only about saving 2 characters." here... it's not about saving two characters, it's about the ability to read something at a glance
And even ignoring the visual side of things "If not local user is an administrator" is obviously less comprehensible at a glance than "Unless the local user is an administrator". I know that everyone in this thread has likely been coding for a decade or more, that's the nature of the community... but that just means we're used to a bad way of doing things, it doesn't make it good. "We can already do that in some other, slightly less readable way" is antithetical to everything else I see in C#, and I object to it strongly. This would be a fine addition to C# Show me someone who claims they have never once misread |
I guess I must be a liar, I've never had problems with You can also do |
🖐 I honestly think that makes me a liar too? |
Yeah, count me too. I learned the lesson about the need to pay attention to every character long before I encountered any of the languages in the C family that use So I'm pretty confident that I've not overlooked a FWIW though, I'd kind of low key like to see if and unless permitted as conditional suffixes after other statements ...
... but that's been discussed to death and it's not going to happen ... and I'm happy enough with that. |
One of the best design features of Ruby is the fluid read-time semantics of code which can be achieved.
Probably the most useful feature to this effect is the
unless
statement, which behaves like anif
statement, except code runs if the expression evaluates to false.This is great at reducing logical noise, particularly where an if statement's expression is complex enough to need wrapping in brackets before inversion.
For example
Proposed syntax:
It's quite a small language feature in the grand scheme of things, but I believe this could reduce the read-time complexity on a lot of C# code.
The text was updated successfully, but these errors were encountered: