-
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: if and return in one statement #1397
Comments
First, your solution does not seem to match the problem: your original code has e.g. Second, you yourself seem to be unclear about which expression is tested and which is returned (as seen in the |
Sorry about wrong return types, I rewrite code many times. Yes, is only ideas, but my proposal is not about the solution, but if is possible to reduce if and return statements in only one.
and use something like:
|
Seems a lot like use case for code contracts #105 |
What speaks against using a combination of boolean operators and/or ternary bool SomeMethod(int a, int b)
{
return CheckMethod(a)
&& ValidateSomething(b)
&& ...
&& ... ;
} |
Ruby already has a well established syntax for this. If memory serves, it looks like this:
This Ruby style syntax is more general purpose than your proposal - it can be used in more different places than just for guard clauses. The idea of adding this syntax has already been discussed several times (it keeps coming up in the comments on other proposals) and it's unlikely to happen.
If I saw someone write this on a code review, I'd be encouraging the author to refactor their code. To me this sounds as though there's an essential domain object begging to be identified and turned into a real class. #ymmv |
Also #138 dotnet/roslyn#11562 |
@leonardobos what do you want is called macro. For example, in Rust: macro_rules! try {
($expr:expr) => (match $expr {
$crate::result::Result::Ok(val) => val,
$crate::result::Result::Err(err) => {
return $crate::result::Result::Err($crate::convert::From::from(err))
}
});
($expr:expr,) => (try!($expr));
} that allows you to write: fn foo() -> Result<bool> {
try!(bar(a));
try!(baz(b));
return true;
} which expands into: fn foo() -> Result<bool> {
match bar(a) {
Ok(()) => (),
Err(e) => return Err(From::from(e));
}
match baz(b) {
Ok(()) => (),
Err(e) => return Err(From::from(e));
}
return true;
} But C# doesn't have macros so it's not currently doable. |
Ruby just places a condition after |
@Pzixel - in Rust you're supposed to use the |
@Clockwork-Muse I intendenly didn't use question mark operator because it's a compiler builtin (which OP is asking for) while it can be easily done with macro system (that's what I wanted to show). |
What if I wanted |
Based on comments and down votes I think that is a bad idea |
Hi,
in many cases, especially in parameter checks, I have to write:
CheckMethod
andValidateSomething
are methods that usually have many lines of code and are used in other methods, especially in MVC where various actions has validation steps or returns of different views/results.My proposal is reduce the if-return statement in one statement like:
What you think about that? is hard to do... c# itself has a better approach, or this proposal has simple a bad idea?
Thanks
The text was updated successfully, but these errors were encountered: