Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are HTTP status codes considered "magic numbers"?

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?

like image 447
exupero Avatar asked Dec 02 '11 14:12

exupero


People also ask

What are considered magic numbers?

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.

What is magic number example?

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.

Are HTTP status codes integers?

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.

What do the HTTP status codes mean?

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 )


2 Answers

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

like image 197
Sign Avatar answered Oct 25 '22 16:10

Sign


Yes, magic numbers are magic numbers even if they are well known and well documented.

like image 33
Guffa Avatar answered Oct 25 '22 16:10

Guffa