• Naomi (unregistered)

    Before anyone decides to sass poor Remy about how this isn't a WTF, please remember that this site runs on reader submissions. He can only work with what he's given.

  • doubtingposter (unregistered) in reply to Naomi

    I've remembered, but I still want to sass about how this isn't a WTF. If there's no good WTF to show, don't show a non-WTF?

  • P (unregistered)

    No, TRWTF is that the submitter's coworker is incompetent at golfing. Even a novice knows that

    ($i%12?$i%12:12).($i/12>=1?'pm':'am')
    

    can be replaced with

    (1+($i+11)%12).($i>11?'pm':'am')
    

    which is even shorter and more clear.

  • Hanneman (unregistered)

    The WTF is not to use time as 00 to 24 hours. Sorts perfect. Date as yyyymmdd also sorts without problems ...

  • Foo AKA Fooo (unregistered)

    Apart from not using library date handling, and using AM/PM time in the first place, I also fail to see how that's a WTF. How would you do it, except doing basically the same, but more verbose (such as if/else) or a longish table? FWIW, I'd invert the condition and use "i < 12" to avoid the less obvious magic number 11.

  • Anon (unregistered)

    No need to use {} for single line for, $x?:$y is equivalent to $x?$x:$y, the 'm' is redundant, and there is too many white spaces, a better approach would be:

    <?php for($i=0;$i<24;$i++)echo '<th>
    '.($i%12?:12).($i>11?'p':'a').'m
    ';?>
  • Edd (unregistered)

    TRWTF is that there are formats other than ISO8601

  • The Ternary King (unregistered)

    I have no problem with this (apart from not using "$i>=12" or "$i>11" for the second condition). What are you proposing, using up 5 lines to render this single piece of markup? That may be 'clearer' in a very local context but that means there's 4 lines less code you can see to give you the bigger picture. It's pretty clear that that's an expression to render a time of some kind.

    The lack of white space is a bit grating to me but that's a code style issue, not a WTF.

  • Alex (unregistered)

    This feels like the kind of cleverness that is OK as long as it comes in discrete bite-sized chunks ("golf balls"?"). The important thing is separation of concerns: can you read the rest of the codebase without needing a deep understanding of what insanity the programmer got up to in each specific section. If so, you can just file the code chunk under "needs more braincells to understand" when you do a first-pass read through the code, and then go back and putt each golf ball in turn.

    Compare and contrast with a junior colleague of mine who read about functional programming and suddenly started framing everything as map() calls even when that was grossly inappropriate. (For-Case Paradigm 2: Functional Boogaloo.) In that case, the crazy touched every part of the code.

  • Dlareg (unregistered) in reply to The Ternary King

    Just yes an IDE that can collapse if statements. You have the bigger picture and clear core

  • Dlareg (unregistered) in reply to The Ternary King

    Just yes an IDE that can collapse if statements. You have the bigger picture and clear core

  • Vilx- (unregistered)

    This... really looks about as good as it can get. What would be the better alternative here? IAmPmFormmaterFactoryServiceGenerator?

  • (nodebb) in reply to P

    Even better you could take the common m out and get

    <?php for ($i = 0; $i < 24; $i++) { echo '<th>
    '.(1+($i+11)%12).($i>11?'p':'a').'m
    '; } ?>
  • Peter (unregistered)

    Not terrible. The annoying thing is relying on PHP's rules for truthiness for some of the logic. A comment would be nice, like 'format hour columns with AM/PM', so we know all this is deliberate.

  • ooOOooGa (unregistered)

    I think I recognize this as another of my submissions. checks version history Indeed it is.

    And yes, I am scraping the bottom of the barrel to find anything to submit. I guess that is an indication that I have been working here too long.

    Currently I have it rewritten (spacing changed for display reasons) as:

    for ($i = 0; $i < 24; $i++) {

    $hour = ($i % 12) ? $i % 12 : 12;

    $ampm = ($i / 12 >= 1) ? 'pm' : 'am';

    echo "$hour $ampm";

    }

    Though I may take a broken-out version of (1+($i+11)%12).($i>11?'pm':'am') under consideration.

  • ooOOooGa (unregistered) in reply to Vilx-

    I could create an 'hour' table in the database with numbers from 1 to 12, an 'ampm' table with two entries for 'am' and 'pm, then do a cross join to create the data set. That would leave the view file for display only.

    But I think that is going in the wrong direction.

  • (nodebb) in reply to P

    (1+($i+11)%12).($i>11?'p':'a').'m</div></th><th></th>'

    Removing the duplicated m saves a whole byte.

  • ooOOooGa (unregistered)

    And if we want to go in the wrong direction on the code golf, we could do:

    foreach (array_merge(array_map(function ($hour) { return "$hour am"; }, range(1, 12)), array_map(function ($hour) { return "$hour pm"; }, range(1, 12))) as $hourHeader) {

    echo "$hourHeader";

    }

    For anyone who doesn't think that TRWTF is PHP.

  • ooOOooGa (unregistered) in reply to sibtrag

    Unfortunately, replacing the empty th node with the colspan='2' actually increases the count by a couple of characters. Removing the extraneous div tags helps quite a bit though.

  • Brian (unregistered) in reply to Edd
    TRWTF is that there are formats other than ISO8601

    Paraphrasing xkcd:

    Problem: There are n competing standards. Solution: Let's make one complete and perfect standard than can replace them all. Problem: There are n+1 competing standards.

  • Appalled (unregistered)

    What am I missing? Especially in PHP. I would have done an 'i" loop for 24 iterations, starting at zero not 1, and simply output a Format(DateAdd("h", i, StartTime),"HHa"), or whatever the syntax is in PHP (I've been retired too long to remember off the top of my head).

  • Angela Anuszewski (google) in reply to Brian

    Yep, I was briefly in a IEEE working group to create a universal AC Adapter type of standard and basically someone said "USB is becoming that standard so why are we bothering?" Everyone agreed and quit.

  • Angela Anuszewski (google) in reply to Brian

    Yep, I was briefly in a IEEE working group to create a universal AC Adapter type of standard and basically someone said "USB is becoming that standard so why are we bothering?" Everyone agreed and quit.

    Addendum 2020-06-04 11:38: Stupid double post. I'm blaming the proxy.

  • tbo (unregistered)

    I get the feeling there are a lot of people on this site who don't understand PHP, so let me assure you, there absolutely is a much clearer way to do the same thing. I could type it, but I don't know the formatting for these comments, so it would look like crap.

  • ooOOooGa (unregistered) in reply to tbo

    Double line break between each code line or else it will combine the lines together into a paragraph. Don't try to do intenting, HTML will strip everything down to a single space anyway.

    But yes, always interested in seeing a better way of doing something.

  • Kleyguerth (github) in reply to Naomi

    Can we sass the submitter then?

    Is it a WTF because of a lack of whitespace? Maybe that code should be extracted into a function? Or does the submitter just have a personal vendetta against all ternaries?

  • Neveranull (unregistered)

    The best thing about this post is that in spite of all my years of coding professionally, I never heard of the term “code golf,” and had to look it up. From what I’ve seen In the industry, you get judged for how many lines of code you can deliver, so instead of coding an algorithm with the least amount of code, you would write the most lines of code possible.

  • James (unregistered)

    a branch to handle the fact that hour 0 should be printed as 12.

    Loops don't have to start at 0.

  • Abigail (unregistered) in reply to P

    (1+($i+11)%12) can be golfed to (($i%12)||12), saving a byte.

  • ooOOooGa (unregistered) in reply to Kleyguerth

    By all means, sass away. I can handle a day or two of good sassing every 6 months or so.

    The lack of space between the hour and the 'am'/'pm' was what caused me to go looking for the code generating the text. The lack of spacing in the code itself is just a minor irritation and a difference in coding style.

    My biggest problem with it is that the two inline embedded ternary statements made it about as hard to read as a regular expression. And that certainly wasn't what I was looking for when reading through the code trying to find what was generating the poorly formatted headers on this table.

  • WTFGuy (unregistered) in reply to Edd

    @Edd ref

    TRWTF is that there are formats other than ISO8601

    IMO the TR^2WTF is developers who somehow think they're going to reeducate 8 billion humans who speak 4,500 languages written in 400+ scripts used in 200+ countries to a) think like computers and b) all adopt a single date format.

    ISO8601 is a fine format for machine-to-machine communication. In fact it's currently THE agreed format, no matter how many legacy apps are still doing something else.

    But this solution is for rendering UI for meatspace. Meatspace is much more unruly. Whether you or I like that or not.

  • Nathan (unregistered)

    Building on P, sibtrag, and James, looping from 11 to 34 saves 5 more bytes in the hour portion at the cost of 1 in the for (and significant readability).

    `for ($i=11;$i<35;$i++) {

    echo '

    '.($i%12+1).($i<23?'a':'p').'m
    ';

    }`

  • Raj (unregistered) in reply to WTFGuy

    TRTRWTF is hand-coding html in PHP while there are excellent templating libraries like twig that do a much better job

  • sizer99 (google)

    This is code written by someone who just recently discovered ternaries.

    I'm not sure that's true. I've seen stuff like this from people who've been using them for years. This is just the level of competence they plateaued at.

  • Fnord (unregistered) in reply to James

    This one kind of does need to start at 0 (unless you want to add additional complication), because the sequence is 12 am (0), 1 am (1), ..., 11 am, 12 pm, 1 pm....

  • ooOOooGa (unregistered) in reply to James

    In this case the loop does need to start at zero, because hour zero (12:00 am) needs to come before hour one (1:00 am).

    I could rearrange the entire table to have the time go from 1:00 am (hour 1) in the first column, to 12:00 am (hour 0) at the end of the table in order to make the headings easier to generate, but that would make the rest of the table a lot harder to populate.

  • Worf (unregistered) in reply to WTFGuy

    No, the real WTF is not separating out presentation from logic. Internally the time should be ISO8601. Then the presentation layer can translate that down however you please. Some places want 24 hour time, 00-23, while others want 01-24. Yet others want 12AM-11PM, or 1AM-12AM. Or property, 12m-1AM...-12n-1PM-11PM. (12AM and 12PM are technically incorrect for midnight and noon, respectively. In fact, 12AM is a very ambiguous time since there are two meanings on the same day - is January 1, 2021 at 12AM referring to the moment between December 31, 2020 at 11:59:59PM and January 1, 2021 12:00:01AM, or between January 1, 2021 11:59:59PM and January 2, 2021 12:00:01AM ?

    How are you going to handle daylight saving?

    Thus there is no real sane way that will satisfy everyone - time is a very local thing. Handle it internally the standard way and leave it up to the presentation layer to actually convert and display properly. And you'll get daylight saving time handled for free as well.

  • Fun Time (unregistered)

    I would take two inputs, meridies and html template. Then make an array 12 items long, and map.

  • Foo AKA Fooo (unregistered) in reply to ooOOooGa

    This doesn't add any readability. It keeps the bad parts, including the questionable "$i / 12 >= 1" bit and just adds unnecessary one-time variables. Such variables don't improve the code structure, since to understand the code, you'll have to piece it all together mentally anyway.

    At best, they serve as comments about what the values mean. But even that is weak: "$ampm", that name is literally listing the possible values (rather than a concept or such), and you see the possible values in the expression anyway. And when dealing with 12-hour time, it should be immediately obvious that "% 12" must be the hour, so that name doesn't add much either. (Sure, the computation with "% 12" may be correct or incorrect, but that's the same with and without the extra variable, so it doesn't help.)

    So, no readability, just more LOC. Congratulations on your raise.

  • David Mårtensson (unregistered) in reply to ooOOooGa

    Been there, done that, bad idea ;)

    Not exactly, but once we used to have a dates table with every date in to be able to join with an events table so that every date had at least one row with the date.

    That proved to be a very very stupid bad idea that came back to bite us until we abolished it :)

    Never put computable data like dates or hours into the database ;)

  • (nodebb) in reply to ooOOooGa

    Great idea, bolting on a database would make it more enterprisey!

  • (nodebb)

    All IMHO, but both approaches are fundamentally wrong.

    Build your data then render your data. Never mix building and rendering.

    If your data is about dates/times, use dates/times:

    $data = array(); for( $x=0; $x<606024; $x = $x + (60*60) ) $data[] = $x;

    $h = ''; foreach( $data as $d ) $h .= "

    " . date(ha, $d) . "
    ";

    No ternaries, no extra code branches. Built-in date/time functions used for handling times, meaning no extra "code golf". And the ability to log or otherwise examine the data being rendered if there are issues to debug. The code follows the data, not the other way around.

  • The Ternary King (unregistered) in reply to ooOOooGa

    Currently I have it rewritten (spacing changed for display reasons) as ... 5 lines instead of 3, for no increase in clarity (and you've still got the weird second condition lol). So all you've done is made it take up more space, hiding other code from the page. What does breaking that line into three actually gain you? Anyone can see that it's formatting an 'hour' and an 'ampm' from the original already ...

  • The Ternary King (unregistered) in reply to Worf

    the real WTF is not separating out presentation from logic

    This entire post is about the presentation layer, isn't it?

  • The Ternary King (unregistered) in reply to Dlareg

    Just yes an IDE that can collapse if statements

    Then you can't see this code at all! How is that any better?

    When you're having to appeal to your IDE to contract your code, because your coding standard forbids you from expressing the code concisely, then the WTF is the coding standard that mandated multiple lines where one will do.

  • (nodebb) in reply to Worf

    And you'll get daylight saving time handled for free as well.

    Nope.

    I've seen a ton of event lists that go screwy the day after a time change. Things like "Betty, you were an hour late yesterday, you punched in at 9:00." It turns out that Betty works remotely two timezones away and the software shows local time, because Betty can sleep until 10:45 in Los Angeles... as long as she gets on the phones by 8:00am New York time. So we can't display the punch time in the time zone the punch was acquired, we have to display it in New York time. But now, we end up with very strange time zone rules like:

    • Store the punch in as local time - with time zone
    • Display the punch in call center time zone
    • Remember to correct the DST flag on the time zone to reflect the DST state at the time of the punch.

    You can reduce it to two steps by converting to UTC when storing, but you certainly didn't "get daylight savings time handled for free". ISO 8601 has too many representations of the same time. Also, "convert to UTC when storing" is a rule on top of "use ISO 8601".

    BTW, ISO 8601 in general makes my angry. The only reason we picked a standard text representation for dates is because javascript (and therefore JSON), only formally recognizes strings and numbers as literal primitives. If the first draft of JSON did actual deserialization instead of exec'ing the JSON blob, we wouldn't be in this mess. Back in the days when we didn't care what was on the wire, our protocols handled this for us. I've been using databases for forty years and I've never had a database client flip the months and days on me as it fetched actual date typed data from a server. When APIs started introducing SOAP support in the late 90s, no one ever had to think about how the system was formatting their dates - it was date-in, date-out, always worked.

  • Ulysses (unregistered) in reply to Naomi

    Before anyone decides to sass poor Remy about how this isn't a WTF, please remember that this site runs on reader submissions. He can only work with what he's given.

    Well I'm still waiting for the obstinator to post my article, which has been repeatedly bypassed for some really 'slow news day' stuff.

  • markm (unregistered)

    Code golf seems to me to be quite similar to obfuscated c - which was a challenge to find the least apparent algorithm possible, then to jam the code into one unreadable line. (And c can be very unreadable.) Neither one of these should ever be seen in production code; good code is obvious! In this case, the algorithms weren't bad, although the condition tests could have been simplified. But the code was written as if the goal was to be unreadable.

    It should be possible to use line breaks, indentation, and comments to make a ternary as clear as the well-written and commented if-then-else equivalent, but I find reading the ternary to always take a little more effort.

    That leaves the question as to whether there is any advantage to the ternary. At least in c, the generated code should be the same either way, and . The compiler must translate both structured 'if' and a ternary to machine code, using the equivalents of "if(condition) goto label" and "goto label" instructions. Sometimes (if the ternary result is used directly rather than being explicitly assigned to a variable) the if-then-else version will require defining a variable just to hold the result temporarily, but in translating the ternary to branches of the code, the compiler must create a hidden temporary variable - and either way, a good compiler will use a register.

    In PHP, I have no idea what creates efficient code...

  • (nodebb)

    You've all missed the final byte saving: 'ap'[+($i>11)] (or 23 if you're using Nathan's loop). If you turn off warnings, you can also remove the +().

  • Jake (unregistered)

    perl -le 'print"";print"",$%12||12,$>11?"p":"a","m"for 0..23;print""'

Leave a comment on “Scheduling your Terns”

Log In or post as a guest

Replying to comment #:

« Return to Article