Comment On A Dash of Comma Spice

Yesterday we saw the old, today we see the obscure, maybe obscure is a bit strong, Dennis G sent me this rather interesting pattern he came across in a product he was maintaining, I didn't quite get it ... [expand full text]
« PrevPage 1Next »

Re: A Dash of Comma Spice

2005-03-16 14:05 • by
Using memcmp instead of strcmp is nice too.  And comparing seven
bytes when you've provided eight is bound to confuse the poor kid who
doesn't understand why it's matching "CBLECO11" as well.

Re: A Dash of Comma Spice

2005-03-16 14:14 • by
This is indeed harder to maintain because the comma operator is more
fragile than curly braces would be - for example, if you change the
order by cutting or pasting, try to insert an if block, when order of
execution becomes relevant etc. - and it is a WTF because the usual
convention is to use curly braces when possible.



However, I hope no C or C++ programmer would have trouble understanding
it. Even though I hate it, the comma operator was especially tailored
to make it possible to put multiple expressions into one statement,
like in this code snippet:

for (int i = 0, y = 0; i < 5; ++i, y += 30)

  drawText(lines[i], x, y);

I wouldn't write code like that, maybe some people do - at least it was
explicitly made possible to do so by the language designers. Otherwise,
there would be no such thing as the "comma operator", and for() could
also have been designed more simply.

Re: A Dash of Comma Spice

2005-03-16 14:16 • by
31291 in reply to 31289
I guess hdr.cName is a static array which is not necessarily zero
terminated, so using strcmp would be even worse. However, the 7/8 byte
thing is a nice mini-wtf.

Re: A Dash of Comma Spice

2005-03-16 14:21 • by
The wtf is that they're comparing the first seven characters of an eight character string.

Re: A Dash of Comma Spice

2005-03-16 14:40 • by fluffy
31293 in reply to 31291
:
I guess hdr.cName is a static array which is not necessarily zero
terminated, so using strcmp would be even worse. However, the 7/8 byte
thing is a nice mini-wtf.





