Adding a "return if" statement #6345
Replies: 5 comments 5 replies
-
Also, see #2195 |
Beta Was this translation helpful? Give feedback.
-
I don't know why so many people are so keen on just returning the value in a single line, even considering that for a single line
Example 1:
Example 2:
Or in the proper recommended Microsoft code style for if statements this would look like this: Example 1:
Example 2:
Which one should we choose? All of them are bad - reading-wise, both create some kind of waste. We don't need this isValid value stored if it's null, we don't need to pollute our variables. That's why I think their proper use cases to generate valid C# code, that would return value from an if statement Example:
|
Beta Was this translation helpful? Give feedback.
-
This is not the ms code style for if-statements. We allow the braces to be left off, and we allow it to be a single line. You can see examples of both in roslyn itself, as well as the runtime. |
Beta Was this translation helpful? Give feedback.
-
So it occurs to me that this could be simpler:
For code simplicity, readability and to reduce arguments I propose the following:
This return:
ArgumentNullException.ThrowIfNull(thingId)
one liner;Like an
else if
statement you can have multiple else if's with a final else. You can have multiplereturn if
statements before a final return.it could even go as far as:
Beta Was this translation helpful? Give feedback.
All reactions