DEV Community

Cover image for 5 Simple Practices That Help Me Write Cleaner Code
Noriuki
Noriuki

Posted on

5 Simple Practices That Help Me Write Cleaner Code

When I first started coding, my main goal was simple: make the program work.

If it ran without errors, I considered the job done. But as projects grew in size and complexity, I realized something important — working code isn't always easy to read or maintain.

Sometimes I would revisit code I wrote weeks earlier and think: "Why did I write it like this?"

Because of that, I started paying more attention to small habits that make code easier to understand and maintain over time.

Here are five simple practices I use to keep my code cleaner in day-to-day development.

1. Comment Strategically: Add Value, Don't Just Explain

Comments can be helpful, but I try to avoid adding them everywhere.

Most of the time, clear naming and well-structured code already explain what the code is doing. Instead, I prefer to add comments when the logic is complex or when there's a business rule that isn't obvious.

For example, a comment like this usually doesn't add much value:

// check if the user is active
if (user.isActive) {
  sendNotification(user);
}
Enter fullscreen mode Exit fullscreen mode

The code already explains what is happening.

However, comments can be very useful when they provide context:

// Users created before 2022 don't have the "isActive" field,
// so we assume they are active by default
const isActiveUser = user.isActive ?? true;
Enter fullscreen mode Exit fullscreen mode

In this case, the comment explains why the code exists, not just what it does.

2. Group Imports Logically

Another small habit that improves readability is organizing import statements.

Instead of keeping all imports in a single block, I like grouping them by type. For example: external libraries, internal components, and utility functions.

// external libraries
import React from "react";

// components
import Button from "@/components/Button";
import Card from "@/components/Card";

// utilities
import formatDate from "@/utils/formatDate";
Enter fullscreen mode Exit fullscreen mode

This makes it easier to understand where things are coming from and keeps the file structure cleaner.

3. Use Early Returns to Simplify Logic

A pattern I often use is early return.

Instead of wrapping the entire logic inside nested if statements, I prefer checking invalid conditions first and returning early.

Without early return:

if (user) {
  if (user.isActive) {
    showDashboard();
  }
}
Enter fullscreen mode Exit fullscreen mode

With early return:

if (!user || !user.isActive) {
  return;
}

showDashboard();
Enter fullscreen mode Exit fullscreen mode

This helps avoid deep nesting and makes the main flow of the function easier to read.

4. Use Clear and Descriptive Names

Naming is one of the most important aspects of writing clean code.

Variables and functions should clearly describe their purpose.

For example:

// not very clear
const d = new Date();

// clearer
const currentDate = new Date();
Enter fullscreen mode Exit fullscreen mode

Good names make code easier to read and reduce the need for additional comments.

5. Keep Functions Focused on One Responsibility

I try to keep functions focused on a single responsibility.

When a function starts doing too many things, it becomes harder to read, test, and maintain.

Breaking logic into smaller functions often makes the code more modular and easier to understand.

Final Thoughts

Clean code isn't about writing perfect code. It's about writing code that other developers — and your future self — can easily understand.

Small habits like writing meaningful comments, organizing imports, using early returns, and choosing better names can make a big difference over time.

They may seem like small improvements, but they help keep codebases more readable and maintainable.

Top comments (0)