• can't think of any more stupid names (unregistered)
    const char FRIST  = "FRIST";
    
  • Peregrine (unregistered)

    At least that way, typos become compiler errors.

  • Sole Purpose Of Visit (unregistered) in reply to Peregrine

    Except when they don't. Which, as with most stringified things, will happen quite often.

  • (nodebb)

    Isn't this just the stringy equivalent of this?

    #define ZERO 0
    #define ONE 1
    
  • CrashMagnet (unregistered)

    I didn't know a '$' could be used in a variable names in C/C++ (I assume). Last time I saw a '$' in a variable names was in PL1 (circa. 1970's).

    Silver start to TDWTF for teaching something new.

  • (nodebb) in reply to Steve_The_Cynic

    Its a little worse than #defines because this only produces a warning:

    const char $$B[] = "$$B"; char *ptr = $$B; *ptr = 'x'; printf("%s\n", ptr);

  • Anonymous') OR 1=1; DROP TABLE wtf; -- (unregistered) in reply to CrashMagnet

    I didn't know a '$' could be used in a variable names in C/C++ (I assume).

    They're not allowed by default by the ISO C or ISO C++ standards, but the standards do allow for identifiers to contain implementation-defined characters in them. Some compilers such as GCC allow them as an extension, depending on the target architecture.

  • Hanzito (unregistered)

    There is a benefit to this approach: you can't document strings, but you can document variables.

  • Sole Purpose Of Visit (unregistered) in reply to Hanzito

    That is so true.

    In future, I must revise my approach to writing documents. I shall no longer use the deprecated "noun" or "verb" things, which are hideously stringified.

    "Variables?" That's the ticket.

  • (nodebb)

    I choose to believe the first three constants are involved in a ridiculously convoluted approach to adding the BEL character to something and the rest was added by people just going along with it.

  • (nodebb) in reply to can't think of any more stupid names

    const char FRIST = "FRIST";

    Well, at least that won't compile...

  • Conradus (unregistered) in reply to CrashMagnet

    "I didn't know a '$' could be used in a variable names in C/C++ (I assume). Last time I saw a '$' in a variable names was in PL1 (circa. 1970's)."

    It can't. A C++ variable name, like a C variable name, can only be letters, digits and underscores, and can't start with a digit. Either their compiler allows nonstandard identifiers or there's something bogus here.

  • Conradus (unregistered) in reply to Conradus

    Ah, it's the compiler. Found this little gem on Microsoft's documentation web site: "The dollar sign $ is a valid identifier character in the Microsoft C++ compiler (MSVC)."

  • Barry Margolin (github)

    This isn't as useless as it seems. IDEs and cross-reference tools can find uses of a variable.

  • Yet Another Old Programmer (unregistered) in reply to Barry Margolin

    ... and grep or similar can find both.

  • Conradus (unregistered) in reply to Barry Margolin

    I suspect that it's there to make porting old programs in other languages to C easier since it may eliminate the need to change variable names. Of course, if you use this feature, you've just made your program utterly non-portable, which I expect is not a downside to Microsoft.

  • a cow (not a robot) (unregistered)

    When used with meaningful strings, this approach is sometimes actually useful.

    const char zzz[]="Something interesting and maybe long and difficoult...";

    If I'm not too wrong, this way zzz is actually a pointer. You can assign it to other variables (pointers, of course), and then (if needed) a check for equality is the check of a pointer, probably faster than a string with random length. Also, checking for equality of the pointers assures that something was really meant to be set to zzz, and it's not the case that two strings have the same content for random reasons. The utility of all of this, of course, depends on what the program should do...

  • (nodebb) in reply to Conradus

    Apparently "$" in C/C++ variable names is legal in many cases: https://stackoverflow.com/a/18033976/11026

  • Steve W (unregistered) in reply to bdoserror

    Cool! I'm going to start naming my variables $1000, $1000000, $19999, etc. - and claim my code is worth more!

  • Yikes (unregistered)

    $$BILLSYALL

  • (nodebb)

    Come compilers collapse duplicate literals, some don't. Some compilers have strings with duplicate values point to the same buffer, some don't.....

  • Bob Geary (unregistered)

    My favorite in the genre of "magic numbers bad named constants good!" was a cow-orker's test code that contained this:

    const int SIXTY_SECONDS = 120;

  • (nodebb) in reply to Rick

    (effectively) #warnings as errors...

    seriously, my CI/CD pipelines along with my Git commit hocks will refuse to process any project which does not have this set to true. (means of setting and detecting depend on compiler/environment,,,

  • (nodebb) in reply to Conradus

    That is why "Compliance Mode" should always be turned on. (Note for NERW projects it is on by default, for old projects it is off. Also /sdl should always be on.

  • Sauron (unregistered)

    Let's hope that code isn't meant to generate PHP code, where the double $ would be used for a variable variable name...

Leave a comment on “Throw it $$OUT”

Log In or post as a guest

Replying to comment #:

« Return to Article