The value of meaningful identifiers in variables
0I found the following code today that reminded me of my first attempt at programming in college. I was having a terrible time with it and was convinced I did not want to do programming as a career. Unfortunately, the code I’d written was based off an example in a book, which looked very much like the below code I found today while at work.
function calculate_easter($y) { $a = $y % 19; $b = intval($y / 100); $c = $y % 100; $d = intval($b / 4); $e = $b % 4; $f = intval(($b + 8) / 25); $g = intval(($b - $f + 1) / 3); $h = (19 * $a + $b - $d - $g + 15) % 30; $i = intval($c / 4); $k = $c % 4; $l = (32 + 2 * $e + 2 * $i - $h - $k) % 7; $m = intval(($a + 11 * $h + 22 * $l) / 451); $p = ($h + $l - 7 * $m + 114) % 31; $EasterMonth = intval(($h + $l - 7 * $m + 114) / 31); // [3 = March, 4 = April] $EasterDay = $p + 1; // (day in Easter Month) return format_date($y, $EasterMonth, $EasterDay); }
I can’t make heads or tails of this code and leaves me thinking the person who wrote it was either very smart or very dumb. I’d lean towards the latter.
When I asked my professor for help and showed him my program full of single letter variable names, he said “have you ever heard of meaningful identifiers?”. He should have given me a smack on the head as he said it. Today, I don’t mind writing out long variable names or functions names as long as they “meaningfully” describe the variable or function. Trust me on this – it will make code maintenance much easier in the long run and will make the lives of other developers looking at your code much less painful.
A side note – the author of the above code could have just used PHP’s built in easter_date function http://us2.php.net/easter_date .
Hi I'm Nick Bartlett and thanks for visiting my blog. I'm not much of a writer; many of my posts are short and to the point while others are meant to be a reference for myself and other web developers.