• (unregistered)

    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.

  • (unregistered)

    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.

  • (unregistered) in reply to

    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.

  • (unregistered)

    The wtf is that they're comparing the first seven characters of an eight character string.

  • (cs) in reply to
    :
    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.)
  • (unregistered)

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

  • (unregistered)

    Obligatory abuse of valid C syntax link:

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

  • (cs)

    <FONT style="BACKGROUND-COLOR: #efefef">The first time I saw something like that it was like this:</FONT>

       <FONT style="BACKGROUND-COLOR: #efefef">if (comparision)
          x=y,z++;</FONT>

    <FONT style="BACKGROUND-COLOR: #efefef">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:</FONT>

    <FONT style="BACKGROUND-COLOR: #efefef">   x = (y,z++);</FONT>

    <FONT style="BACKGROUND-COLOR: #efefef">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:</FONT>

    <FONT style="BACKGROUND-COLOR: #efefef">   (x=y), (z++);</FONT>

    <FONT style="BACKGROUND-COLOR: #efefef">(so that if you'd written w = (x=y,z++)  x would be set equal to y, w would be set equal to z!)</FONT>

    <FONT style="BACKGROUND-COLOR: #efefef">SO, this means that not only is he using an obscure operator, but also an obscure precedence rule involving that obscure operator.</FONT>

    <FONT style="BACKGROUND-COLOR: #efefef"> 


       </FONT>

    <FONT style="BACKGROUND-COLOR: #efefef"></FONT> 

  • (unregistered)

    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 */)

       

  • (cs) in reply to

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

  • (unregistered) in reply to Bustaz Kool

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

    }

  • (cs)

    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 [;)]

  • (unregistered) in reply to JamesCurran
    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.

  • (unregistered) in reply to
    :
    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 );
  • (cs) in reply to

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

     

  • (unregistered) in reply to JamesCurran
    JamesCurran:

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

     

  • Lars (unregistered) in reply to
    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.
  • (cs) in reply to
    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.

  • (cs) in reply to
    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.

    <font style="color: rgb(0, 0, 0); font-weight: bold;" color="cyan" size="4">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!</font>
  • Karl (unregistered)

    just counting letters - maybe i cannot count anymore - why 7?

    if (!memcmp(hdr.cname,"CBLECO10",7))

  • The Amazing Ham (unregistered) in reply to
    :
    :
    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 );

    Hey, wow! You know how to convert between a while loop and a for loop! If only you could teach this magical equivalence of loops to others!

  • ronny (unregistered)
    Comment held for moderation.

Leave a comment on “A Dash of Comma Spice”

Log In or post as a guest

Replying to comment #31297:

« Return to Article