DEV Community

Abhay Singh Kathayat
Abhay Singh Kathayat

Posted on

1 1 1 1 1

Just Stop Writing Node Functions Like This!

Node.js is powerful, but many developers write inefficient, unreadable, or unscalable functions. If you recognize any of these bad practices in your code, it's time for a change!

  1. Writing Functions Without Proper Naming Bad Example: function data(d) { console.log(d); } Why it's bad: The function name is vague, and it just logs data without context. Better Approach: function logData(data) { console.log(data); } ✔ Use a clear, descriptive name ✔ Avoid single-letter variables

  1. Using Global Variables Inside Functions Bad Example: let globalCount = 0;

function increment() {
globalCount++;
}
Why it's bad: Modifying global state makes debugging difficult and leads to unpredictable behavior.
Better Approach:
function increment(count) {
return count + 1;
}
✔ Keep state encapsulated ✔ Make functions pure whenever possible


  1. Ignoring Async/Await in Promises Bad Example: function fetchData() { return fetch('https://api.example.com') .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error(error)); } Why it's bad: Nested .then() chains make it harder to read and maintain. Better Approach: async function fetchData() { try { const response = await fetch('https://api.example.com'); const data = await response.json(); console.log(data); } catch (error) { console.error(error); } } ✔ Use async/await for better readability ✔ Handle errors with try/catch

  1. Not Handling Errors Properly Bad Example: function divide(a, b) { return a / b; } Why it's bad: It will crash if b is zero. Better Approach: function divide(a, b) { if (b === 0) throw new Error('Division by zero is not allowed'); return a / b; } ✔ Validate inputs ✔ Prevent crashes by throwing meaningful errors

  1. Writing Monolithic Functions That Do Too Much Bad Example: function processData(data) { let cleanedData = data.map(d => d.trim().toLowerCase()); let uniqueData = [...new Set(cleanedData)]; uniqueData.sort(); uniqueData.forEach(item => console.log(Processed: ${item})); } Why it's bad: This function cleans, filters, sorts, and prints data - all in one place! Better Approach: function cleanData(data) { return data.map(d => d.trim().toLowerCase()); }

function getUniqueSortedData(data) {
return [...new Set(data)].sort();
}
function printProcessedData(data) {
data.forEach(item => console.log(Processed: ${item}));
}
✔ Follow the Single Responsibility Principle (SRP) ✔ Make functions reusable


Conclusion
If you're writing Node.js functions like the bad examples above, it's time to stop! Follow these best practices: ✅ Use meaningful function names ✅ Avoid modifying global state ✅ Prefer async/await over .then() ✅ Handle errors properly ✅ Keep functions small and focused
Write better Node.js today! 🚀

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay