I have read (and generally agree) that to increase code legibility, you should use constants instead of magic numbers as method parameters. For example, using PHP:
// no constants ////////////////////
function updateRecord($id) {
if ($id == -1) {
// update all records
} else {
// update record with "id = $id"
}
}
updateRecord(-1); // future maintainer says: "wtf does -1 do?"
// and then has to jump to the function definition
// with constants: /////////////////
define('UPDATE_ALL', -1);
function updateRecord($id) {
if ($id == UPDATE_ALL) {
// update all records
} else {
// update record with "id = $id"
}
}
updateRecord(UPDATE_ALL); // future maintainer says: "woot"
Yeah, it's not a great example, I know...
So, I can see how this is a Better Thing, but it raises the question of how often you should do this? If it is for every function, you'd end up with a metric shirtload of constant definitions.
Where would you draw the line? Stick with constants over magic numbers all the way, or take a blended approach depending on the usage of the function in question?