tags:

views:

199

answers:

6

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 statement that I am unaware of?

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

if (thing == stuff)
  dostuff();
A: 

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).

FuleSnabel
Python requires a colon to indicate the beginning of the enclosed statement; F# uses `do` or `then`.
Gabe
C came from [B](http://en.wikipedia.org/wiki/B_%28programming_language%29)
Matt Ellen
+8  A: 

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.

Gabe
You're not wrong, but according to the BNF, there's *always* a single statement in the "then" clause. The trick is that it can be a composite statement, with some number of statements contained between its curlies.
Steven Sudit
This is true. However, it still doesn't answer the question why not *either* can be ommitted, as long as *both* are not. Neither of the OP's last two examples are ambiguous - why is only one of them allowed? (Asked more as a rhetorical question...)
Tomas Lycken
ah, good point. i thought the braces thing was just an interesting piece of trivia -- but you're right, you can't have optional braces *and* optional parens.
Matt Sherman
One could have optional braces and parens if there were some other delimiter before the next statement (like "Then"). The fact that some operators exist in unary and binary form does allow some examples to be truly ambiguous. Something like "if x+y==3 " could evaluate as "if (((x+y)==3) " or as "if (x) ((+y==3) " Different execution semantics.
supercat
+1  A: 

[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.

Alex Essilfie
As a former VB programmer, I too would have found parens (and braces, for that matter) irritating. But as a reformed C# programmer, I find that, for me, braces and parentheses make reading the code easier, because they act visually as fence posts (parens and braces are visually distinct from words). Today I find reading VB uncomfortable, but I suppose it comes down to what you are used to (or currently using, perhaps).
Robert Harvey
I agree that I find punctuation makes it easier to read. Languages that use words for punctuation (like COBOL or VB) are harder to read.
Gabe
VB has parens too, the Then keyword. Using punctuation instead of keywords is merely a style preference.
Hans Passant
@Hans: It wouldn't surprise me if unnecessary use of parenthesis is considered bad style in VB.
Robert Harvey
@Robert, I really dislike unnecessary parens in C# and C++ as well. Never recovered from Lisp.
Hans Passant
@Robert: Actually unnecessary use of parentheses in VB is not the way to go for VB programmers. For instance we can simply call `Form.Show` instead of the C-ish style which requires the parentheses like so: `Form.Show()`.This is one of the reasons I find it troublesome coding in C# and I try to avoid it as much as possible. One doesn't need any parentheses to show that `Form.Show` is being called without any parameters.As I see it, VB was designed to avoid parentheses whenever possible.
Alex Essilfie
@Alex: Ah, yes. An empty set of parentheses at the end of an identifier is instantly recognizable by C# developers the world over as indicating the *calling of a method.* (as opposed to a property, variable or some other member). This is an important enough piece of information that having the visual indicator can be quite valuable when reading code. In addition, calling a method can be done *only one way* in C# (using parentheses); this arguably makes the language (and the ability to read it without friction) more consistent.
Robert Harvey
+3  A: 

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.

Henk Holterman
A: 

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.

supercat
I agree, `if!` is a nice, shorter way of saying `unless`.
Gabe
A: 

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.

AMissico