Using numeric HTTP status codes like 403
, 404
, or 500
makes code less readable and more prone to errors.
These "magic numbers" require developers to memorize or look up what each code means, reducing code clarity.
Symbolic status codes like :forbidden
, :not_found
, or :internal_server_error
are self-documenting. They
immediately convey the meaning without requiring knowledge of HTTP status code numbers. This makes the code more maintainable and easier to understand
for developers at all experience levels.
Additionally, symbolic codes reduce the risk of typos. It’s easier to catch a misspelled symbol than an incorrect number, and Ruby will raise an
error for undefined symbols, providing immediate feedback during development.
What is the potential impact?
Using numeric status codes reduces code readability and maintainability. While this doesn’t create security vulnerabilities or runtime errors, it
makes the codebase harder to understand and maintain. Developers may need to spend extra time looking up status code meanings, and there’s a higher
risk of introducing bugs through numeric typos that might not be immediately obvious during code review.