anything fundamentally wrong with...?

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

SchighSchagh
Posts: 24
Joined: Thu Jan 15, 2009 8:16 am UTC

anything fundamentally wrong with...?

Postby SchighSchagh » Wed Feb 04, 2009 12:00 am UTC

is there anything fundamentally wrong with using commas to string together what would otherwise be separate statements and ending the "block" with a semi instead of the usual way of grouping statements in braces so that a control keyword knows which block you want executed?
eg,

Code: Select all

if(condition)
   statement1,
   statement2,
   statement3;

instead of

Code: Select all

if(condition)
{
   statement1;
   statement2;
   statement3;
}


this came up while i was programming some C just now, but the same question applies to C++, Java, etc.

qbg
Posts: 586
Joined: Tue Dec 18, 2007 3:37 pm UTC

Re: anything fundamentally wrong with...?

Postby qbg » Wed Feb 04, 2009 12:23 am UTC

You'd better have a good reason for doing so. (as it isn't idiomatic and therefore hurts the ability of the code to communicate to the reader)

stephentyrone
Posts: 778
Joined: Mon Aug 11, 2008 10:58 pm UTC
Location: Palo Alto, CA

Re: anything fundamentally wrong with...?

Postby stephentyrone » Wed Feb 04, 2009 1:23 am UTC

It's horribly non-idiomatic and makes your code less readable.

Do you need a better reason?
GENERATION -16 + 31i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

Lightnix
Posts: 4
Joined: Tue Feb 03, 2009 6:54 pm UTC

Re: anything fundamentally wrong with...?

Postby Lightnix » Wed Feb 04, 2009 1:41 am UTC

Nested statements would be pretty hilarious. Not to any other readers of the code, they'd find it annoying. You'd be introducing syntax for the sake of saving two characters, as far as I can tell, no point.

User avatar
Why Two Kay
Posts: 266
Joined: Sun Mar 23, 2008 6:25 pm UTC
Location: Plano, TX
Contact:

Re: anything fundamentally wrong with...?

Postby Why Two Kay » Wed Feb 04, 2009 1:47 am UTC

Braces help for syntax highlighting in a good editor program. They help keep things organized and nobody doubts what they mean, even if you are unfamiliar with the language in particular. Plus you can copy/paste such code into terribly-formatted forum posts and people can still kinda sorta read it.

