• (cs) in reply to hoodaticus
    hoodaticus:
    Iceman:
    if (GlobalConfig["UseSpellingError"]) { pKey = pKey.replace("gb_custom", "gb_costum"); }

    This not a WTF. This is normally used when a old version of some config file used a config option with a spelling error. In a new version you fix the spelling error, but have a compatible option for the old spelling. This way you don't need to update all config files everywhere, but new files will be correct. This is called backward compatibility.

    Or you could make it work properly. This is called "fixing the problem".

    yeah, you could - you could even do it in one hit - but that could cause there to be a simultaneous change to a score of deployed production databases, a whole host of configuration files etc yada yaya. Far better solution to put something like the above in, then once the code is there to allow both spellings (correct and incorrect), you can go through the various production systems and raise a change-ticket on each one in turn, do one a weekend till they're all done.

    BTDTBTFTS on a system that had a similar problem with spelloes. I still contend that if computer programmers know they're dyslexic, they pass their work over to a literate colleague whose job it is to correct their fucking stupid mistakes. Not to do that is the real WTF.

  • (cs) in reply to gallier2
    gallier2:
    gallier2:
    gallier2:
    trtrwtf:
    gallier2:
    Bye, bye

    Quickest meltdown in tdwtf history? Guy went to pieces so fast he took out Nagesh with the shrapnel!

    (stolen, yes, but stolen with love)

    That's what you think.

    Hey, there's only room for one fake gallier2 around here, and I'm him.

    This one isn't fake (can be noticed in my approximative english and my overly pedantic rants).

    If they're unregistered they're fake, so fuck off both of you shitheads.

  • (cs) in reply to hoodaticus
    hoodaticus:
    Jay:
    BTW, even seemingly obvious constants are not always so obvious. Like, how many weeks are in a year? Obviously 52, right? Well, not really, there are 52 weeks plus an extra day, 2 in leap years. My previous employer dealt with that by every 4 or 5 years having 53 weeks in the calendar.
    I like how c is slowing down as magnetic force increases. That's what I think of as a non-obvious non-constant.

    52 weeks in a year not being a constant is about as obvious as it gets.

    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.

  • gallier2 (unregistered) in reply to Matt Westwood
    Matt Westwood:
    hoodaticus:
    Jay:
    BTW, even seemingly obvious constants are not always so obvious. Like, how many weeks are in a year? Obviously 52, right? Well, not really, there are 52 weeks plus an extra day, 2 in leap years. My previous employer dealt with that by every 4 or 5 years having 53 weeks in the calendar.
    I like how c is slowing down as magnetic force increases. That's what I think of as a non-obvious non-constant.

    52 weeks in a year not being a constant is about as obvious as it gets.

    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.

    Yeah, well that's probably because you're a stinky-foot duck-plucker.

  • Danimal (unregistered) in reply to D-Coder
    D-Coder:
    Danimal:
    "A lot of students only go to school three quarters a year," Bob writes, "so maybe the author thought a business might take a summer off. Or try to squeeze another one in."

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

    The REAL WTF is that there are always 4 quarters in a year. There could be 3 trimesters, though.

    I'm voting this one the least WTF-y of the bunch. QUARTERS_IN_A_YEAR explains exactly why this number is here. Words are more explanatory than numbers in almost all cases.

    A word is worth 1,000 numbers.

    Man, we should all be writing GUIs.

  • SimonT (unregistered) in reply to PleegWat

    86400 seconds? I'm guilty of that!!!!!

  • (cs) in reply to Helena Handcart
    Helena Handcart:
    It looks like gallier2 wrote the original code, and feels guilty for it. To make amends he's posted a super-efficient rewrite in the hope of saving his soul. He's bound to get shirty if you go and pooh-pooh his considerable efforts. I for one will be implementing his visionary rewrite just as soon as I can.

    Now, where's the upvote button?

    He's bound to get shirty? What's that, and how do I get that way?

  • Chris (unregistered) in reply to SCSimmons

    I came here to say the same thing.

  • Tau (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.
    If you're working out the value of pi by measuring a real circle, then he's right - though most of the time you don't actually need that kind of precision. It's not going to change the first ten or twenty decimal places.

    If you calculate it from first principles, then he's wrong.

    Where did you get your pi?

  • (cs) in reply to gallier2
    gallier2:
    Matt Westwood:
    hoodaticus:
    Jay:
    BTW, even seemingly obvious constants are not always so obvious. Like, how many weeks are in a year? Obviously 52, right? Well, not really, there are 52 weeks plus an extra day, 2 in leap years. My previous employer dealt with that by every 4 or 5 years having 53 weeks in the calendar.
    I like how c is slowing down as magnetic force increases. That's what I think of as a non-obvious non-constant.

    52 weeks in a year not being a constant is about as obvious as it gets.

    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.

    Yeah, well that's probably because you're a stinky-foot duck-plucker.

    Good grief, that's a bit personal. Let me put it on record that I do not pluck ducks. Chickens, maybe, but never ducks. No quack.

  • Max (unregistered)

    'Is a single-qauted string' is a single-qauted string.

  • (cs) in reply to Tau
    Tau:
    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.
    If you're working out the value of pi by measuring a real circle, then he's right - though most of the time you don't actually need that kind of precision. It's not going to change the first ten or twenty decimal places.

    If you calculate it from first principles, then he's wrong.

    Where did you get your pi?

    The only time you "work out pi by measuring a real circle" is when you're in grade school with a dippy old bat handing out yarn and cocoa tins.

    In contemporary mathematics, Pi is not defined as the ratio of a circumference to a diameter. One of its properties is that in the Euclidean plane, circles satisfy that famous relationship. In the non-Euclidean plane, circles fail to have that property - but you don't go redefining pi according to the particular curvature of the frame of reference you happen to be using at the time. That's even more fucking stupid than confusing "its" and "it's".

    A simple way to define pi is pi/4 = 1 - 1/3 + 1/5 - 1/7 ... but it's impractical (and I may even have got it wrong - hey it's late here and I can' be bothered to look it up).

  • (cs) in reply to Max
    Max:
    'Is a single-qauted string' is a single-qauted string.

    +1 for the quine.

  • Phew (unregistered) in reply to WC
    WC:
    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?

    Thanks. I was starting to doubt my sanity on that one because I could not imagine the ugly if statement required to validate that in the same way.

    Not just me then....

    I'm trying to picture the single 'if' myself....

  • Jimmy (unregistered) in reply to eVil
    eVil:
    Plus its fine to break out of a for loop, so long as its obvious why you're breaking, and the loop isn't horrendously complicated.

    People just think that its bad practice, because it similar to using 'goto's in a way... however it is a very specific form of goto.... eg one that doesn't cause a spaghetti-code problem.

    This x 100.

    Too foten we see people saying "That's bad because my lecturer (or some guru I used to work for) says so. Misusing anything (as we have seen in the past) is probably bad practice, but that's not to say that all usage is bad practice.

  • Mick (unregistered) in reply to PleegWat
    PleegWat:
    I've got one colleague who tends to write things like #define N_HR 24. I've got another who tends to sprinkle magic numbers like 86400 (seconds per day) all over the place. Guess what I prefer maintaining...
    There is a difference (that people seem to miss) between "Magic Numbers should be avoided" and "Magic numbers should never be used".

    7 days in a week, 24hour in a day, 60 minutes in an hour, 60 seconds in a minute etc. Not really expecting any of these to change, and I have little issue (provided they use comments) with these being magic numbers (especially when people get to 86400). Maybe something like:

    /* Convert from days into seconds */
    whatever_in_seconds = whatever_in_days * 24 * 60 * 60;
    

    I would hope programmer's can work out what 24, 60, 60 mean here, even if I have worked with some who might struggle with 86400 Of course, I could also handle it as

    whatever_in_seconds = whatever_in_days * HRS_IN_DAY * MIN_IN_HR * SEC_IN_MIN;
    

    but this feels a little cumbersome for a fairly straightforward calculation

  • Sh!tSt!rrer (unregistered) in reply to gallier2
    gallier2:
    WC:
    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?

    Thanks. I was starting to doubt my sanity on that one because I could not imagine the ugly if statement required to validate that in the same way.

    As always the RWTF are the comments.

    integer ival = cint(val) bolMatch = (ival >= 3000 and ival <= 3299) or (ival >= 3351 and ival <= 3441) or (ival >= 5964 and ival <= 5969)

    In the worst case the original does: 394 loop (compare+increment) + 394 comparisons + 2394 int conversions. In the best case it does: 96 loop (compare+increment) + 96 comparisons + 296 int conversions. (if it finds the value in the first loop, the other loops are executed).

    My implementation has in the best case 2 comparisons + 1 conversion in the best case (if that unrecognized language has short cut evaluation) and at most 6 comparisons and 1 conversion.

    Uhm....In the best case, the loop does a single comparison (what does "exit for" do?

    Either way, 1576, 384, 3,7. THey're all constant....O(1).

    <disclaimer> I AM BEING SILLY, BUT FEEL FREE TO ADD YOUR OWN LECTURES ON BIG-O NOTATION ANYWAYS....

  • Mwah (unregistered) in reply to Jay
    Jay:
    I don't think the QUARTERS_IN_YEAR is all that dumb.

    Sure,

    String[] quarters=new String[QUARTERS_IN_YEAR]
    

    probably adds nothing to the comprehensibility of the code over

    String[] quarters=new String[4]
    

    I'm sure anyone smart enough to understand the code can figure out that 4 is the logical number of quarters and requires no further explanation.

    But what happens when we get a little farther in the code and we come across a statement that reads:

    for (int n=0;n<4;++n)
    

    Now a reader might well ask, Why 4? What is this counting over? What's magic about 4? But:

    for (int n=0;n<QUARTERS_IN_YEAR;++n)
    </pre>
    

    makes the intent clear.

    And once we create such a symbolic constant, we ought to use it everywhere. Do we really want to go through the code and say, "Oh, here it's obvious why we're using 4 so I'll hard-code the number, but over here it's ambiguous, so I'll use the symbolic constant. And this one, hmm, that's debateable, I'd better take a survey of all the programmers in the department and see how many understand the intent if I hard-code the constant." That would be a massive waste of thinking time. Once you've created the constant, just use it.

    The above is doubly true for constants that might change. In the case of quarters, okay, it's not likely that we'll have a calendar reform and start having 5 quarters in a year. (Though another poster did bring up a case where he in fact had to deal with 3 quarters in a year.) But if it's something like, say, number of regions, if some places that's hard-coded and some place we use a symbol, and then we change the number of regions, someone could easily be fooled into thinking that changing the constant solves the problem.

    Constants to replace numbers for clarity mean the code is poorly commented. Constants should instead be used for values that are constant at execution time, but may one day need changing for a future build, or to define a default, eg:

    #define DEFAULT_TIMEOUT 100

    We can change this value for a future build if we find 100 is insufficient. While the actual setting might be read from a config file, we set up this value in case there is nothing present in the config:

    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;
    

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

  • Mwah (unregistered) in reply to Jay
    Jay:
    BTW, even seemingly obvious constants are not always so obvious. Like, how many weeks are in a year? Obviously 52, right? Well, not really, there are 52 weeks plus an extra day, 2 in leap years. My previous employer dealt with that by every 4 or 5 years having 53 weeks in the calendar.
    And how does using a constant fix that problem?
  • sdfhtsr (unregistered) in reply to trtrwtf
    trtrwtf:
    Jay:
    BTW, even seemingly obvious constants are not always so obvious. Like, how many weeks are in a year? Obviously 52, right? Well, not really, there are 52 weeks plus an extra day, 2 in leap years. My previous employer dealt with that by every 4 or 5 years having 53 weeks in the calendar.

    365%52 != 0 Years aren't composed of weeks - a given date is within week N of a given year, determined by Julian date and your conception of where the week is bounded. If your calendar of the year has a set number of weeks you're doing something wrong.

    Maybe we could try: #define DAYS_IN_MOTH = (approx 30)

  • Mwah (unregistered) in reply to Jerry
    Jerry:
    Jay:
    BTW, even seemingly obvious constants are not always so obvious. Like, how many weeks are in a year? Obviously 52, right? Well, not really, there are 52 weeks plus an extra day, 2 in leap years. My previous employer dealt with that by every 4 or 5 years having 53 weeks in the calendar.
    Weeks don't fit into months or years, months aren't all the same size, even years have a variable number of days... and don't get me started with hours minutes and seconds that don't multiply by something reasonable but instead by 24 or 60... human time reckoning is a mess!

    What is needed is to replace all time measurements and expressions by a simple integer counter of http://en.wikipedia.org/wiki/Planck_time since the Big Bang.

    OK we might have to show it in hex instead of decimal, so it doesn't take up quite so much room.

    Whoa....I was with you until you started picking on hours and minutes. 24 and 60 are awesome numbers, because they are evenly divisible by a lot more than 10 or 100....

  • Korn (unregistered) in reply to Matt Westwood
    Matt Westwood:
    hoodaticus:
    Iceman:
    if (GlobalConfig["UseSpellingError"]) { pKey = pKey.replace("gb_custom", "gb_costum"); }

    This not a WTF. This is normally used when a old version of some config file used a config option with a spelling error. In a new version you fix the spelling error, but have a compatible option for the old spelling. This way you don't need to update all config files everywhere, but new files will be correct. This is called backward compatibility.

    Or you could make it work properly. This is called "fixing the problem".

    yeah, you could - you could even do it in one hit - but that could cause there to be a simultaneous change to a score of deployed production databases, a whole host of configuration files etc yada yaya. Far better solution to put something like the above in, then once the code is there to allow both spellings (correct and incorrect), you can go through the various production systems and raise a change-ticket on each one in turn, do one a weekend till they're all done.

    BTDTBTFTS on a system that had a similar problem with spelloes. I still contend that if computer programmers know they're dyslexic, they pass their work over to a literate colleague whose job it is to correct their fucking stupid mistakes. Not to do that is the real WTF.

    It was while mocking dyslexic programmers that we came up with the idea of fi and esac. I would have liked to use pool for loops, but people feared it might confuse the users....

  • Kristie (unregistered) in reply to Matt Westwood
    Matt Westwood:
    hoodaticus:
    Jay:
    BTW, even seemingly obvious constants are not always so obvious. Like, how many weeks are in a year? Obviously 52, right? Well, not really, there are 52 weeks plus an extra day, 2 in leap years. My previous employer dealt with that by every 4 or 5 years having 53 weeks in the calendar.
    I like how c is slowing down as magnetic force increases. That's what I think of as a non-obvious non-constant.

    52 weeks in a year not being a constant is about as obvious as it gets.

    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.

    I think he's right - but I don't think the universe is flat.

  • (cs)

    An excellent chance to show off VB's "Select Case" awesomeness:

    Select Case CInt(Val)
        Case 3000 To 3299: bolMatch = True
        Case 3351 To 3441: bolMatch = True
        Case 5964 To 5969: bolMatch = True
    End Select
    
  • (cs)

    So, would getQuarters() work for the Maya calendar? I'm just not sure.

    Which reminds me of my contribution to humor at work earlier this year. I created a script that converts mm/dd/ccyy dates to the Mayan calendar (long and calendar round); and also converts long-count dates (lc.lc.lc.lc.lc) back to mm/dd/ccyy.

    Then I wrote this deadpan tip on how to use the command to prepare for our upcoming conversion to the Maya calendar (because of the end of the world in 2012, of course) and sent it to everyone...

    ...on April Fools of course.

    Some people thought I was serious, even though I mentioned at the end of the article that, if they questioned my sanity, they should take a closer look at the date at the top of the article.

  • (cs) in reply to Matt Westwood
    Matt Westwood:
    gallier2:
    Matt Westwood:
    hoodaticus:
    Jay:
    BTW, even seemingly obvious constants are not always so obvious. Like, how many weeks are in a year? Obviously 52, right? Well, not really, there are 52 weeks plus an extra day, 2 in leap years. My previous employer dealt with that by every 4 or 5 years having 53 weeks in the calendar.
    I like how c is slowing down as magnetic force increases. That's what I think of as a non-obvious non-constant.

    52 weeks in a year not being a constant is about as obvious as it gets.

    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.

    Yeah, well that's probably because you're a stinky-foot duck-plucker.

    Good grief, that's a bit personal. Let me put it on record that I do not pluck ducks. Chickens, maybe, but never ducks. No quack.

    You should consider it. "Once you try quack, you'll never go back."

  • trtrwtf (unregistered) in reply to D-Coder
    D-Coder:
    Matt Westwood:
    gallier2:
    Matt Westwood:
    hoodaticus:
    Jay:
    BTW, even seemingly obvious constants are not always so obvious. Like, how many weeks are in a year? Obviously 52, right? Well, not really, there are 52 weeks plus an extra day, 2 in leap years. My previous employer dealt with that by every 4 or 5 years having 53 weeks in the calendar.
    I like how c is slowing down as magnetic force increases. That's what I think of as a non-obvious non-constant.

    52 weeks in a year not being a constant is about as obvious as it gets.

    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.

    Yeah, well that's probably because you're a stinky-foot duck-plucker.

    Good grief, that's a bit personal. Let me put it on record that I do not pluck ducks. Chickens, maybe, but never ducks. No quack.

    You should consider it. "Once you try quack, you'll never go back."

    I'm not the pheasant plucker, I'm the pheasant plucker's mate And I'm only plucking pheasants 'cause the pheasant plucker's late.

    I'm not the pheasant plucker, I'm the pheasant plucker's wife We're all day plucking pheasants, it's a pheasant plucking life.

  • The Outsorcerer (unregistered) in reply to SCSimmons
    SCSimmons:
    "A lot of students only go to school three quarters a year," Bob writes, "so maybe the author thought a business might take a summer off. Or try to squeeze another one in."

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

    Am I missing something? This seems to me to return a four-element array of strings, getQuarters(0) is "1", (1) is "2", (2) is "3", and (3) is "4"; it may be confusing and ugly, but still should work & not leave out any quarters ... Mostly just an illustration of why not to be rigid about your indexing standards.

    I'd compile it and verify, but I have a long-standing policy of not copying any code from this site for any purpose.

    I use code from this site all the time. It's always way better than my own.

  • (cs) in reply to Jay
    Jay:
    BTW, even seemingly obvious constants are not always so obvious. Like, how many weeks are in a year? Obviously 52, right? Well, not really, there are 52 weeks plus an extra day, 2 in leap years. My previous employer dealt with that by every 4 or 5 years having 53 weeks in the calendar.
    As opposed to just sending everyone home for that week and pretending it never happened?
  • (cs) in reply to trtrwtf
    trtrwtf:
    Jay:
    BTW, even seemingly obvious constants are not always so obvious. Like, how many weeks are in a year? Obviously 52, right? Well, not really, there are 52 weeks plus an extra day, 2 in leap years. My previous employer dealt with that by every 4 or 5 years having 53 weeks in the calendar.

    365%52 != 0 Years aren't composed of weeks - a given date is within week N of a given year, determined by Julian date and your conception of where the week is bounded. If your calendar of the year has a set number of weeks you're doing something wrong.

    isn't that exactly what he just said?!

  • (cs) in reply to trtrwtf
    trtrwtf:
    I'm not the pheasant plucker, I'm the pheasant plucker's mate And I'm only plucking pheasants 'cause the pheasant plucker's late.

    I'm not the pheasant plucker, I'm the pheasant plucker's wife We're all day plucking pheasants, it's a pheasant plucking life.

    I'm not the pheasant plucker, I'm the pheasant plucker's son, And I will not pluck the pheasants 'til the pheasant plucker comes.

  • Aufgehaben (unregistered) in reply to BlueCollarAstronaut

    I usually just write: // Here be dragons! That does the trick.

  • (cs) in reply to Mick
    Mick:
    PleegWat:
    I've got one colleague who tends to write things like #define N_HR 24. I've got another who tends to sprinkle magic numbers like 86400 (seconds per day) all over the place. Guess what I prefer maintaining...
    There is a difference (that people seem to miss) between "Magic Numbers should be avoided" and "Magic numbers should never be used".

    7 days in a week, 24hour in a day, 60 minutes in an hour, 60 seconds in a minute etc. Not really expecting any of these to change, and I have little issue (provided they use comments) with these being magic numbers (especially when people get to 86400). Maybe something like:

    /* Convert from days into seconds */
    whatever_in_seconds = whatever_in_days * 24 * 60 * 60;
    

    I would hope programmer's can work out what 24, 60, 60 mean here, even if I have worked with some who might struggle with 86400 Of course, I could also handle it as

    whatever_in_seconds = whatever_in_days * HRS_IN_DAY * MIN_IN_HR * SEC_IN_MIN;
    

    but this feels a little cumbersome for a fairly straightforward calculation

    Yes but there's more to it than that. If you need to use 24 or 60 in another context, it's important to understand which 24 and which 60 are used in each particular context. If you needed at one point to investigate the 60 used in the other context, you don't want to have to get it mixed up (especially with a "search" tool) for the places where you might have hard-wired the number of seconds in a day ("because that's never going to change"). So if I saw code where 60 was used as a magic number for number of seconds in a day, I'd change it to use a parameter. No quack.

  • (cs) in reply to Korn
    Korn:
    Matt Westwood:
    hoodaticus:
    Iceman:
    if (GlobalConfig["UseSpellingError"]) { pKey = pKey.replace("gb_custom", "gb_costum"); }

    This not a WTF. This is normally used when a old version of some config file used a config option with a spelling error. In a new version you fix the spelling error, but have a compatible option for the old spelling. This way you don't need to update all config files everywhere, but new files will be correct. This is called backward compatibility.

    Or you could make it work properly. This is called "fixing the problem".

    yeah, you could - you could even do it in one hit - but that could cause there to be a simultaneous change to a score of deployed production databases, a whole host of configuration files etc yada yaya. Far better solution to put something like the above in, then once the code is there to allow both spellings (correct and incorrect), you can go through the various production systems and raise a change-ticket on each one in turn, do one a weekend till they're all done.

    BTDTBTFTS on a system that had a similar problem with spelloes. I still contend that if computer programmers know they're dyslexic, they pass their work over to a literate colleague whose job it is to correct their fucking stupid mistakes. Not to do that is the real WTF.

    It was while mocking dyslexic programmers that we came up with the idea of fi and esac. I would have liked to use pool for loops, but people feared it might confuse the users....

    Unfortunately in some more-or-less important application languages, "fi" is a reserved word for what in sane languages is indicated by "end if" or the closing of a curly.

  • Idu Knowski (unregistered)

    This needs to be optimized:

     for j=0 to 4294967295
        for i=3000 to 3299
           if cint(j)=cint(i) then
              bolMaybeMatch=true  
              exit for 
           end if
        next
         
        for i=3351 to 3441
            if cint(val)=cint(i) then
               bolMaybeMatch=true   
               exit for
            end if
        next
    
        for i=5964 to 5969
            if cint(val)=cint(i) then
               bolMaybeMatch=true   
               exit for
            end if
        next
    
        if bolMaybeMatch and cint(val)=cint(j)
            bolMatch=true
            exit for
        end if
     next
    
  • Jesper (unregistered)

    I always signle quat my strings. I like nicely qauted strings. What's wrong with that?

  • iMalc (unregistered)

    "while reconfiguring Sybase stored procedures for a custom version of an HL7 interface application, I stumbled upon this useless-yet-used everywhere procedure."

    That's clearly the result of something that got refactored and probably previously did something else. You think management wants the programmer to rip it out everywhere, taking extra time and potentially introducing a bug, when it could just be left as-is after it got refactored into nothingness? Heaven forbid it ever became needed to do something more complex again.

  • Neil (unregistered) in reply to Max
    Max:
    'Is a single-qauted string' is a single-qauted string.
    No.

    "Is a sentence fragment" is a sentence fragment.

    ''Is a single-qauted string'' is a single-qauted string. Of course I single-qauted the string, so there are no escape characters.

    CAPTCHA: bene there, done that.

  • (cs) in reply to Mick
    Mick:
    Maybe something like:
    /* Convert from days into seconds */
    whatever_in_seconds = whatever_in_days * 24 * 60 * 60;
    
    I would hope programmer's can work out what 24, 60, 60 mean here, even if I have worked with some who might struggle with 86400 Of course, I could also handle it as
    whatever_in_seconds = whatever_in_days * HRS_IN_DAY * MIN_IN_HR * SEC_IN_MIN;
    
    but this feels a little cumbersome for a fairly straightforward calculation
    But the advantage of the symbolic names is that they help you escape from the trap of writing non-portable code.

    In C, 246060 == what?

    If you answer 86400, take a portability penalty.

    If you answer "It depends on the size of int", congratulations. On 16-bit systems (not so may of those around now, I know, but bear with me), 246060 is 20864, even in "long seconds = 246060;". (Listen to the voice of experience, and bear in mind that I haven't had any reason to deal with C99, so I don't know if it is different there.)

    If you use the symbolic constants, you can stick an L in one of them (probably HRS_IN_DAY), and then the result of the multiply will be a long everywhere you use it, and consequently it will be 86400. You will never have to track down the one place where the multiplication got 16-bitted because you left off the L.

    Further note: #define SECS_IN_DAY 86400 will work correctly, as the compiler will expand the constant to be a long.

  • (cs) in reply to Steve The Cynic
    Steve The Cynic:
    But the advantage of the symbolic names is that they help you escape from the trap of writing non-portable code.

    In C, 246060 == what?

    If you answer 86400, take a portability penalty.

    If you answer "It depends on the size of int", congratulations. On 16-bit systems (not so may of those around now, I know, but bear with me), 246060 is 20864, even in "long seconds = 246060;". (Listen to the voice of experience, and bear in mind that I haven't had any reason to deal with C99, so I don't know if it is different there.)

    If you use the symbolic constants, you can stick an L in one of them (probably HRS_IN_DAY), and then the result of the multiply will be a long everywhere you use it, and consequently it will be 86400. You will never have to track down the one place where the multiplication got 16-bitted because you left off the L.

    Further note: #define SECS_IN_DAY 86400 will work correctly, as the compiler will expand the constant to be a long.

    This brings back horrific memories of VB6 applications suddenly breaking shortly after a primary key column gets past 32000.

  • Anon (unregistered) in reply to Sh!tSt!rrer
    Sh!tSt!rrer:
    gallier2:
    integer ival = cint(val) bolMatch = (ival >= 3000 and ival <= 3299) or (ival >= 3351 and ival <= 3441) or (ival >= 5964 and ival <= 5969)

    In the worst case the original does: 394 loop (compare+increment) + 394 comparisons + 2394 int conversions. In the best case it does: 96 loop (compare+increment) + 96 comparisons + 296 int conversions. (if it finds the value in the first loop, the other loops are executed).

    My implementation has in the best case 2 comparisons + 1 conversion in the best case (if that unrecognized language has short cut evaluation) and at most 6 comparisons and 1 conversion.

    Uhm....In the best case, the loop does a single comparison (what does "exit for" do?

    "exit for" exits the current for loop. However, the original code has 3 "for" loops, and the second and third loops will run even if we exited the first "for" loop. In fact, if we exited the first "for" loop, then we are guaranteed to go through the second and third loops completely - we can't possibly match the tests in those loops, so we cannot possibly end them early.

  • NotLeo (unregistered) in reply to Abso
    Abso:
    Leo:
    #define THREE_THOUSAND 3000 #define THREE_THOUSAND_ONE 3001 #define THREE_THOUSAND_TWO 3002 ... #define THREE_THOUSAND_THREE_HUNDRED_FIFTY_ONE 3351

    if cint(val)=cint(THREE_THOUSAND) then bolMatch=true end if if cint(val)=cint(THREE_THOUSAND_ONE) then bolMatch=true end if ...

    FTFY.

    This post has been sponsored by your grade three teacher.

    I guess it depends what language you're using for identifiers. If you use English, then THREE_THOUSAND_AND_ONE is correct. If you use American, then you might prefer THREE_THOUSAND_ONE.

  • forgottenlord (unregistered)

    Being in the education software industry, I can say with certainty that there are definitely cases where you'll need all 4 quarters. Why you've hard coded the number of terms, however, doesn't make sense to me. Why you've called it quarters makes even less sense.

  • eVil (unregistered) in reply to NotLeo
    NotLeo:
    Abso:
    FTFY.

    This post has been sponsored by your grade three teacher.

    I guess it depends what language you're using for identifiers. If you use English, then THREE_THOUSAND_AND_ONE is correct. If you use American, then you might prefer THREE_THOUSAND_ONE.

    Its pretty amusing when Americans choose to correct something when they're just wrong.

    We know they're wrong because they claim their speaking English, but getting all the rules mixed up.

    If you want to correct us on our own language, please remember that you are simply wrong, and your "4th Year" teacher probably ought to have explained that cultural truism to you. Although, had you started Kindergarten a year earlier, maybe you wouldn't have been a year behind your entire life, and might have worked this out for yourself.

    Bows I thank you! Bows

  • eVil (unregistered) in reply to eVil

    Also - I know I made some terrible spelling mistakes in the pos above, but frankly, since its my language, I have carte blanche to abuse it how I like... including via the inclusion of french (lower case F intentional).

  • (cs)

    Alex, you're slacking. QUARTERS_IN_A_YEAR isn't really a wtf, and certainly doesn't fail as has been pointed out. I can see very valid reasons for wanting strings representing the index of a quarter starting from one, when still referencing them in code by 0. If, for example, you were to display to a user "Quarter #X", you wouldn't want to display 0-3, but 1-4. This function retrieves an array for such a use.

    Also,

    "// Get today's date, and also format it in a more human-friendly way"

    was posted twice.

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

    Yeah, well you're a big poopyhead too.

    That's so childish! Besides, I am rubber, you are glue... ;)

  • Jay (unregistered) in reply to trtrwtf
    trtrwtf:
    Jay:
    BTW, even seemingly obvious constants are not always so obvious. Like, how many weeks are in a year? Obviously 52, right? Well, not really, there are 52 weeks plus an extra day, 2 in leap years. My previous employer dealt with that by every 4 or 5 years having 53 weeks in the calendar.

    365%52 != 0 Years aren't composed of weeks - a given date is within week N of a given year, determined by Julian date and your conception of where the week is bounded. If your calendar of the year has a set number of weeks you're doing something wrong.

    Ummm ... I think that was my point.

  • Jay (unregistered) in reply to Jerry
    Jerry:
    Jay:
    BTW, even seemingly obvious constants are not always so obvious. Like, how many weeks are in a year? Obviously 52, right? Well, not really, there are 52 weeks plus an extra day, 2 in leap years. My previous employer dealt with that by every 4 or 5 years having 53 weeks in the calendar.
    Weeks don't fit into months or years, months aren't all the same size, even years have a variable number of days... and don't get me started with hours minutes and seconds that don't multiply by something reasonable but instead by 24 or 60... human time reckoning is a mess!

    What is needed is to replace all time measurements and expressions by a simple integer counter of ttp://en.wikipedia.org/wiki/Planck_time since the Big Bang.

    OK we might have to show it in hex instead of decimal, so it doesn't take up quite so much room.

    Here's my suggestion: http://johansens.us/sane/offthewall/cal.htm

    Shut up, Akismet!

  • qbolec (unregistered)

    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

    but what I am really interested in is your debate about magic constants!

    P.S. in what language does it define a string of length 4?

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

Log In or post as a guest

Replying to comment #:

« Return to Article