• (nodebb)

    But at least they future-proofed convert_compare_result by having it print an error message if called with an unimplemented member of their enum. Surely everywhere else in their codebase has that printf() directed to a log of some kind and surely all their admins and customers' admins always read and act on every log entry.

    Surely?

  • Officer Johnny Holzkopf (unregistered)

    As a truly antique C programmer, I think the enum definition and its use in the function declaration don't seem to match. With "typedef enum exop_t { ... };", an "enum exop_t" is declared, but no type is defined (i. e., this won't compile, the "typedef" needs to be removed). In order for "static bool compare_booleans (bool bool1, bool bool2, exop_t exop)" and "static boolean convert_compare_result (int32_t cmpresult, exop_t exop)" to work, the declaration should be "typedef enum { ... } exop_t;". I won't comment on the suggestion of leaving the "_t" suffix to POSIX datatypes - you souldn't use them in your custom code. And finally, the printf() call should be a "fprintf(stderr, ...)" to address the standard error output, and maybe contain something helpful. Okay, I'm old, and I'll show myself out...

  • Hanzito (unregistered)

    I suspect the problem came from using non booleans as booleans, e.g. comparing two ints for their truthiness. I cannot explain the enum operations. Smells like over-engineering.

    Which makes me think: were these really C programmers? Because they would use a boolean (*exop)(int32_t).

  • (nodebb)

    I'm having my breakfast and this code made me nauseous and difficult to finish eating.

  • (nodebb)

    This line:

    if ((bool1 && bool2) || (!bool1 && !bool2)) {

    is just if ( bool1 != bool2 ).

    Well, unless they've done something über-stupid like defining their own bool as something other than _Stdbool. See, the thing is, _Stdbool is just a weird name for what amounts to C++'s bool, and, except in the UB-inducing case of one that hasn't been initialised yet, it is, like the one in C++, constrained by the language to be either false (0) or true (1).

    However, there's a substantial anonymisation fail, one that makes me suspect that the original code isn't in C (nor in C++), but perhaps Java. Consider this definition:

    static bool compare_booleans (bool bool1,
                                  bool bool2,
                                  exop_t  exop)
    {
        // ...
    }
    

    (all decorated with lovely bools) and this one:

    static boolean
        convert_compare_result (int32_t cmpresult,
                                exop_t exop)
    {
        // ...
    }
    

    which returns boolean instead.

    Or they've defined their own boolean alongside C99+'s bool == _Stdbool, which is definitely a WTF by itself.

    But yeah, the whole thing is a WTF, what amounts to a low-quality partial reimplementation of C++20+'s spaceship operator (operator <=>) without the automatic selection (by the compiler) of how to use its return.

    Addendum 2025-10-28 08:29: Ugh, no, it's if ( bool1 == bool2 )

  • (nodebb) in reply to Steve_The_Cynic

    Not Java, at least not that code. The 'typedef enum' is a dead giveaway it's C or C++.

  • (nodebb)

    I don't believe the frist line of the article. Were booleans really ever "frustrating"? If you didn't like using 1 and 0, it was trivial to use

    #define TRUE 1
    #define FALSE 0
    

    And if you have a variable that might not be 1 or 0 and you want to test it for truthiness, we all knew the idiom !!variable. You didn't even need this in if statements.

    The closest to frustration might have been inconsistent styles.

  • Robin (unregistered)

    TRWTF is that they didn't handle FILE_NOT_FOUND

  • (nodebb)
    Comment held for moderation.
  • (nodebb) in reply to colejohnson66

    The 'typedef enum' is a dead giveaway it's C or C++.

    Or that it has been anonymised into C/C++. But badly.

  • lzsiga (unregistered)

    Okay, this is an over-engineered solution for a non-existent problem, but how and why was dealing with logical values problematic before stdbool? It way always clearly defined that zero means 'no/false' and non-zero means 'yes/true'. Also comparison and logical operators always return 1 for true.

Leave a comment on “ A Truly Bad Comparison”

Log In or post as a guest

Replying to comment #:

« Return to Article