When to use constants as parameters instead of magic values

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?

+4
source share
5 answers

As you have already indicated, future companions will thank you for your clear naming. This companion may be for you, I am amazed again and again about how much I forget about my own code and how difficult it is to understand it again when I have not worked on it for some time.

I would definitely go with constants all the way, as soon as the area is larger than maybe one short method. Once you start passing these values, IMHO they should be defined as a constant. There may be a comment inside the method. This, however, does not help any caller of your code who does not see this comment. Even another method in the same class should be considered as an "API client", which should not be aware of the implementation details of other methods that it calls in this regard.

With languages โ€‹โ€‹that support "real" enumerations (for example, the Java enum keyword introduced in Java 5), โ€‹โ€‹you even get type safety and do not risk uniqueness problems that can have, for example, integer-based constants.

+3
source

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.

If each function accepts "magic parameters", then you are already doing this terribly wrong.

I would always use constants. If you think this means that you have too many constants, then this simply reflects other flaws in your design.

+5
source

I would use it where it is ..

  • improves readability.
  • help you remember
  • allows you to immediately see what you pass as an argument

Take PHP sort () for example. It makes sense:

 sort($array, SORT_NUMERIC); 

But will it be?

 sort($array, 2); // Haven't actually dug in to see what it matches, but you get the point 
+2
source

Well, this is less of a problem if you port things to classes and use class constants. I rarely use global constants other than paths of things.

+1
source

You should try to keep literals in your code to the absolute minimum: each literal is a potential problem because your environment may change, and other developers may not know what that means.

When I start a project, I always dedicate a file that will be used only for named constants, often wrapped up in a class dedicated to such, and I use them with liberal use, as this requires my work. All named constants are in this file and managed from this file, which gives you excellent organization and control over your named constants. You can also organize them into groups with comments or regions of the code, and depending on the language, nests and building one on the other.

This practice has helped me endless times over the years.

0
source

Source: https://habr.com/ru/post/1301068/


All Articles