Proposal: Language feature "return if myCondition" #1493
Replies: 13 comments
-
What would be the advantage of this? return if (myCondition);
if (myCondition) return;
təˈmeɪtoʊ;
təˈmɑːtəʊ; |
Beta Was this translation helpful? Give feedback.
-
gaurd statement? #138 |
Beta Was this translation helpful? Give feedback.
-
@alrz - because some companies (like where I work) enforce curly braces for statements for the condition, even if its a one liner. If added to the language as per my post then people working for companies which require these code rules can have more succinct code. Having to have the braces means your guard clauses can take up half your screen! |
Beta Was this translation helpful? Give feedback.
-
@ufcpp - nice to see others have already proposed it. |
Beta Was this translation helpful? Give feedback.
-
Riot. Seriously though, you want this feature to get around your company's coding convention? that really doesn't seem to be a good reason to add a language feature IMO. I'd expect that such strict guidelines to forbid using |
Beta Was this translation helpful? Give feedback.
-
Well then, return if (myCondition);
if (myCondition) { return; } As much as I like the idea of a |
Beta Was this translation helpful? Give feedback.
-
But if the function must return a value, how do you do that with this proposed solution? |
Beta Was this translation helpful? Give feedback.
-
guard, defer, return if... these all go into file 13. My friend in high school joked that we should have an "unless" statement. Throw that in there too. |
Beta Was this translation helpful? Give feedback.
-
That's #560. 😄 |
Beta Was this translation helpful? Give feedback.
-
Take a Look to the following code:
Right now we cannot mix the first condition (if (!rootLineTotalPlanned.HasValue)) |
Beta Was this translation helpful? Give feedback.
-
You can already write this as: public decimal? CalculateNewPlannedAmountForLastPaidLine(EscalationDataAmount escalationDataAmount, decimal? rootLineTotalPlanned, DateTime? plannedDate)
=> rootLineTotalPlanned.HasValue
? CalculateNewPlannedAmount(escalationDataAmount, plannedDate) - rootLineTotalPlanned.Value
: default(decimal?); I think there are already enough language features to help get brevity here. I don't think a new form of 'return' is really needed. |
Beta Was this translation helpful? Give feedback.
-
I don't know who you are or how you found that but you made my day. |
Beta Was this translation helpful? Give feedback.
-
I am a fan of one line guard clauses like.
Which would throw an exception if the argument value is null.
So in the same vein, my currently most desired new C# feature would be a "return if" kind of statement.
Which would return from a method if the condition is true. It would simply be syntatic-sugar for
Is this a language feature which could be considered for inclusion in a future version of C#?
Beta Was this translation helpful? Give feedback.
All reactions