Useful constants



  • I'm fortunate enough to be taking over a VB.NET/Winforms app (we're simultaneously rebuilding the entire thing in C#/WPF). I often run into minor gems, such as a MSSQL WHEN statement that returns the string "heheheheheh.... / Elmer Fudd". I just came across this Best Practices snippet:

    
    Public NotInheritable Class Constants
        Public Const COLON As String = ":"
        Public Const COMMA As String = ","
        Public Const INTERNAL_SYSTEM_1 As String = "INTERNAL_SYSTEM_1"
        Public Const INTERNAL_SYSTEM_2 As String = "INTERNAL_SYSTEM_2"
    End Class
    
    

    Good to know we'll be prepared when "colon" becomes the character 'ಠ'.



  • Yes, the use of constants here is ridiculous.

    For another laugh, look at the way he's done it - shouldn't those constants be declared as Shared (static for our C# friends)???

    Martin.



  • @muppetman said:

    For another laugh, look at the way he's done it - shouldn't those constants be declared as Shared (static for our C# friends)???
    No, for the same reason they aren't declared Shared here.

     



  • @muppetman said:

    Yes, the use of constants here is ridiculous.

    For another laugh, look at the way he's done it - shouldn't those constants be declared as Shared (static for our C# friends)???

    Martin.

     

     

    I don't know about VB, but in C# const implies static, so it's unnecessary to include it, and is in fact a syntax error.



  • Compare with this, which I've seen used in many codebases:

    [code]
    #if OSENV == UNIX



    const char slash = "/";



    #else /
    Windows */



    const char *slash = "\\";



    #endif
    [/code]

    Makes a lot more sense when you realize the thing might change. There's maybe a little bit of WTF here in that COLON and COMMA would probably be better named "SEPARATOR_1" and "SEPARATOR_2" but oh well.



  • @smxlong said:

    Compare with this, which I've seen used in many codebases:

    <font face="Lucida Console" size="2"> #if OSENV == UNIX

    const char *slash = "/";

    #else /* Windows */

    const char *slash = "\\";

    #endif </font>
    The issue of the differing file separators. This code is completely redundant since windows does recognize '/' as a valid file separator.


  • This is unbelievably true, and just makes me cry.

    in file_a.c:

    /* file_a.c  stuff for module A */
    

    #define FILEA_DWZERO 0U
    #define FILEA_FLOATZERO 0.0f
    #define FILEA_FLOATONE 1.0f
    #define FILEA_DWONE 1U

    /* stuff like... */

    counter = counter + FILEA_DWONE;

    in file_b.c:

    /* file_b.c  stuff for module B */
    

    #define FILEB_DWZERO 0U
    #define FILEB_FLOATZERO 0.0f
    #define FILEB_FLOATONE 1.0f
    #define FILEB_DWONE 1U

    This goes on for each of about twenty modules.

    All this because people take the mandate "no magic numbers" too literally. I really wish they [i]did[/i] require a license to write software some days.



  •  @delta534 said:

    This code is completely redundant since windows does recognize '/' as a valid file separator.
    Since what version? XP only recognises the forward slash as a seperator in the shell, many of the API calls, applications and the command prompt do not:

     C:\>type "c:/program files/adobe/Adobe Dreamweaver CS4/FlashAuthor.cfg"
    The system cannot find the file specified.

    C:\>type "c:\program files\adobe\Adobe Dreamweaver CS4\FlashAuthor.cfg"
    # FlashAuthor.cfg
    #

    [...]

     

     



  • @delta534 said:

    The issue of the differing file separators. This code is completely redundant since windows does recognize '/' as a valid file separator.

    Windows does. However, bajillions of other things don't. If you're generating paths as output and feeding these to some tool which does something with them, and that tool barfs when it sees slashes instead of backslashes, then it's not relevant what Windows can and cannot handle.



  • I'm not a Windows expert (thank God!) but my general impression is that the Windows API will accept forward slash, but the Windows shell command line will not, since shell commands use forward slashes for parameter markers. Whether any given application will take forward slash or not depends, of course, on who wrote the application. Indeed, typing forward slashes into a Windows program can lead to weirdness where features implemented by a call to the API will work, but features implemented by application code can fail.



  • It's cmd.exe that's funky, not windows, and the ability to use the '/' character as a path separator goes back to [url=http://blogs.msdn.com/b/larryosterman/archive/2005/06/24/432386.aspx] dos 2.0[/url]



  • @smxlong said:

    Compare with this, which I've seen used in many codebases:

    <font face="Lucida Console" size="2"> #if OSENV == UNIX
    const char *slash = "/";
    #else /* Windows */
    const char *slash = "\\";
    #endif </font>

    Makes a lot more sense when you realize the thing might change. There's maybe a little bit of WTF here in that COLON and COMMA would probably be better named "SEPARATOR_1" and "SEPARATOR_2" but oh well.

    I have sometimes seen this. I have done in C something like that, but for the ':' and ';' in environment variables for include paths (';' in Windows because ':' is for drive letters), and using a single character (type char) rather than a string (type char*), and using #define. Also, it is correct that Windows can also use forward slashes, so this shouldn't be needed. This is the code I used (it is part of TeXnicard you can see the signature):
    [code]@<Set |includepath_separator| depending on operating system@>=
    #ifdef WIN32
    #define @!includepath_separator ';'
    #else
    #define includepath_separator ':'
    #endif[/code]
    It's cmd.exe that's funky, not windows
    It is true. The reason cmd.exe does that is because of / for switches of some commands, to make similar to DOS programming. Some programs will accept it both ways on their command-line parameters, when used in Windows. Also, there is some other thing about certain things about the command processor too, such as the dealing with alterante streams, and other devices, can sometimes do things that might be a bit unexpected.


  • @delta534 said:

    This code is completely redundant since windows does recognize '/' as a valid file separator.
    Win32 API does (as long as you aren't using \?\ paths), but I usually saw *slash used to cut the path off the program name (usually in command-line utilities), which in Windows will always use backslashes.



  • @Mole said:

     @delta534 said:

    This code is completely redundant since windows does recognize '/' as a valid file separator.
    Since what version? XP only recognises the forward slash as a seperator in the shell, many of the API calls, applications and the command prompt do not:

     C:\>type "c:/program files/adobe/Adobe Dreamweaver CS4/FlashAuthor.cfg"
    The system cannot find the file specified.

    C:\>type "c:\program files\adobe\Adobe Dreamweaver CS4\FlashAuthor.cfg"
    # FlashAuthor.cfg
    #

    [...]

    How bizarre.  A similar command wjffm on w2k:

    Microsoft Windows 2000 [Version 5.00.2195]
    (C) Copyright 1985-2000 Microsoft Corp.

    C:\Documents and Settings\Administrator>dir *.txt
     Volume in drive C is SYSTEM
     Volume Serial Number is D472-11D8

     Directory of C:\Documents and Settings\Administrator

    18/04/2011  10:03                3,585 schedlog.txt
                   1 File(s)          3,585 bytes
                   0 Dir(s)  24,761,765,888 bytes free

    C:\Documents and Settings\Administrator>type "C:/Documents and Settings/Administ
    rator/schedlog.txt"
    30/07/2009 07:42:04: Scheduler (5.86.1) starting.
    30/07/2009 07:42:14: Scheduler quitting.
    31/08/2009 15:50:50: Scheduler (5.86.1) starting.
    31/08/2009 15:57:37: Scheduler quitting.

     

    Guess Microsoft gone done broke it sometime between then and XP....

     



  • @DaveK said:

    @Mole said:

     @delta534 said:

    This code is completely redundant since windows does recognize '/' as a valid file separator.
    Since what version? XP only recognises the forward slash as a seperator in the shell, many of the API calls, applications and the command prompt do not:

     C:&gt;type "c:/program files/adobe/Adobe Dreamweaver CS4/FlashAuthor.cfg"
    The system cannot find the file specified.

    C:&gt;type "c:\program files\adobe\Adobe Dreamweaver CS4\FlashAuthor.cfg"
    # FlashAuthor.cfg
    #

    [...]

    How bizarre.  A similar command wjffm on w2k:

    Microsoft Windows 2000 [Version 5.00.2195]
    (C) Copyright 1985-2000 Microsoft Corp.

    C:\Documents and Settings\Administrator>dir *.txt
     Volume in drive C is SYSTEM
     Volume Serial Number is D472-11D8

     Directory of C:\Documents and Settings\Administrator

    18/04/2011  10:03                3,585 schedlog.txt
                   1 File(s)          3,585 bytes
                   0 Dir(s)  24,761,765,888 bytes free

    C:\Documents and Settings\Administrator>type "C:/Documents and Settings/Administ
    rator/schedlog.txt"
    30/07/2009 07:42:04: Scheduler (5.86.1) starting.
    30/07/2009 07:42:14: Scheduler quitting.
    31/08/2009 15:50:50: Scheduler (5.86.1) starting.
    31/08/2009 15:57:37: Scheduler quitting.

     

    Guess Microsoft gone done broke it sometime between then and XP....

     

    The really odd part is that the only thing that doesn't work on XP is doing it with the extended folder name - type it as /progra~1/ or whatever it is, and it'll work just fine. I can imagine that being a fun script-bug to try and track down :)



  • seen in ender's tags : 

    this "one" char : ‧̴̵̶̷̸̡̢̧̨̛̖̗̘̙̜̝̞̟̠̣̤̥̦̩̪̫̬̭̮̯̰̱̲̳̹̺̻̼̀́̂̄̃̅̆̇̈̉̊̋̌̍̎̏̐̑̒̓̔̽̾̿̀́͂̓̈́̕̚͠͡ͅ

    excuse my naiveness and ignorance in advance but............ what the fuck is this shit, ender ?



  • @toshir0 said:

     ‧̴̵̶̷̸̡̢̧̨̛̖̗̘̙̜̝̞̟̠̣̤̥̦̩̪̫̬̭̮̯̰̱̲̳̹̺̻̼̀́̂̄̃̅̆̇̈̉̊̋̌̍̎̏̐̑̒̓̔̽̾̿̀́͂̓̈́̕̚͠͡ͅ

    what the fuck is this shit, ender ?

    It's a U+2027 HYPHENATION POINT combined with the following combining diacritical marks: U+0300 COMBINING GRAVE ACCENT, U+0301 COMBINING ACUTE ACCENT, U+0302 COMBINING CIRCUMFLEX ACCENT, U+0304 COMBINING MACRON, U+0303 COMBINING TILDE, U+0305 COMBINING OVERLINE, U+0306 COMBINING BREVE, U+0307 COMBINING DOT ABOVE, U+0308 COMBINING DIAERESIS, U+0309 COMBINING HOOK ABOVE, U+030A COMBINING RING ABOVE, U+030B COMBINING DOUBLE ACUTE ACCENT, U+030C COMBINING CARON, U+030D COMBINING VERTICAL LINE ABOVE, U+030E COMBINING DOUBLE VERTICAL LINE ABOVE, U+030F COMBINING DOUBLE GRAVE ACCENT, U+0310 COMBINING CANDRABINDU, U+0311 COMBINING INVERTED BREVE, U+0312 COMBINING TURNED COMMA ABOVE, U+0313 COMBINING COMMA ABOVE, U+0314 COMBINING REVERSED COMMA ABOVE, U+0315 COMBINING COMMA ABOVE RIGHT, U+0316 COMBINING GRAVE ACCENT BELOW, U+0317 COMBINING ACUTE ACCENT BELOW, U+0318 COMBINING LEFT TACK BELOW, U+0319 COMBINING RIGHT TACK BELOW, U+031A COMBINING LEFT ANGLE ABOVE, U+031B COMBINING HORN, U+031C COMBINING LEFT HALF RING BELOW, U+031D COMBINING UP TACK BELOW, U+031E COMBINING DOWN TACK BELOW, U+031F COMBINING PLUS SIGN BELOW, U+0320 COMBINING MINUS SIGN BELOW, U+0321 COMBINING PALATALIZED HOOK BELOW, U+0322 COMBINING RETROFLEX HOOK BELOW, U+0323 COMBINING DOT BELOW, U+0324 COMBINING DIAERESIS BELOW, U+0325 COMBINING RING BELOW, U+0326 COMBINING COMMA BELOW, U+0327 COMBINING CEDILLA, U+0328 COMBINING OGONEK, U+0329 COMBINING VERTICAL LINE BELOW, U+032A COMBINING BRIDGE BELOW, U+032B COMBINING INVERTED DOUBLE ARCH BELOW, U+032C COMBINING CARON BELOW, U+032D COMBINING CIRCUMFLEX ACCENT BELOW, U+032E COMBINING BREVE BELOW, U+032F COMBINING INVERTED BREVE BELOW, U+0330 COMBINING TILDE BELOW, U+0331 COMBINING MACRON BELOW, U+0332 COMBINING LOW LINE, U+0333 COMBINING DOUBLE LOW LINE, U+0334 COMBINING TILDE OVERLAY, U+0335 COMBINING SHORT STROKE OVERLAY, U+0336 COMBINING LONG STROKE OVERLAY, U+0337 COMBINING SHORT SOLIDUS OVERLAY, U+0338 COMBINING LONG SOLIDUS OVERLAY, U+0339 COMBINING RIGHT HALF RING BELOW, U+033A COMBINING INVERTED BRIDGE BELOW, U+033B COMBINING SQUARE BELOW, U+033C COMBINING SEAGULL BELOW, U+033D COMBINING X ABOVE, U+033E COMBINING VERTICAL TILDE, U+033F COMBINING DOUBLE OVERLINE, U+0340 COMBINING GRAVE TONE MARK, U+0341 COMBINING ACUTE TONE MARK, U+0342 COMBINING GREEK PERISPOMENI, U+0343 COMBINING GREEK KORONIS, U+0344 COMBINING GREEK DIALYTIKA TONOS, U+0345 COMBINING GREEK YPOGEGRAMMENI, U+0360 COMBINING DOUBLE TILDE, U+0361 COMBINING DOUBLE INVERTED BREVE. I hope this adequately answers your question.



  •  @ender said:

    I hope this adequately answers your question.
    Holy mother of shit.

    It does, thank you very much.



  • @ender said:

    I hope this adequately answers your question.
     

    O_O

     

    I am in awe.



  • @toshir0 said:

    excuse my naiveness
    'Naiveté', froggy. It's your damn word.



  • @ender said:

    U+033C COMBINING SEAGULL BELOW

    The guys in the office are wondering what I'm reading.  There's seriously a character modifier called "seagull"? Where does this actually get used?



  • @Qwerty said:

    @ender said:

    U+033C COMBINING SEAGULL BELOW

    The guys in the office are wondering what I'm reading.  There's seriously a character modifier called "seagull"? Where does this actually get used?

    Lazy fuck.


  • Garbage Person

     "linguolabial constant"



  • @intertravel said:

    'Naiveté', froggy. It's your damn word.

     Our "damn word" is spelled "naïveté", yankee.  (yes, with a ï not a i )

    As well as the adjective is not "naive" but "naïf" for a boy and "naïve" for a girl...



  • @toshir0 said:

    As well as the adjective is not "naive" but "naïf" for a boy and "naïve" for a girl...

    Oh, interesting. I wonder why we borrowed the feminine form instead of masculine. ... Or is that a stupid question?



  • @Xyro said:

    Or is that a stupid question?
    *You* borrowed : *you* tell me.

    What I can tell is that the word seems to have appeared during 12th century, coming from the same latin root which gave "natif" (the english "native") or "naturel" ("natural")

    If you find the previous sentence very interesting, please do humanity a favor : shoot yourself.



  • @toshir0 said:

    If you find the previous sentence very interesting, please do humanity a favor : shoot yourself.

    And how the fuck am I supposed to get a gun to do that?



  • @derula said:

    @toshir0 said:
    If you find the previous sentence very interesting, please do humanity a favor : shoot yourself.

    And how the fuck am I supposed to get a gun to do that?

    Maybe you can get some basketballs to do it slowly..?



  • @Xyro said:

    @derula said:
    @toshir0 said:
    If you find the previous sentence very interesting, please do humanity a favor : shoot yourself.

    And how the fuck am I supposed to get a gun to do that?

    Maybe you can get some basketballs to do it slowly..?

    I could do that, but I doubt it would work. I'm not really good at basketball.



  • @too_many_usernames said:

    All this because people take the mandate "no magic numbers" too literally.

    My rule is something like "no magic numbers except 0, 1, 0.0, and 1.1 without a damn good comment." I think that's a reasonable compromise. Even if one thinks my rule is too liberal, the example given above is idiotic. The names of the constants should be better, specifically they should tell me the purpose of the constant, not its initial value during development. (Someone has already mentioned this.)

    My identifier naming standards are probably a bit more controversial: no uppercase letters or underscores. I really do not like to have to use the shift key. The result is that I cannot hire anyone too pretentious to work with me.



  • @Qwerty said:

    @ender said:

    U+033C COMBINING SEAGULL BELOW

    The guys in the office are wondering what I'm reading.  There's seriously a character modifier called "seagull"? Where does this actually get used?

    Unicode is the real WTF. Character modifiers are a gigantic headache for programmers and users alike. They result in multiple character sequences that look the same, e.g. when viewing a URL. An "Angstrom" can be mistaken for a Scandinavian Å or vice-versa. Please, please avoid this modern day Tower-of-Babel and use plain old ASCII encoding. If you're lazy, this is a great excuse for using Python, Ruby, etc.



  • @bridget99 said:

    Please, please avoid this modern day Tower-of-Babel and use plain old ASCII encoding.
    My last name cannot be written by using only ASCII characters.


  • Garbage Person

    @bridget99 said:

    Unicode is the real WTF. Character modifiers are a gigantic headache for programmers and users alike. They result in multiple character sequences that look the same, e.g. when viewing a URL. An "Angstrom" can be mistaken for a Scandinavian Å or vice-versa. Please, please avoid this modern day Tower-of-Babel and use plain old ASCII encoding. If you're lazy, this is a great excuse for using Python, Ruby, etc.
    Alright, you go ahead and get everyone speaking and writing an ASCII-compatible language.

     

    Universal English is working out well except in the countries that actually have fucked up written languages that really do need Unicode. 



  • @bridget99 said:

    They result in multiple character sequences that look the same, e.g. when viewing a URL.

    ... so? This is a Greek upper Beta: Β, and this a Latin upper B: B. Yeah they look the same. But it doesn't have the same meaning. Imagine trying to write a translator tool, translating from say, Greek to English, and then suddenly among all Greek letters you find a B. Now that makes sense, does it? And what's the problem with different characters looking the same?

    Also, using my favorite Unicode character (U+FEFF) is great for getting around stupid word filters.



  • @Weng said:

    fucked up written languages

    I'm gonna fuck YOU up!



  • @bridget99 said:

    Character modifiers ... result in multiple character sequences that look the same

    Normalization is your friend.

    Java example:

    import java.text.Normalizer;
    import java.text.Normalizer.Form;

    public class Test {
    public static void main(String[] args) {
    Å();
    Å();
    Å();
    }

    static void Å() {
    print("Å");
    }

    static void Å() {
    print("Å");
    }

    static void Å() {
    print("Å");
    }

    static void print(String s) {
    printHex(s);
    printHex(Normalizer.normalize(s, Form.NFC));
    }

    static void printHex(String s) {
    System.out.print(s);
    for (char c : s.toCharArray()) {
    System.out.print(' ');
    System.out.print(Integer.toHexString(c));
    }
    System.out.println();
    }
    }

    (TRWTF being of course that my compiler does not normalize identifiers.)



  • @Weng said:

    Universal English is working out well except in the countries that actually have fucked up written languages that really do need Unicode. 
    Oxford English Dictionary disagrees:
    naïve [na:'i:v, nai'i:v] adj.



  • @ender said:

    @bridget99 said:
    Please, please avoid this modern day Tower-of-Babel and use plain old ASCII encoding.
    My last name cannot be written by using only ASCII characters.

    Sure it can. It might not contain all the nice little accents and other doo-hickies that people in your particular corner of the world expect, but any name can be adequately expressed in the plain, unadorned Latin alphabet.

    Incidentally, I (and a small group of like thinkers) have been advocating (e.g. to my Congressman) legislation to fix the official character set for proper names in American English. Right now, we have people who think their names include apostrophes, dashes, etc. and this needs to be standardized. Really, I personally would be happy if we had a standard set of acceptable names (which some countries actually have). I would argue that preventing someone from naming their kid "Starbeam" (obvious hippy / unemployable) or "Montravious" (obviously black / unemployable) is really an example of society protecting the weak (a baby) from the strong (its parents).



  • @bridget99 said:

    I (and a small group of like thinkers) have been advocating (e.g. to my Congressman) legislation to fix the official character set for proper names in American English. Right now, we have people who think their names include apostrophes, dashes, etc. and this needs to be standardized.
     

    Otherwise known as the "I don't care what your parents named you, we're going to call you Butch" paradigm.



  • I'm glad US-ASCII includes GRAVE ACCENT at 0x60. At the very least I won't have to fix my shell scripts if your cute little crusade against Unicode ever comes to my town.



  • @bridget99 said:

    "Montravious" (obviously black / unemployable)

    Obvious racist troll is obvious.



  • @bridget99 said:

    "Montravious" (obviously black / unemployable)
     

    More racism and ban. Consider this your warning.



  • @bridget99 said:

    @ender said:
    @bridget99 said:
    Please, please avoid this modern day Tower-of-Babel and use plain old ASCII encoding.
    My last name cannot be written by using only ASCII characters.

    Sure it can. It might not contain all the nice little accents and other doo-hickies that people in your particular corner of the world expect, but any name can be adequately expressed in the plain, unadorned Latin alphabet.

    What about N!xau? I expect most people can't even pronounce that properly! The gods really are crazy.

    I would argue that preventing someone from naming their kid "Starbeam" (obvious hippy / unemployable) or "Montravious" (obviously black / unemployable) is really an example of society protecting the weak (a baby) from the strong (its parents).

    For the record I named my son an old-fashioned name, never really common but common enough that people have heard it, plus being a definite boys name. Though once at a medical place they were expecting an old man and got a baby! He was named after my grandfather who died in 1997.



  • @dhromed said:

    @bridget99 said:

    "Montravious" (obviously black / unemployable)
     

    More racism and ban. Consider this your warning.

    YOU have that power?? That's like making SpectateSwamp Secretary-General of NATO.



  • @bridget99 said:

    YOU have that power?? That's like making SpectateSwamp Secretary-General of NATO.

    SpectateSwamp can curse the NATO by dancing. He doesn't need to be put in charge, he already is, more or less.


  • ♿ (Parody)

    @derula said:

    @bridget99 said:
    YOU have that power?? That's like making SpectateSwamp Secretary-General of NATO.

    SpectateSwamp can curse the NATO by dancing. He doesn't need to be put in charge, he already is, more or less.

    But how much power does the secretary general really have? Isn't the main function of NATO to make the Europeans feel like they're contributing to whatever military adventure the Americans come up with?



  • @boomzilla said:

    @derula said:
    @bridget99 said:
    YOU have that power?? That's like making SpectateSwamp Secretary-General of NATO.

    SpectateSwamp can curse the NATO by dancing. He doesn't need to be put in charge, he already is, more or less.

    But how much power does the secretary general really have? Isn't the main function of NATO to make the Europeans feel like they're contributing to whatever military adventure the Americans come up with?

    No idea. What is a NATO, anyway?



  • @boomzilla said:

    @derula said:
    @bridget99 said:
    YOU have that power?? That's like making SpectateSwamp Secretary-General of NATO.
    SpectateSwamp can curse the NATO by dancing. He doesn't need to be put in charge, he already is, more or less.
    But how much power does the secretary general really have? Isn't the main function of NATO to make the Europeans feel like they're contributing to whatever military adventure the Americans come up with to make the Europeans contribute to whatever military adventure the Americans come up with to share the costs and the guilt ?
    F(renched) TFY



  • @toshir0 said:

    F(renched) TFY

    I like how the French word for NATO is OTAN.


Log in to reply