The Daily WTF
Mike has a co-worker who’s better at Code Golf than I am. They needed to generate a table with 24 column headings, one for each hour of the day, formatted in HAM- the hour and AM/PM. As someone bad at code golf, my first instinct is honestly to use two for loops, but in practice I’d probably do a 24 iteration loop with a branch to decide if it’s AM/PM and handle it appropriately, as well as a branch to handle the fact that hour 0 should be printed as 12.
Which, technically, more or less what Mike’s co-worker did, but they did in in golf style, using PHP.
<tr> <?php for ($i = 0; $i < 24; $i++) { echo ‘<th><div>’.($i%12?$i%12:12).($i/12>=1?’pm’:’am’).'</div></th><th></th>’; } ?> </tr>
This is code written by someone who just recently discovered ternaries. It’s not wrong. It’s not even a complete and utter disaster. It’s
To read the full article click on the 'post' link at the top.