strncmp would work nicely though. (but in that case you'd might as well just use memcmp anyway.)

Re: A Dash of Comma Spice

2005-03-16 15:33 • by
Someone seemed to be implying that the use of the comma operator might
prove problematic when order of operations becomes relevant. Use of the
comma operator involves a sequence between the evaluation of its
operands (similar to the || and && operators). This provides
the correct ordering semantics that would match normal statement use.
Of course you couldn't put an if statement in these comma based pseudo
stataments - but you could use a ternary (although there's a good
question: does the ternary have a similar sequence point, I imagine it
should, but I've never looked).

Re: A Dash of Comma Spice

2005-03-16 16:32 • by

Obligatory abuse of valid C syntax link:


http://catb.org/~esr/jargon/html/D/Duffs-device.html

Re: A Dash of Comma Spice

2005-03-16 17:21 • by JamesCurran

The first time I saw something like that it was like this:


   if (comparision)
      x=y,z++;


Knowing how the comma operator works (each term evaluated left to right, with the entire expression equal to the rightmost), I assumed it was evaluated as:


   x = (y,z++);


which would set x equal to the value of z before it was incremented.  However, the code only made sense if the x was set to y.  Eventually, I realized the assigment has a higher precedence than comma, so that it was being evaluated as:


   (x=y), (z++);


(so that if you'd written w = (x=y,z++)  x would be set equal to y, w would be set equal to z!)


SO, this means that not only is he using an obscure operator, but also an obscure precedence rule involving that obscure operator.


 



   


 

Re: A Dash of Comma Spice

2005-03-16 21:41 • by
This one isn't as funny as the others, but my question would be the
most direct one.. why are there 3 arrays.. holding VERY similar
values?  recttls[F_CATEG].  Also storing that extra byte in
"Primary User: ".  That space is totally unnecessary in the
array.  The comma delimiter doesn't bother me as much as
those.  At least this guy didn't pull..

    if (memcmp(hdr.cname, "CBLEC010", 7) == false)



Either way.. it's not as bad as others I've seen.  To be perfectly
honest, what i hate about some of the c++ programmers I know is stuff
like

    if (!true /* hardcoded, but this is just an example
*/ && !false /* stupid notes don't belong in the middle of
statements */)



   

Re: A Dash of Comma Spice

2005-03-16 21:57 • by Bustaz Kool
31300 in reply to 31294

:
does the ternary have a similar sequence point, I imagine it should, but I've never looked).


Yes, is the short answer.  The Condition gets evaluated first and completely.  Next EITHER the true or the false clause gets executed but not both.


a ? b : c


I did run into a usage of the comma operator back in the day.  There was a database (possibly R:Base?) where you had to make a separate application call to get the results of an operation and the status was returned as a parameter.  In order to loop through the results you couldn't simply say "while not EOF" so the code became:


while (GetNextRecord(blah, blah),


    GetStatus(&MyStatus),


     Status <> EOF) {


    ... <do stuff with data> ...


    }


This seems pretty bizarre by today's standards but rest assured it seemed pretty bizarre twenty years ago, too.

Re: A Dash of Comma Spice

2005-03-17 01:05 • by
31303 in reply to 31300
Hey, I kind of like that syntax, I may use it:

while ( hResult = GetNext( &data ), hResult == S_OK ) {

    DoSomething( &data );

}



I think that looks better than:

while ( ( hResult = GetNext( &data ) ) == S_OK ) {


    DoSomething( &data );


}



Re: A Dash of Comma Spice

2005-03-17 03:21 • by V.
Personally I think that you can do a lot of wtf 's in C/C++ which are "correct".

Think about inconsistanty, no descent O-O, no descent naming (that one is not only bound to C/C++ [:)])



about using memcpy (don't know the function really well), I don't think
you can tell, what is hdr.cname? (char array, byte array, string,
CString, ...)?  My experience thought me that comparing string of
different format is not always so easy.



The wtf here could be unclear code.

    I hate it when people don't use {} after if/else/for/...

    , instead of {} or three times ;

    no comment?



ah well, [:#] now [;)]

Re: A Dash of Comma Spice

2005-03-17 07:30 • by
31307 in reply to 31297
JamesCurran:
SO, this means
that not only is he using an obscure operator, but also an obscure
precedence rule involving that obscure operator.

I
think that
just means you don't understand the precedence rules, not that they're
obscure.

I found the snip a little exquisite, but there was nothing I didn't understand.

The only WTF is the
discrepance of char lenghts. which I didn't spot myself. The guy was
probably trying to "subtract" the NUL which string constants
automatically have, except that he didn't count it in the first place.
This really should have been

#define MAGIC "CBLECO10"
if( !memcmp( hdr.cname, MAGIC, sizeof( MAGIC ) - 1 ) )

and of course MAGIC should be a meaningful name so that the code would be self-documenting too.

All in all, not really a WTF at all.

Re: A Dash of Comma Spice

2005-03-17 07:35 • by
31308 in reply to 31303
:
Hey, I kind of like that syntax, I may use it:

while ( hResult = GetNext( &data ), hResult == S_OK ) {

    DoSomething( &data );

}



I think that looks better than:

while ( ( hResult = GetNext( &data ) ) == S_OK ) {


    DoSomething( &data );


}



for( hResult = GetNext( &data ) ; hResult == S_OK ; hResult = GetNext( &data ) )
DoSomething( &data );

Re: A Dash of Comma Spice

2005-03-18 10:26 • by JamesCurran
31345 in reply to 31307

:
I think that just means you don't understand the precedence rules, not that they're obscure.


The "real" precedence rules were defined by mathematicians centuries before there were computer languages : "Multiplication & Division is done before Addition & Subtraction".


Any other precedence rule is, by definition, obscure. 


 

Re: A Dash of Comma Spice

2005-03-18 19:54 • by
31375 in reply to 31345
JamesCurran:

  wrote:
I think that just means you don't understand the precedence rules, not that they're obscure.


The "real" precedence rules were defined by mathematicians centuries before there were computer languages : "Multiplication & Division is done before Addition & Subtraction".


Any other precedence rule is, by definition, obscure. 


 

Re: A Dash of Comma Spice

2005-03-23 05:12 • by Lars
31591 in reply to 31308
Anonymous:
:
Hey, I kind of like that syntax, I may use it:

while ( hResult = GetNext( &data ), hResult == S_OK ) {

    DoSomething( &data );

}



I think that looks better than:

while ( ( hResult = GetNext( &data ) ) == S_OK ) {


    DoSomething( &data );


}



for( hResult = GetNext( &data ) ; hResult == S_OK ; hResult = GetNext( &data ) )
DoSomething( &data );


You just violated the DRY principle. I think that the original poster's is a valid idiomatic use of the comma operator.

Re: A Dash of Comma Spice

2006-02-17 14:49 • by chrismcb
60710 in reply to 31375
Anonymous:

The "real" precedence rules were defined by mathematicians centuries before there were computer languages : "Multiplication & Division is done before Addition & Subtraction".


Any other precedence rule is, by definition, obscure. 



 


I looked up the defintion of obsure and it didn't say anything about precednce rules. The comma and terenary precedence is listed on page 52 along with all of the other C precedences.

Re: A Dash of Comma Spice

2006-02-22 09:51 • by GoatCheez
61188 in reply to 31296
Anonymous:

Obligatory abuse of valid C syntax link:


http://catb.org/~esr/jargon/html/D/Duffs-device.html



Duff's device is one of the coolest things ever realized about C. Too bad it doesn't carry over to C#. A LOT of really good code uses duff's device. I know there's a very lightweight thread implementation (protothreads?) that uses it. I also heard that some really popular open source libraries use it for speed enhancements also.

Unemployed? Out of work? Sober? You sat
around the house all day, but now it's Duff time! Duff, the beer that
makes the days fly by!

Re: A Dash of Comma Spice

2009-05-18 03:44 • by wow gold (unregistered)
Out of runes of magic gold? Need it in urgent? Yes, I can understand you. As the most important currency, without rom gold, you ever can’t do anything. So you need to buy rom gold from those most professional and loyal game online shops with years’ experience and have a good reputation among players. Is there any difficult? No, when you need the rom gold, please feel free to contact us, we are promising to offer you the cheap runes of magic gold with fastest delivery. Moreover, we are online 24/7, you can contact us any time with any question about. So why are you still irresolute? Come here to grab your cheap runes of magic gold now. Crazy about running warhammer gold? Yup, it is so crucial indeed for us in Warhammer Online. Without it, we can even do nothing, without money to buy items, weapons and so on. So enough warhammer gold is substantial.

Re: A Dash of Comma Spice

2009-06-24 05:08 • by Karl (unregistered)
just counting letters - maybe i cannot count anymore - why 7?

if (!memcmp(hdr.cname,"CBLECO10",7))
« PrevPage 1Next »

Add Comment