Replace magic numbers with constants #3139

Closed
opened 2025-12-29 18:26:03 +01:00 by adam · 3 comments
Owner

Originally created by @lampwins on GitHub (Jan 9, 2020).

Originally assigned to: @jeremystretch on GitHub.

Proposed Changes

We have a number of instances in which we define "magic numbers" with little or no provided context. Particularly surrounding numeric min/max validators. We should properly define these in constants.py instead.

Justification

Defining these values as constants will allow for better descriptive use and also proper reuse throughout the codebase.

Originally created by @lampwins on GitHub (Jan 9, 2020). Originally assigned to: @jeremystretch on GitHub. <!-- NOTE: This type of issue should be opened only by those reasonably familiar with NetBox's code base and interested in contributing to its development. Describe the proposed change(s) in detail. --> ### Proposed Changes We have a number of instances in which we define "magic numbers" with little or no provided context. Particularly surrounding numeric min/max validators. We should properly define these in constants.py instead. <!-- Provide justification for the proposed change(s). --> ### Justification Defining these values as constants will allow for better descriptive use and also proper reuse throughout the codebase.
adam added the status: acceptedtype: housekeeping labels 2025-12-29 18:26:03 +01:00
adam closed this issue 2025-12-29 18:26:03 +01:00
Author
Owner

@jeremystretch commented on GitHub (Jan 9, 2020):

Just want to point out that the BGP ASN min/max values (#3876) weren't arbitrary; they're governed by the protocol. But yes, it makes sense to declare them as constants. What other examples did you have in mind?

Also: As part of the migration from integers to slug for ChoiceFields in v2.7, most of the old "magic numbers" have been removed and in some cases constants.py has been deleted (choice values were moved to choices.py within each app). I have no problem reinstating constants.py as needed though.

@jeremystretch commented on GitHub (Jan 9, 2020): Just want to point out that the BGP ASN min/max values (#3876) weren't arbitrary; they're governed by the protocol. But yes, it makes sense to declare them as constants. What other examples did you have in mind? Also: As part of the migration from integers to slug for ChoiceFields in v2.7, most of the old "magic numbers" have been removed and in some cases `constants.py` has been deleted (choice values were moved to `choices.py` within each app). I have no problem reinstating `constants.py` as needed though.
Author
Owner

@hSaria commented on GitHub (Jan 9, 2020):

I've found the following so far:

  • Interface's MTU
  • Service's port
  • VLANs

The MTU would benefit from the constants as the form widgets have a lower max value than the model validators.

@hSaria commented on GitHub (Jan 9, 2020): I've found the following so far: * Interface's MTU * Service's port * VLANs The MTU would benefit from the constants as the form widgets have a lower max value than the model validators.
Author
Owner

@lampwins commented on GitHub (Jan 9, 2020):

Really it boils down to, in most cases when we are defining a static value in the codebase, that value should be a variable. As @hSaria pointed out, there are several other instances of numeric validation and I will note that in those cases the values tend to be duplicated within both the model validation and the forms. There are some other things like in several instances when we define a color field, it has a length of 6.

I think this issue can be covered by a sweeping pass over the codebase and then, of course, going forward make it a development policy for any new values.

This is all about making the life of developers easier and making the codebase more maintainable as a whole, so to that end, we should still exercise good judgment in deciding if converting some value to a constant is actually more cumbersome than it is worth, to not do it then. For instance, several fields define a max_length of 100 but I would argue in that context it does not make sense to convert.

@lampwins commented on GitHub (Jan 9, 2020): Really it boils down to, in _most_ cases when we are defining a static value in the codebase, that value should be a variable. As @hSaria pointed out, there are several other instances of numeric validation and I will note that in those cases the values tend to be duplicated within both the model validation and the forms. There are some other things like in several instances when we define a color field, it has a length of 6. I think this issue can be covered by a sweeping pass over the codebase and then, of course, going forward make it a development policy for any new values. This is all about making the life of developers easier and making the codebase more maintainable as a whole, so to that end, we should still exercise good judgment in deciding if converting some value to a constant is actually more cumbersome than it is worth, to not do it then. For instance, several fields define a max_length of 100 but I would argue in that context it does not make sense to convert.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#3139