I read (and generally agree) that to increase the clarity of the code, 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"
Yes, this is not a great example, I know ...
So, I see how it is better, but the question arises, how often do you have to do this? If this is for each function, you will get a metric shirt with constant definitions.
Where would you draw the line? Insert constants by magic numbers completely or take a mixed approach depending on the use of the function in question?
source share