Parenthesis

Chris Nicholson-Sauls ibisbasenji at gmail.com
Fri Dec 22 13:49:22 PST 2006


Gregor Richards wrote:
> NN wrote:
>> What about removing unuseful parenthesis in 'if', 'for', 'while' ?
>> E.g. instead of:
>> if(a == b)
>> {
>>   while(c)
>>   {
>>      ...
>>   }
>> }
>> We can write:
>> if a == b
>> {
>>   while c
>>   {
>>      ...
>>   }
>> }
> 
> Does anybody have a spoon I can stab into my eyes? Thanks.
> 
>>
>> If someone writes parenthesis the code will compile too.
>> So this is almost backwards compatible feature.
> 
> And with only the price of making elegant code become inelegant, ugly 
> code! Fantastic!
> 
>>
>> What about 'if' with one expression ?
>> Almost everyone writes curly brackets for more than one expression, so 
>> there they will be always written, and there will be no bugs.
> 
> .... what? Most people write curly brackets if they have more than one 
> /statement/ (that's the word you're looking for), but with only one 
> simple statement I'd say it's more like a 50-50 split.
> 
>  - Gregor Richards

Personally, I almost always write in the braces.  It just makes things so much simpler if 
I later need to go back and expand that statement body, or want to add debugging/temporary 
output or checks of some kind.

That said, I'm not so sure D needs to drop those parentheses.  I don't even really 
consider them superfluous.  They are encapsulating what is a special case expression (or 
set of expressions, such as with for*each) whose meaning is dependent on the statement at 
hand.  So:

# for int i = 1; i < something; ++i {
#   doStuff();
# }

Ew.  And before someone mentions it, yes I have worked with languages that do things this 
way already, but most of them either have significant whitespace, significant newlines, or 
some other such syntactic anomaly which makes this feasible.  In other cases, it was a 
trap that had to be watched out for (expression ambiguities).  (To get around which, guess 
what, you add parentheses.)

-- Chris Nicholson-Sauls



More information about the Digitalmars-d mailing list