

How I got three errors into one line of code

At work we had this script that was trying to report how long it had taken to run, and it was using DateTime::Duration : my $duration = $end_time->subtract_datetime($start_time); my ( $hours, $minutes, $seconds ) = $duration->in_units( 'hours', 'minutes', 'seconds' ); log_info "it took $hours hours $minutes minutes and $seconds seconds to run" This looks plausible, but because DateTime::Duration is shit, it didn't work. Typical output: it took 0 hours 263 minutes and 19 seconds to run I could explain to you why it does this, but it's not worth your time. I got tired of seeing 0 hours 263 minutes show up in my cron email every morning, so I went to fix it. Here's what I changed it to: my $duration = $end_time->subtract_datetime_absolute($start_time)->seconds; my ( $hours, $minutes, $minutes ) = (int(duration/3600), int($duration/60)%60, $duration%3600); I was at some pains to get that first line right, because getting DateTime to produce a useful time interval value is a tricky proposition. I did get the first line right. But the second line is just simple arithmetic, I have written it several times before, so I dashed it off, and it contains a syntax error, that duration/3600 is missing its dollar sign, which caused the cron job to crash the next day. A co-worker got there before I did and fixed it for me. While he was there he also fixed the $hours, $minutes, $minutes that should have been $hours, $minutes, $seconds . I came in this morning and looked at the cron mail and it said it took 4 hours 23 minutes and 1399 seconds to run so I went back to fix the third error, which is that $duration%3600 should have been $duration%60 . The thrice-corrected line has my ( $hours, $minutes, $seconds ) = (int($duration/3600), int($duration/60)%60, $duration%60); What can I learn from this? Most obviously, that I should have tested my code before I checked it in. Back in 2013 I wrote: Usually I like to draw some larger lesson from this sort of thing. … “Just write the tests, fool!” This was a “just write the tests, fool!” moment if ever there was one. Madame Experience runs an expensive school, but fools will learn in no other. I am not completely incorrigible. I did at least test the fixed code before I checked that in. The test program looks like this: sub dur { my $duration = shift; my ($hours, $minutes, $seconds ) = (int($duration/3600), int($duration/60)%60, $duration%60); sprintf "%d:%02d:%02d", $hours, $minutes, $seconds; } use Test::More; is(dur(0), "0:00:00"); is(dur(1), "0:00:01"); is(dur(59), "0:00:59"); is(dur(60), "0:01:00"); is(dur(62), "0:01:02"); is(dur(122), "0:02:02"); is(dur(3599), "0:59:59"); is(dur(3600), "1:00:00"); is(dur(10000), "2:46:40"); done_testing(); It was not necessary to commit the test program, but it was necessary to write it and to run it. By the way, the test program failed the first two times I ran it. Three errors in one line isn't even a personal worst. In 2012 I posted here about getting four errors into a one-line program. [ Addendum 20170215: I have some further thoughts on this. ]

[Other articles in category /oops] permanent link