Python on the other hand, must keep indentations, which I found outrageous and against my style of formatting code. (Or maybe it's just my FEAR of CHANGE.)
tl;dr - I said nothing important.

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: anything fundamentally wrong with...?

Postby Rysto » Wed Feb 04, 2009 1:52 am UTC

Yes, there is. The comma is an operator in that context: it only works on expressions. This means that you can't use if statements or loops there.

Plus, that kind of thing would be very confusing to anybody else who has to read your code.

Also, Java does not have the comma operator.

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: anything fundamentally wrong with...?

Postby Berengal » Wed Feb 04, 2009 2:31 am UTC

There's nothing fundamentally wrong with using commas to string together statements. In fact, prolog does so (the comma is actually the 'and' operator, as prolog only deals in true and false). It is wrong to do so in C though.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
phlip
Restorer of Worlds
Posts: 7572
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: anything fundamentally wrong with...?

Postby phlip » Wed Feb 04, 2009 2:56 am UTC

Nesting conditionals is easy, you just have to go the whole way with "everything is an expression"... and turn

Code: Select all

if (condition1)
{
  statement1;
  if (condition2) statement2;
  statement3;
}
into

Code: Select all

(condition1) &&
(
  statement1,
  (condition2) && (statement2),
  statement3
);
Of course, then you're just replacing your braces with parens, rather than getting rid of them entirely... Loops, however, would be much more difficult.

That said, this is a Bad Idea for the reasons already listed... it's non-idiomatic, harder to read, easier to break (just add a line with a semicolon instead of a comma, or vice-versa...), and particularly ugly. There are only a small handful of places you should ever use the comma operator, and they're all rare special-cases. Like all patterns of coding that aren't the norm, you need to get some significant benefit in terms of readability or productivity or suchlike to outweigh the loss in readability that comes with doing anything unexpected... and this doesn't have that.

In short: no, there's nothing fundamentally wrong with it... it'll compile to be functionally identical, if not exactly the same opcodes... but still, don't do it, it's confusing and gains you nothing.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
PM 2Ring
Posts: 3713
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: anything fundamentally wrong with...?

Postby PM 2Ring » Wed Feb 04, 2009 12:01 pm UTC

SchighSchagh wrote:is there anything fundamentally wrong with using commas to string together what would otherwise be separate statements and ending the "block" with a semi


I'll admit I was tempted to do this a few times, when I was young & foolish. :) And lived to regret it...

I recommend avoiding this sort of fragile construction for the reasons already mentioned, unless you're writing intentionally obfuscated code.

User avatar
Cosmologicon
Posts: 1806
Joined: Sat Nov 25, 2006 9:47 am UTC
Location: Cambridge MA USA
Contact:

Re: anything fundamentally wrong with...?

Postby Cosmologicon » Wed Feb 04, 2009 5:30 pm UTC

Are there any morally legitimate uses (no matter how far-fetched) of the comma operator, except in the first and third statements of a for loop? You know, like this:

Code: Select all

for (i = 0, j = n; i < n; ++i, --j)

LikwidCirkel
Posts: 169
Joined: Thu Nov 08, 2007 8:56 pm UTC
Location: on this forum (duh)
Contact:

Re: anything fundamentally wrong with...?

Postby LikwidCirkel » Wed Feb 04, 2009 5:57 pm UTC

phlip wrote:

Code: Select all

(condition1) &&
(
  statement1,
  (condition2) && (statement2),
  statement3
);


Does c actually guarantee that in the case of multiple expressions separated by "&&", the expressions get evaluated until one is false, and the rest don't? I know that all major compilers do this, but I don't believe it's guaranteed by the language.. but I could be wrong.

ossix
Posts: 25
Joined: Sat Mar 08, 2008 12:36 pm UTC

Re: anything fundamentally wrong with...?

Postby ossix » Wed Feb 04, 2009 6:14 pm UTC

AFAIR it's mandatory in ANSI C.

Vempele
Posts: 69
Joined: Wed Nov 14, 2007 8:05 am UTC

Re: anything fundamentally wrong with...?

Postby Vempele » Wed Feb 04, 2009 7:10 pm UTC

LikwidCirkel wrote:Does c actually guarantee that in the case of multiple expressions separated by "&&", the expressions get evaluated until one is false, and the rest don't? I know that all major compilers do this, but I don't believe it's guaranteed by the language.. but I could be wrong.

Read up on sequence points.

Note most modern processors only guarantee that if stuff gets executed in ways wasn't supposed to, nobody will know the difference (except in bed). :D
const int ALMOST_FIFTY = 80;

User avatar
Emu*
Posts: 689
Joined: Mon Apr 28, 2008 9:47 am UTC
Location: Cardiff, UK
Contact:

Re: anything fundamentally wrong with...?

Postby Emu* » Wed Feb 04, 2009 7:27 pm UTC

I think relying on the peculiarities of lazy evaluation to make sure stuff happens in your program is just asking for trouble.
Cosmologicon wrote:Emu* implemented a naive east-first strategy and ran it for an hour, producing results that rivaled many sophisticated strategies, visiting 614 cells. For this, Emu* is awarded Best Deterministic Algorithm!

User avatar
phlip
Restorer of Worlds
Posts: 7572
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: anything fundamentally wrong with...?

Postby phlip » Wed Feb 04, 2009 10:45 pm UTC

Cosmologicon wrote:Are there any morally legitimate uses (no matter how far-fetched) of the comma operator, except in the first and third statements of a for loop?

There are a couple... one is if you want to put more than one statement in a #define macro... inasmuch as #define macros are morally legitimate in the first place, of course.

If you do:

Code: Select all

#define macro(a) statement1(a); statement2(a)

if (condition)
  macro(a);
Then only statement1 is conditional. Throw in braces:

Code: Select all

#define macro(a) { statement1(a); statement2(a); }

if (condition)
  macro(a);
else
  otherstuff();
Then the else is a syntax error, because of the extra semicolon (it's "<if statement> <null statement> <else clause>", which isn't any good). The two different idioms to get it to do what you want are:

Code: Select all

#define macro(a) (statement1(a), statement2(a))
#define macro(a) do { statement1(a); statement2(a); } while(0)

As both will take a semicolon on the end to become a single statement that runs the two statements. Of course, only the first of the two uses commas (and more complicated ones will use &&, || and ?: for conditionals), and the latter is arguably easier to read, once you figure out the "do while false" idiom.

The other place is the assert() macro... if you're using an implementation that displays the condition that failed as part of the error message, you can use the comma operator to throw in explanatory messages:

Code: Select all

assert(("Couldn't open the file", fp != NULL));
This will show an error message like "Assertion failed: ("Couldn't open the file", fp != NULL)" if the file pointer is NULL. Again, inasmuch as assert() is morally legitimate in the first place.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: anything fundamentally wrong with...?

Postby Rysto » Wed Feb 04, 2009 11:36 pm UTC

Edit: Never mind, I misunderstood a post.

User avatar
Cosmologicon
Posts: 1806
Joined: Sat Nov 25, 2006 9:47 am UTC
Location: Cambridge MA USA
Contact:

Re: anything fundamentally wrong with...?

Postby Cosmologicon » Thu Feb 05, 2009 12:40 am UTC

Good ones, phlip. Thanks!

User avatar
Emu*
Posts: 689
Joined: Mon Apr 28, 2008 9:47 am UTC
Location: Cardiff, UK
Contact:

Re: anything fundamentally wrong with...?

Postby Emu* » Thu Feb 05, 2009 12:53 am UTC

I'd say assertions are less useful than exceptions (try... catch) since assertions are meant to be for situations that are meant never to happen. They're a useful way of setting steppingstones in your algorithms, but assertions shouldn't be relied upon in the same way try{}catch(){} is in Java...
Cosmologicon wrote:Emu* implemented a naive east-first strategy and ran it for an hour, producing results that rivaled many sophisticated strategies, visiting 614 cells. For this, Emu* is awarded Best Deterministic Algorithm!

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: anything fundamentally wrong with...?

Postby Rysto » Thu Feb 05, 2009 1:17 am UTC

They definitely shouldn't, but when you have a huge piece of code littered with (correct) asserts, an error message when an assert trips is incredibly useful.

However, I'd prefer to write a new assert macro that accepts a printf format string + arguments and fprintfs all that to stderr, then that comma operator trick with assert().

mrkite
Posts: 336
Joined: Tue Sep 04, 2007 8:48 pm UTC

Re: anything fundamentally wrong with...?

Postby mrkite » Thu Feb 05, 2009 6:30 am UTC

LikwidCirkel wrote:Does c actually guarantee that in the case of multiple expressions separated by "&&", the expressions get evaluated until one is false, and the rest don't? I know that all major compilers do this, but I don't believe it's guaranteed by the language.. but I could be wrong.


Yes, it's called "short circuiting" and it's guaranteed in C, C++, C# and Java.

Goplat
Posts: 490
Joined: Sun Mar 04, 2007 11:41 pm UTC

Re: anything fundamentally wrong with...?

Postby Goplat » Fri Feb 06, 2009 7:29 am UTC

Cosmologicon wrote:Are there any morally legitimate uses (no matter how far-fetched) of the comma operator, except in the first and third statements of a for loop?
Sure, it's also useful in loop conditions sometimes. Here's an example from a program I wrote that involved parsing arithmetic expressions:

Code: Select all

while ((op = E_ADD, *parse_ptr == '+') ||
       (op = E_SUB, *parse_ptr == '-'))
{

User avatar
Yakk
Poster with most posts but no title.
Posts: 11128
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: anything fundamentally wrong with...?

Postby Yakk » Fri Feb 06, 2009 5:31 pm UTC

The assert trick: I often do assert( expression && "Description of what went wrong" );

As a side benefit, this places the asserted condition near the front of the line, instead of at the end.
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

User avatar
Area Man
Posts: 256
Joined: Thu Dec 25, 2008 8:08 pm UTC
Location: Local

Re: anything fundamentally wrong with...?

Postby Area Man » Fri Feb 06, 2009 8:38 pm UTC

Cosmologicon wrote:Are there any morally legitimate uses (no matter how far-fetched) of the comma operator [...]

The comma operator is overloadable and some boost libraries (assign, spirit) make use of this.
Bisquick boxes are a dead medium.

User avatar
b.i.o
Green is the loneliest number
Posts: 2519
Joined: Fri Jul 27, 2007 4:38 pm UTC
Location: Hong Kong

Re: anything fundamentally wrong with...?

Postby b.i.o » Fri Feb 06, 2009 10:18 pm UTC

Emu* wrote:I think relying on the peculiarities of lazy evaluation to make sure stuff happens in your program is just asking for trouble.

Well, it is in C.

User avatar
PM 2Ring
Posts: 3713
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: anything fundamentally wrong with...?

Postby PM 2Ring » Sat Feb 07, 2009 10:15 am UTC

Asserts should not be active once the program has been released. They should only be active during the development phase.

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: anything fundamentally wrong with...?

Postby Berengal » Sat Feb 07, 2009 2:36 pm UTC

Asserts are usually conditionally defined, so in release code it's nothing.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
PM 2Ring
Posts: 3713
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: anything fundamentally wrong with...?

Postby PM 2Ring » Sat Feb 07, 2009 3:43 pm UTC

Berengal wrote:Asserts are usually conditionally defined, so in release code it's nothing.


Exactly. So thinking of them as C's version of try...catch is somewhat misleading.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 11 guests