Weekday weirdness



  • I'm working my first real programming job at a small PHP shop. It consists of the two owners, me and another junior programmer. The owners are both self-taught and one of them in particular has been at it for a while, he started back in 2000 or 2002 as a newbie. There's still a good bunch of code around from the ye olden days, so on occasion I stumble upon a bit of TDWTF-worthy material. We've all had to start at some point or other (like for instance, I'm doing), so I never think much of it. The guys are much better coders these days, and I'm sure I'm inadvertently responsible for some WTF'ery of my own that may jump up to bite me in the butt one day.

    However, I found some code today that made me do a quintuple-take. I just couldn't resist sending it in, but it hasn't been posted yet so perhaps it's a good one for the sidebar. We code "in Dutch" and there's a lot of distracting fluff in the original code, so I've redacted it a bit to make the essence clear (and to protect those who have this on their conscience):

     

    for ($i = 1; $i < (!isset($j) ? $j = 7 : $j); $i++) {
        $day_abbreviation = strtolower(substr($bcc['daynames'][$i], 0, 2));
        $output .= do_stuff_with_a_weekday(ucfirst($day_abbreviation));
        if ($i == 6) {
            $i = -1;
            $j = 1;
        }
    }
    To make one thing clear: $j is never defined in the code before this point. The loop looked so weird to me, that it took me a while before I understood what the original programmer must have been thinking (undoubtedly late in the afternoon): in PHP the weeks start on a Sunday, but the programmer needed to iterate the weekdays so they start on a Monday (as, of course, is customary in the business world). So, for the sake of my own sanity, I rewrote the loop to look like this:

    for ($i = 1; $i <= 7; $i++){
        $day_abbreviation = strtolower(substr($bcc['daynames'][$i % 7], 0, 2));
        $output .= do_stuff_with_a_weekday(ucfirst($day_abbreviation));
    }




  • @toon said:

    for ($i = 1; $i < (!isset($j) ? $j = 7 : $j); $i++)
    Just when you think you've seen it all...



  • @DOA said:

    @toon said:

    for ($i = 1; $i < (!isset($j) ? $j = 7 : $j); $i++)
    Just when you think you've seen it all...

    The really weird part, to me, is that the original programmer didn't think of simply putting a line "$j = 7" before the loop. But again, it must be old code written at the end of a long day. TRWTF is that the guy needed the same code in a bunch of other places in the app, and copy-pasted it. So there were four or five of those in the code.



  • Actually, I found it last Tuesday, not today. Sorry...


  • Trolleybus Mechanic

    Makes sense. You want to lazily instantiate your integes. Those things take up a lot of cycles. I think because PHP has to start with a float, then use the cpu fan to mill off the decimal point. 



  • @Lorne Kates said:

    Makes sense. You want to lazily instantiate your integes. Those things take up a lot of cycles. I think because PHP has to start with a float, then use the cpu fan to mill off the decimal point. 

    That was beautiful, not because of its absurdity but because PHP would probably do things that way if it was physically possible.



  • $j was probably taken out entirely at some point which is why you're wondering "huh what's going on here?"

    These kinds of strange code snippets are usually the equivalent of

    debug = false;
    ...
    
    if(debug) {
      ..
      doSomethingForDebugging();
      ..
    }
    

    It's a little obnoxious and should have been refactored but it doesn't really jump out.



  • @The_Assimilator said:

    @Lorne Kates said:

    Makes sense. You want to lazily instantiate your integes. Those things take up a lot of cycles. I think because PHP has to start with a float, then use the cpu fan to mill off the decimal point. 

    That was beautiful, not because of its absurdity but because PHP would probably do things that way if it was physically possible.

     

    Actually, PHP would probably be built to do it that way and to do it by trimming off the fraction by guillotining it with the CD drawer. There would be two distinct syntaxes for the two mechanisms (one procedural, one looking more object-oriented), each with its own representation of floats and rounding rules, and a few functions/methods that do a partial and approximate mapping between the two.

     

     



  • (!isset($j) ? $j = 7 : $j)

     

    I'm torn between laughing and being impressed by the innovation. :P

     


  • @Volmarias said:

    $j was probably taken out entirely at some point which is why you're wondering "huh what's going on here?"

    These kinds of strange code snippets are usually the equivalent of

    ...

    It's a little obnoxious and should have been refactored but it doesn't really jump out.

    The short answer is: no.

    There really are very, very few for loops in our codebase. I mentioned that there are 4 or 5 of these loops in the app? Those are very likely the only ones. I've never seen an app of ours with this many. The reason is that PHP has a foreach loop that we use quite a lot (and makes more sense than a vanilla for loop, about 99% of the time).

    I've seen older code of ours that uses a foreach loop with a $i variable, which is used as a counter to keep track of which record you're dealing with. Why? Because the guys simply weren't familiar with for loops. I used to code C++ in high school, so to me that screams "WTF". But the guys have another frame of reference entirely, so honestly, I can't blame them.

    Also, the $j variable definitely has meaning and purpose in this context. In fact, if it were instantiated elsewhere in the code path, it might break this loop since there is no unset($j) before it (another WTF to be sure).



  • @Lorne Kates said:

    Makes sense. You want to lazily instantiate your integes. Those things take up a lot of cycles. I think because PHP has to start with a float, then use the cpu fan to mill off the decimal point. 

    Laughing so hard it hurts.



  • @Arancaytar said:

    (!isset($j) ? $j = 7 : $j)

    I'm torn between laughing and being impressed by the innovation. :P 

    A wtf in itself.



  • It does work, and it's not totally ugly, but using it for showing the days fo the week is a bit of a WTF. I mean, when did a week last have any more or less than 7 days, unless the code was meant to be able to show partial weeks (at the start or end of a month, for example) in which case there is no WTF and this could just be a remnant of that code. Berate the guy for not removing obselete code sure, but I think we've all been guilty of it at some time or another.

    Presonally I wouldn't do it this way, because the loop would need to make a check on the existance of $j every iteration and then compare $i against it. Much better to instantiate $j outside of the loop and take out the unecessary checking each time the loop runs, but it does solve a problem, and for a loop this size it's not really going to be noticeable.



  • Just some off topic and unsolicited advice: You should probably not start your programming career at a place where noone knows and cares about building things the right way.. Friend of mine started at a similar company and got stuck learning everything the wrong way for 2 years before moving to the company i worked at and he had to start all over..



  • @toon said:

    $bcc['daynames'][$i]

    I'm a bit lost: what is bcc? Was it possible to just set $bcc['daynames'][0]="monday", ... , $bcc['daynames'][6]="sunday" if they needed the week to start on monday?



  • @toon said:

    The owners are both self-taught and one of them in particular has been at it for a while, he started back in 2000 or 2002 as a newbie.

    To some of us, a 10 or 12 years' experience with self-taught PHP would make him still a newbie.

     

     



  • @dargor17 said:

    @toon said:

    $bcc['daynames'][$i]

    I'm a bit lost: what is bcc? Was it possible to just set $bcc['daynames'][0]="monday", ... , $bcc['daynames'][6]="sunday" if they needed the week to start on monday?

    I genuinely have no idea what bcc stands for. $bcc is like an uberglobal that has all the "configuration", and many of the constants of the app in it. So it might have the name of the client, database connection information, etc, as well as stuff like, for instance, weekday names, enum-like arrays etc. So odds are somewhere in the app, some code might rely on the fact that the week starts on a Sunday.



  • @Timm said:

    Just some off topic and unsolicited advice: You should probably not start your programming career at a place where noone knows and cares about building things the right way.. Friend of mine started at a similar company and got stuck learning everything the wrong way for 2 years before moving to the company i worked at and he had to start all over..

    Unsolicited, but not unwelcome. I think everyone at our firm cares about doing things the right way. But "the right way" can mean different things depending on the context. We all want our software to be scalable and stable. So, in that sense, everybody in our firm definitely cares about doing things the right way. Now let's just say that your point is not lost on me, and then I think we'll agree that we understand one another.

    I'm around 30 and have coding experience as a hobbyist, and I like to think I grasp the basics of software. I know a lot of background stuff for a beginning PHP programmer. For instance, I knew that when I saw that a guy wrote a function that keeps generating a random number until it does not equal some number that's passed as a parameter, I needed to 'waste some time on trivial stuff', take one for the team and make it nonrecursive (let that one sink in for a moment). Sure, the odds are negligible. But what's that site called again, you know: where you end up when you google programming stuff? You know the one I mean. Nobody wants to risk that?

    So I know about software, I can think analytically, and what have you. Problem is, I had zilch to show for it until I landed this job, because I'm a college dropout. After a number of years with the guys, I'll be a better programmer; my own WTF-to-LOC ratio will go down and I'll not only be able to code, but to make time estimates, and generally know how to be a professional programmer. I don't think anyone in the world expects me to be there 10 more years, but I like the boys, I like my job and if I'll have to unlearn some stuff, so be it. It's the only way I can make a career in software development, and in 10 years I hope to be in a position where I have learned, and am able to apply best practices, OOP, test-driven programming, source control, all of those things that my current job lacks.



  •  @toon said:

    uberglobal that has all the "configuration", and many of the constants of the app in it

    Fascinating... Very, very fascinating...



  • @Timm said:

    You should probably not start your programming career at a place where noone knows and cares about building things the right way.. Friend of mine started at a similar company and got stuck learning everything the wrong way for 2 years before moving to the company i worked at and he had to start all over..

    Sometimes, being exposed to the "wrong way" first gives one a deeper appreciation of doing things the "right way", since it not only offers a comparison but allows a developer to spot their previous "wrong way". Of course, that presumes that a developer is able to unlearn old habits in addition to taking on new ones.

    Now, if only there was a site that documented all these Wrong Techniques (Fundamentally) then other people could learn by those mistakes...



  • @Cassidy said:

    @Timm said:

    You should probably not start your programming career at a place where noone knows and cares about building things the right way.. Friend of mine started at a similar company and got stuck learning everything the wrong way for 2 years before moving to the company i worked at and he had to start all over..

    Sometimes, being exposed to the "wrong way" first gives one a deeper appreciation of doing things the "right way", since it not only offers a comparison but allows a developer to spot their previous "wrong way". Of course, that presumes that a developer is able to unlearn old habits in addition to taking on new ones.

    Now, if only there was a site that documented all these Wrong Techniques (Fundamentally) then other people could learn by those mistakes...

    C2 Wiki.


Log in to reply