-
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: Conditional control statements #724
Comments
Seems completely unnecessary given you can already use all of those control statements as the embedded statement of a condition: if (condition) break;
if (condition) continue;
if (condition) goto foo;
if (condition) return;
if (condition) throw new Exception();
if (condition) yield break;
if (condition) yield return value; The new proposal isn't at all more concise. All it does is satisfy the excessively OCD who feel that their code is somehow "dirty" if they don't follow a conditional with a block. It's another way to do something for the sake of another way to do something. |
Reminds me of Ruby. However, I don't really think it's worth it. First of all, it doesn't add anything extremely useful. Furthermore, we'll have two ways of expressing the same idea without any semantic difference so it needs to be covered by project code guidelines or otherwise the code will be written differently by each person. |
@HaloFour I mentioned your point in the last section. I kinda disagree with you because I think consistency (by adding blocks after conditionals) is important, but I have a feeling other people will side with you (13 minutes in, 2 thumbs-down already). I'll close this issue. |
In Ruby, lines equivalent to the following kept confusing me: throw new ArgumentOutOfRangeException("Some exception message, that is very long.", nameof(parameter)) if condition; When scanning code, I start reading from the begging of each line. If I see that the line starts with It's even worse here on GitHub, I have to scroll to see the That's why I'm against this proposal. |
Background
It's pretty common to see control statements such as
break
,continue
,return
, etc. inside a branch of a loop. For example, the following is a common idiom that ensures a condition is true before continuing the loop.Proposal
One thing that struck me is the logic to break out of the loop seems more verbose than necessary. Since this is such a common pattern, wouldn't it be nice if we could just write
? Right? No new keywords introduced, and we eliminate 3 really sparse lines of code (
{
,break;
,}
) that taking up precious screen real estate. I believe Python has similar syntax to do this, e.g. you can writereturn foo if bar else baz
,raise some_error if condition
, etc. I think this would be a logical feature for C# to adopt, too.More usage examples
List of control statements that will be affected
break
continue
goto
return
throw
yield break
yield return
Extra Notes
if (!condition(item)) break;
in the first place, which is just as short asbreak if (!condition(item));
. However, for those who prefer to use braces or even leave thebreak
statement on the next line, there is a significant difference in concision.The text was updated successfully, but these errors were encountered: