Jump to page: 1 24  
Page
Thread overview
Parenthesis
Dec 22, 2006
NN
Dec 22, 2006
Gregor Richards
Dec 22, 2006
Kristian Kilpi
Dec 22, 2006
Wolven
Dec 23, 2006
Steve Horne
Dec 23, 2006
Wolven
Dec 23, 2006
Kristian Kilpi
Dec 24, 2006
Steve Horne
Dec 28, 2006
NN
Dec 29, 2006
Wolven
Dec 23, 2006
NN
Dec 23, 2006
Steve Horne
Dec 23, 2006
Kevin Bealer
Dec 23, 2006
Kristian Kilpi
Dec 23, 2006
NN
Dec 23, 2006
Alexander Panek
Dec 23, 2006
NN
Dec 23, 2006
Alexander Panek
Dec 23, 2006
NN
Dec 23, 2006
Alexander Panek
Dec 29, 2006
Walter Bright
Dec 29, 2006
Wolven
Dec 29, 2006
Walter Bright
Dec 29, 2006
Waldemar
Dec 29, 2006
Walter Bright
Dec 29, 2006
Sean Kelly
Dec 29, 2006
Sean Kelly
Jan 01, 2007
Bruno Medeiros
Jan 01, 2007
Georg Wrede
Jan 01, 2007
%u
Jan 13, 2007
Bruno Medeiros
Dec 29, 2006
John Demme
Dec 29, 2006
NN
December 22, 2006
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
  {
     ...
  }
}

If someone writes parenthesis the code will compile too.
So this is almost backwards compatible feature.

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.
December 22, 2006
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
December 22, 2006
On Fri, 22 Dec 2006 19:08:31 +0200, Gregor Richards <Richards@codu.org> 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

Well, well, it seems that Gregor is a pure C/C++/D syntax kind of a guy... :)

Ugliness is in the eye of the beholder, as we all know. It's a matter of opinion. If one uses the "if a == b" syntax a long time, (s)he will very likely get used to it, and no longer think it's ugly.

I have programmed with C++ mainly, but "if a == b" is not so ugly, actually it's kind of okay for me (not so elegant, sure, but okay). Even if I always write return statements insided parenthesis. Ah yes, the return statements... you can write "return a" or "return(a)". So it's not so strange if that would apply to other statements also.

But I think that NN suggested the syntax not because it's prettier but because it's easier/faster to type.

I did some testing. I use a scandinavian keyboard: 'Shift + 8' -> '(' and 'Shift + 9' -> ')'. I wrote "if a == 1" ten times, and then "if(a == 1)" (without the quotation marks):

"if a == 1" : 21 secs.
"if(a == 1)" : 29 secs.

(The starting and stopping of my timer took some time.) So, "if a == 1" is 28% faster than "if(a == 1)"...
December 22, 2006
Kristian Kilpi wrote:

> On Fri, 22 Dec 2006 19:08:31 +0200, Gregor Richards <Richards@codu.org> wrote:
> 
>> NN wrote:
>>> What about removing unuseful parenthesis in 'if', 'for', 'while' ?

NEVA! :)

> I did some testing. I use a scandinavian keyboard: 'Shift + 8' -> '(' and
> 'Shift + 9' -> ')'. I wrote "if a == 1" ten times, and then "if(a == 1)"
> (without the quotation marks):
> 
> "if a == 1" : 21 secs.
> "if(a == 1)" : 29 secs.
> 
> (The starting and stopping of my timer took some time.) So, "if a == 1" is
> 28% faster than "if(a == 1)"...

Good code is written once and read for years.
December 22, 2006
== Quote from Gregor Richards (Richards@codu.org)'s article
> 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


"Elegance" is in the eyes of the beholder...   I agree with the original poster. All the meaningless (or not really necessary) parenthesis are, in my eyes, hideous.  Along with the curly braces, and other C style syntax.  I'm sure shorthand looks "elegant", to those that read and write it.  To the rest of us, it looks like scribbling... much like C style syntax.  Of course, I'll at least admit I'm biased.
December 22, 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
December 23, 2006
Others have commented on aesthetics.  I think the real problem is the syntax. Dropping the parenthesis makes it ambiguous.

if a == b * c - d;

is it:

if (a == b * c) { - d };

or

if (a == b) { * c - d };

Kevin
December 23, 2006
On Fri, 22 Dec 2006 21:02:22 +0000 (UTC), Wolven <rma@wolven.net>
wrote:

>"Elegance" is in the eyes of the beholder...   I agree with the original poster.

I agree. Pascal-family language advocates have often ridiculed C-family language advacates for the inelegancy of needing those parens.

Personally, I don't much care whether block statements have mandatory parens or not, and that shorthand-everything logic has caused significant problems in C (though the only approach I personally prefer to brace-based blocks are the indentation-based blocks in Python).

But I do agree that single-statement conditionals are far from rare, and often don't get the braces treatment, and this is also a good thing. Redundant braces look at least as stupid as redundant parens.

You basically can't always have everything. The C and Pascal styles for block structures have different advantages and disadvantages. Trying to get the best of both worlds risks bringing in a whole bunch of new problems...

  if a == b        //  fine
  {
    ...
  }

  if a == b return c;  //  errm - well...

  if a == b *c++;       //  aaarrrggghhh!!!

So, since the best-of-both-worlds doesn't work, making the brackets optional implies making the braces non-optional. For every happy person there's an angry person. What's the point?

Of course, we could add a Python-style colon. Pascal and Basic advocates would generally advocate keywords in place of colons, but they'd advocate different keywords for different block structures (if ... then, while ... do, etc etc) so a single consistent symbol sounds good to me for reasons that have nothing to do with shorthand.

There's always an alternative syntax, in other words. But this isn't D any more. It's a whole new language. With its own advantages and disadvantages. I seem to recall a recurring suggestion on comp.lang.python that the colons for block structures should be made optional, for instance. And the indentation-haters are always suggesting that brace-based blocks should be added.

Language designers have been messing about with block structure syntax for decades. Innovation can still be worthwhile, but if there was a single perfect approach that would keep everyone happy, I think someone would have found it by now.

-- 
Remove 'wants' and 'nospam' from e-mail.
December 23, 2006
On Fri, 22 Dec 2006 15:49:22 -0600, Chris Nicholson-Sauls <ibisbasenji@gmail.com> wrote:

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

To me, redundant braces are extra clutter, making it harder to read the code. I used to always use them in the past, back when I was more Pascal-family-influenced than now, but over time I just got irritated with them. And leaving them out has never caused a problem.

The basic rule is that if I only drop the braces if the whole block statement is on a single line - a useful way to tidy up code, and something that can be done similarly in Pascal, but not really Modula-2 or Ada due to begin/end issues.

Of course the amount of clutter we're talking about from braces is a trivial issue. Just like adding in the braces when they are needed is a trivial issue.

Not that anything above should influence anyone one way or the other. The point is that both views are equally valid. There never will be a definitive right way.

-- 
Remove 'wants' and 'nospam' from e-mail.
December 23, 2006
On Sat, 23 Dec 2006 04:25:09 +0200, Kevin Bealer <kevinbealer@gmail.com> wrote:

> Others have commented on aesthetics.  I think the real problem is the syntax.
> Dropping the parenthesis makes it ambiguous.
>
> if a == b * c - d;
>
> is it:
>
> if (a == b * c) { - d };
>
> or
>
> if (a == b) { * c - d };
>
> Kevin


So, if one chooses not to use the parenthesis, (s)he must use the braces.

Or, a new keyword 'then' (e.g. "if a == b * c - d then")... Errr...


Ok, how the following is interpreted (assuming the parenthesis are optional and 'then' is not used)?

if (a + 1) * c {...}

->

if((a + 1) * c) {...}

or

if(a + 1) {* c} {...}

... maybe we should stick with the original parenthesis syntax... ;)
« First   ‹ Prev
1 2 3 4