Wikipedia's article on Magic Numbers suggests that any constant should be assigned to a variable with a meaningful name.
Does the same practice apply to HTTP status codes, which are a well-defined standard? That is, when writing tests, should one do assert response.status_code == HTTP_STATUS_CODE_SUCCESS
or is assert response.status_code == 200
appropriate in this context?
What's a magic number? A magic number is a number in the code that seems arbitrary and has no context or meaning. This is considered an anti-pattern because it makes code difficult to understand and maintain. One of the most important aspects of code quality is how it conveys intention.
Magic Number in Mathematics For example, 325 is a magic number because the sum of its digits (3+2+5) is 10, and again sum up the resultant (1+0), we get a single digit (1) as the result. Hence, the number 325 is a magic number. Some other magic numbers are 1234, 226, 10, 1, 37, 46, 55, 73, etc.
The Status-Code element in a server response, is a 3-digit integer where the first digit of the Status-Code defines the class of response and the last two digits do not have any categorization role. There are 5 values for the first digit: S.N. It means the request has been received and the process is continuing.
HTTP response status codes indicate whether a specific HTTP request has been successfully completed. Responses are grouped in five classes: Informational responses ( 100 – 199 ) Successful responses ( 200 – 299 ) Redirection messages ( 300 – 399 )
Using a constant you defined is a good idea. Using one defined by the language is a better idea. I'm not sure what language you are using but most languages have one. Java C# python
Yes, magic numbers are magic numbers even if they are well known and well documented.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With