开发者

Why does C# require parens around conditionals?

I was just reading an SO question on Python, and noticed the lack of parentheses in a for-loop. Looked nice to me, then I wondered: why does C# require them?

For example, I currently need to write:

if (thing == stuff) {
}

and

foreach (var beyonce in allthesingleladies) {
}

So I am wondering why I can't write:

if thing == stuff {
}

Is there a syntactic ambiguity in that sta开发者_C百科tement that I am unaware of?

PS, funnily, braces can be optional for one-liners:

if (thing == stuff)
  dostuff();


The system needs some way to know when the condition stops and the statement starts. That means that either parens around the condition are optional, or the braces around the statement are optional. The designers of C# chose to make the braces optional (in the event that there's a single statement).

Consider this:

if x == 12 - x == 3 ? x++ : x--;

A parser could be written that can tell where the condition ends and the statement starts, but requiring the parentheses makes it substantially easier. In the case of my example, the grammar indicates that the full statement is if (x == 12) { -x == 3 ? x++ : x--; }. However, you don't know that the - is the beginning of the statement until you hit the second ==, which is already 3 tokens into it.

Since correctly parsing such examples requires looking an arbitrary number of tokens ahead in the input stream (and it arguably makes the code harder for humans to read), there's a good reason for requiring the parens.


It is a syntax 'feature' that goes back to C, and maybe to some language before that.

Essentially it was a choice between

if (condition) statement;

and

if condition then statement;

In the time when C was conceived, shorter notation and fewer keywords were the trend.


[I know I'm likely to her flamed for this but I've got to say it all the same]

As a predominantly vb programmer that's one thing which annoys me to the bone. In my opinion, parens should be optional and braces mandatory. If it's too much to ask then Microsoft could 'borrow' from vb into c# by introducing a c# equivalent of vb's Then.

But then why bother? Microsoft designed C# based on C, C++ and Java, all of which put the if condition in parens so why should c# be different?


Edit

Honestly, I think that based on C# 4's 'adoption of Optional and Named Parameters, a feature that has always been in VB (at least since VB6), the next release of C# (C# 5.0, that is) could as well introduce a 'new' feature so you do not have to type in unnecessary parentheses.

Since there's a ternary if condition ? truePart : falsePart, having if condition then something or for those who aren't into C# looking like vb, if condition do something wouldn't be a bad idea.


It's a language design choice that came from Java which in turn came from C++ which in turn came from C which in turn came from ???

Python don't need bracers as indention shows the intent. F# does the same. It's a good idea (IMO).


BTW, if I had my druthers, I would allow one operator outside the parens: a leading exclamation point. It irks me that code testing for complicated negative conditions ends up with an extra set of parentheses compared with code testing for complicated positive ones.


Simply put, it is due to how C/C++/C# handles white space. You need some kind of delimiter since statements can span multiple lines. You also need a way to "block" or group statements together into a block.

  • C/C++/C# use ()'s, {}'s
  • VB/VB.NET/VBScript/VBA use Function/End Function, Sub/End Sub, If/Then/Else/Else If/End If, and so on. (For blocking only, since VB based languages do not support statements spanning multiple lines.)

If your specific question, you are using ()'s to create an expression. Therefore, you need a way to tell the compiler you are done with the expression, since the {} (blocking delimiters) are not required.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