DEV Community

Codanyks
Codanyks

Posted on

1 1 1 1 1

🧼 Clean Code Tip: Don’t Hardcode HTTP Status Codes – Use Named Constants Instead

Stop Writing Cryptic Code – Let Your Code Speak Clearly

If you've ever written res.status(200) in an Express route and thought, "I hope someone understands what this means"—you're not alone.

Hardcoded HTTP status codes are everywhere. They work, but they're not expressive. They force developers to memorize what numbers like 201, 403, or 422 actually mean. That's fine for machines—but humans deserve better.

Clean code is all about clarity and communication. Your code should explain itself without needing comments or cheat sheets.

That's where http-response-status-code comes in—a simple package that replaces confusing numbers with meaningful names like HTTP_CODE_404 and NOT_FOUND.

Let's explore how this tiny change can make a huge difference in the readability and quality of your codebase.


Why Avoid Hardcoded Status Codes?

Here's a quick comparison:
Before (hardcoded):

res.status(404).json({ error: "Not found" });
Enter fullscreen mode Exit fullscreen mode

After (clean & expressive):

import { NOT_FOUND } from 'http-response-status-code';

res.status(NOT_FOUND).json({ error: "Not found" });
Enter fullscreen mode Exit fullscreen mode

🔹 Which one is easier to read at a glance?
🔹 Which one looks more intentional?


Introducing http-response-status-code

This handy package gives you named constants for all standard HTTP status codes—both by number (HTTP_CODE_200) and by label (OK, NOT_FOUND, etc.).

npm install http-response-status-code
Enter fullscreen mode Exit fullscreen mode

Use Case: Express Example

import express from 'express';
import {
  HTTP_CODE_200,
  BAD_REQUEST,
  INTERNAL_SERVER_ERROR,
} from 'http-response-status-code';

const app = express();

app.get('/ping', (req, res) => {
  res.status(HTTP_CODE_200).json({ message: 'pong' });
});

app.use((err, req, res, next) => {
  const status = err.isClientError ? BAD_REQUEST : INTERNAL_SERVER_ERROR;
  res.status(status).json({ error: err.message });
});

Enter fullscreen mode Exit fullscreen mode

This is much more readable than trying to remember which number means what.


Use Meaningful Constant Names

This idea applies everywhere.

Instead of:

const TIMEOUT = 5000;
Enter fullscreen mode Exit fullscreen mode

Prefer:

const REQUEST_TIMEOUT_MS = 5000;
Enter fullscreen mode Exit fullscreen mode

Clear naming saves time, prevents bugs, and helps onboard new developers faster.


Bonus: Dual Exports

The http-response-status-code package exports both number-based constants and label-based ones.

So you can use:

import { HTTP_CODE_404, NOT_FOUND } from 'http-response-status-code';

console.log(HTTP_CODE_404); // 404
console.log(NOT_FOUND);     // 404
Enter fullscreen mode Exit fullscreen mode

Both are valid—it’s up to your team's style guide or preference.


Final Thoughts

Clean code is about making your intent obvious.
Named constants like HTTP_CODE_400 and INTERNAL_SERVER_ERROR improve readability, reduce cognitive overhead, and make your code easier to maintain.

So next time you're about to write res.status(500), reach for a name instead of a number.

👉 Try http-response-status-code and make your code speak for themselves.

Top comments (0)