Replies: 21 comments
-
Duplicate of #616. The reason this idea gets downvoted is because it wouldn't work with nested try/catches. Nested if/else's without braces aren't ambiguous. try/catches would be. |
Beta Was this translation helpful? Give feedback.
-
@DavidArno by the way nested if else statement for me is ambiguous. its pretty much opinion based.
It happened to me quite few times, so I stopped ever removing braces for nested if statements. |
Beta Was this translation helpful? Give feedback.
-
They're by definition unambiguous, but the syntax itself is absolutely ambiguous. if(foo)
bar();
else if(foo2)
bar();
else
bar(); This expression can be parsed in two separate ways:
or
The parser will just select the first option given the chance. edit: this is the reason why some languages use |
Beta Was this translation helpful? Give feedback.
-
Ambiguous to you, but not ambiguous to the compiler. The compiler only has one rule for how to deal with that. An try
try DoSomething()
catch (FooException ex) HandleFoo(ex);
catch (BarException ex) HandleBar(ex);
catch (BazException ex) HandleBaz(ex);
finally CleanUp(); There are three perfectly legal interpretations of that code. To quote Bjarne Stroustrup on the subject:
|
Beta Was this translation helpful? Give feedback.
-
I see, removing braces for catch only wouldn't be ambiguous though. |
Beta Was this translation helpful? Give feedback.
-
Your point is well made there. That'll teach me to be lazy. 😀
Interestingly (for me, at least), back in the day when I was using the unix tools, |
Beta Was this translation helpful? Give feedback.
-
True, the syntax is ambiguous, but the decision was made to treat it unambiguously following some pretty simple rules, for better or worse. The same kind of rules could be applied to |
Beta Was this translation helpful? Give feedback.
-
Would'nt it be a solution to require a finally block on nested single line try/catch blocks ? the finally block could act a bit like a try
try
...
catch (SomeException e)
...
finally;
catch
try
...
catch
try
...
catch (SomeException e)
...
catch (Exception e)
...
finally;
finally;
catch (Exception e)
...
finally; |
Beta Was this translation helpful? Give feedback.
-
Why is that better than requiring braces? In most cases it would end up being more verbose than just requiring the blocks. |
Beta Was this translation helpful? Give feedback.
-
Not nested -> finally not required try
...
catch
... Nested -> Requires Finally try
...
catch
...
catch
try
...
catch
...
catch
...
finally;
finally; It's a bit like ambiguous references. When there could be confusion you have to be verbose/explicit otherwise not. |
Beta Was this translation helpful? Give feedback.
-
I still don't think that improves the language in any way. It's not less verbose and now you have to know two sets of syntax and when to use which. The grammar also becomes dependent on the context in which it's used, making |
Beta Was this translation helpful? Give feedback.
-
@HaloFour I get your Point. |
Beta Was this translation helpful? Give feedback.
-
I wish this weren't legal syntax. Someone will inevitably write more lines of indented code (or copy/paste them) under This is precisely the reason behind the infamous Apple "goto fail" SSL bug. I would be ok with single-line unbraced statements such as if (condition) return true;
else return false; Unfortunately, allowing that also allows the unbraced multi-line version, given that it's a design principle of C# to be whitespace agnostic. |
Beta Was this translation helpful? Give feedback.
-
Oddly enough, I have never seen this happen and I drop the braces all the time. |
Beta Was this translation helpful? Give feedback.
-
That's the point. It's fairly invisible and not easy to notice. "Goto fail" was in the wild for several years on both iOS and OS X before being discovered and fixed. It can also be easily overlooked during merge. |
Beta Was this translation helpful? Give feedback.
-
@bondsbw I mean out of all the bugs I've diagnosed, none have been due to accidental misleading indentation/missing braces. Maybe it's a side effect of the auto formatting IDEs do out of the box these days. |
Beta Was this translation helpful? Give feedback.
-
@jnm2 Good point. |
Beta Was this translation helpful? Give feedback.
-
As for me I never drop brace when nested. But in the real world we almost not nest try/catch block, even more so for one line nested try/catch block. But one line try/catch is very common So I don't see any reason to go against this idea because ambiguity |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
It is personal preference. I personally find that the braces make for more verbose, harder to read syntax. It would be nice if this feature was optional to satisfy the dedicated "braces people" and people who are more "tabby." Why do we have brace-less if statements but require them for try/catch? It doesn't make much sense to me. |
Beta Was this translation helpful? Give feedback.
-
I tried to explore how |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Proposal: Add try/catch statement without curly braces
This syntax is already common for single line if/else stetements, foreach loops, for loops and using blocks. It would be convenient if the same pattern would be applied to the try/catch statement, because it would shorten a lot of code;
Single line if statement:
Single line try/catch statement:
VS.
Single line try/catch statement:
Beta Was this translation helpful? Give feedback.
All reactions