• Cpt. Repeatable (unregistered)

    "While looking through some old code, I found this snippet that creats a 'human friendly' date to display to the user," Adam Smith writes, "perhaps this is more efficient than date('d/m/Y')."

    // Get today's date, and also format it in a more human-friendly way
    $date = date("Y-m-d");
    $nicedate = preg_replace("/(....).(..).(..)/","$3/$2/$1",$date);
    
  • I. G. E. (unregistered) in reply to qbolec
    qbolec:
    As to:
    String[] quarters = new String[QUARTERS_IN_A_YEAR];
    
    I think I know languages in which this creates : a) a 3 character long string with trailing \0 character, making the length equal to 3 b) a 4 pseudo random characters which happen to lay in the uninitialized memory, making the length anyhting between 0 and .. Segfault c) a 4 bytes array initialized with all zeros, making the length equal to 0

    I wonder what those might be. Doesn't compile in C or C++ (unless there's obscene preprocessor abuse going on, in which case it could do anything), in C# and Java it creates a string array of length four. It needs C-ish syntax, so we can exclude Python, Haskell, O'Caml, Lisp, VB, Brainfuck, Malbolge, Befunge, ... I'm not sure whether Perl or PHP would accept it, I think they wouldn't. Javascript perhaps, but if it does, it would behave more like C# and Java.

    P.S. in what language does it define a string of length 4?
    Not in any commonly used. The language in the article seems to be Java, so it creates a String array of length four, as one would expect.
  • JustSomeGuy (unregistered)

    Actually, something like:

    if (GlobalConfig["UseSpellingError"])
        pKey = pKey.replace("gb_custom", "gb_costum");
    has a valid use case. Specifically, when fixing problems in the field that are causing problems for one customer, the *worst* thing you can do is to introduce problems for all your other customers.

    So what you do is to add a configuration option to use the new behaviour. Anyone who doesn't care or who, for some reason, has come to rely on the "errant" behaviour, remains untouched. All those who complain (including the customer you're fixing the problem for) are told to add that particular configuration option.

    Ideally, it's cleaned up in the next major release, but this almost never happens :-)

  • (cs) in reply to silent d
    silent d:
    dgvid:
    D-Coder:
    ... Words are more explanatory than numbers in almost all cases.

    A word is worth one-thousand numbers.

    FTFY

    #define NUMBER_OF_NUMBERS_THAT_A_WORD_IS_WORTH 1000

    So the picture that's worth a thousand words must be a megapixel since each word is worth a thousand numbers.

  • Kef Schecter (unregistered)

    Funny... NoScript didn't like the URL of this article. Thought it was some kind of XSS attack. I have verified that, yes, it really is the URL that's the problem, since I created a dummy file on my own webserver with the same filename, no JS or anything at all in it, and it reported the same thing.

  • (cs) in reply to eVil
    eVil:
    A sinlge if could probably do the job, but surely you'd have to write some pretty inglorious code to achieve that.

    As it stands, the code is readable and efficient, so I dont see what you'd gain by refactoring it?

    You have a strange definition of "efficient". Remind me not to hire you.

  • eVil (unregistered) in reply to realmerlyn
    realmerlyn:
    eVil:
    A sinlge if could probably do the job, but surely you'd have to write some pretty inglorious code to achieve that.

    As it stands, the code is readable and efficient, so I dont see what you'd gain by refactoring it?

    You have a strange definition of "efficient". Remind me not to hire you.

    May I also remind you to read the rest of the thread? All is explained.

  • NotJamieZawinski (unregistered)

    "While looking through some old code, I found this snippet that creats a 'human friendly' date to display to the user," Adam Smith writes, "perhaps this is more efficient than date('d/m/Y')."

    // Get today's date, and also format it in a more human-friendly way
    $date = date("Y-m-d");
    $nicedate = preg_replace("/(....).(..).(..)/","$3/$2/$1",$date);
    

    “Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.” - Jamie Zawinski

  • gilgamash (unregistered)

    I think the only problem here is, that it is not written as a recursive function...

    for i=3000 to 3299
        if cint(val)=cint(i) then
            bolMatch=true  
            exit for 
        end if
     next
         
     for i=3351 to 3441
        if cint(val)=cint(i) then
            bolMatch=true   
            exit for
        end if
     next     
    
     for i=5964 to 5969
        if cint(val)=cint(i) then
            bolMatch=true   
            exit for
        end if
     next
    
  • Jay (unregistered) in reply to Mwah
    Mwah:
    Constants to replace numbers for clarity mean the code is poorly commented. ... Things that are constant in the world (number of quarters in a whole, for example) are usually expressed more efficiently as numbers, with a clarifying comment as necessary....

    I humbly beg to disagree.

    Sure, instead of writing:

    final static float PI=3.1415926;
    ...
    if (angle>PI/4) ...
    

    We could write

    if (angle>0.78539815) // value is pi/4
    

    But why is (b) better than (a)?

    In simple human psychology, once you define a constant, it's a lot easier to get programmers to use it, then to get them to add a comment. They have to write SOME value to get the code to work, and it's no more effort to type in the name of the constant than to type in the value. But writing a comment is always extra effort.

    Also, using named constants enforces consistency. Suppose we find one comment that refers to "top speed" and another that refers to "maximum velocity". Are these the same thing or two different things? With a named constant, the programmer would have to use the exact same name. (I suppose he could create two named constants with the same value, but that would require extra effort and thus be less likely.)

    I'm all for writing good documentation, and I don't buy the argument that "good code is self-documenting". But I don't see any reason to go out of our way to make the code LESS self-documenting and then make up for it with more comments or external documentation.

  • Jay (unregistered) in reply to Matt Westwood
    Matt Westwood:
    I shared an office with someone who suggested that the value of pi will change if they discover the universe isn't flat. I never was able to persuade him he was talking bollocks.

    Hmm, considering that the value of pi was discovered in this universe, it's hard to see how any new knowledge about the nature of the universe could change the value of pi. Perhaps we could learn that there are other possible universes with a different value for pi, but that's not what your friend is saying at all.

  • Randy Snicker (unregistered) in reply to the beholder
    the beholder:
    gallier2:
    the beholder:
    <snip>

    Yeah, well you're a big poopyhead too.

    That's so childish! Besides, I am rubber, you are glue... ;)
    Oh, it's all right. I'm sure that we can handle this situation maturely, just like the responsible adults that we are. Isn't that right, Mr... Poopy Pants?

  • Paul (unregistered) in reply to eVil

    Efficient!?!?? One If statement would use 3 comparisons, whereas this uses 394 comparisons....

  • (cs)

    Amazes me how downright stupid people are with renaming things. Unless you are coding in Notepad, modern IDEs have a "Refactor" feature that will rename all references to a particular variable/method/file/class. There should NEVER be a reason to keep a misspelled or incorrectly-named piece of code around other than developer ignorance.

  • photo shopper (unregistered) in reply to Eternal Density
    Eternal Density:
    silent d:
    dgvid:
    D-Coder:
    ... Words are more explanatory than numbers in almost all cases.

    A word is worth one-thousand numbers.

    FTFY

    #define NUMBER_OF_NUMBERS_THAT_A_WORD_IS_WORTH 1000

    So the picture that's worth a thousand words must be a megapixel since each word is worth a thousand numbers.

    Well, considering todays smartphones and stuff, you might rewrite that bit to something like this (warning: assuming stuff about preprocessors, don't know exactly what they do):

    #ifdef IPHONE_3 #define NUMBER_OF_NUMBERS_THAT_A_WORD_IS_WORTH 3000 #elseif IPHONE_4 #define NUMBER_OF_NUMBERS_THAT_A_WORD_IS_WORTH 8000 ... #endif

    .. or something similar (numbers are aproximate too, I'm to lazy to google the exact numbers of megapixels in the cameras).

  • Rasmus (unregistered)

    Perhaps it's just me, but I cannot see the "real error" quarter code above.

    const QUARTERS_IN_A_YEAR = 4;
    
    ... several codefiles later...
    
    private String[] getQuarters() 
    {
       String[] quarters = new String[QUARTERS_IN_A_YEAR];
       for (int k = 0; k < quarters.length; k++) {
          quarters[k] = new Integer(k+1).toString();
       }
       return quarters;
    }
    

    To the best of my understanding, that would actually generate the following array:

    quarters[0] = "1"
    quarters[1] = "2"
    quarters[2] = "3"
    quarters[3] = "4"
    

    Ofc. it depends on the language used

  • Axel (unregistered) in reply to Mwah
    Mwah:
    int _timeout = getTimeout(configFile);
    /* We have a range of errors that are returned as negative numbers, but we don't care here about them much, to be honest */
    if(_timeout < 0) _timeout = DEAFULT_TIMEOUT;
    
    This reminds me of http://www.ibiblio.org/harris/500milemail.html

Leave a comment on “SingleQautedString(), RemoveMeFunction(), Human-Friendly Dates, and More”

Log In or post as a guest

Replying to comment #:

« Return to Article