The other day I was updating an algorithm for searching some data elements. The general structure of the code was something like this (btw we use Perl):
my $from = Date::Utility->new($finput)->epoch; my $to = Date::Utility->new($tinput)->epoch; #... my $result = $chronicle_reader->get_for_period("category1", "title1", $from->minus_time_interval("1d"), $to->plus_time_interval("1d")); return $result;
Did you notice the problem with above code?
Epoch is an integer number. Both $from and $to variables store epochs, so they have integers. I was assuming their type to be Date::Utility which has some utility methods (E.g. plus_time_interval). Problem is, the above code does not cause any error or warning by Perl interpreter until that specific line is executed. Of course this could be prevented by writing a unit test which covered that line, but hey! why write a unit test just to do what can be done automatically by software?
Had we used a static-typed programming language like Java or C#, this would be detected upon compiling the code. Of course using Perl has it’s own advantages but detecting such issues is not one of them.