DEV Community

Cover image for How to Write Clean Code: Refactoring and Best Practices

How to Write Clean Code: Refactoring and Best Practices

Balraj Singh on February 13, 2025

Let’s be honest, every developer (yes, even the ones with years of experience) has shipped messy code at some point. Writing clean code isn’t some...
Collapse
 
tomasdevs profile image
Tomas Stveracek

Good article, I wrote a similar one, but regarding commits, here is the link

Collapse
 
balrajola profile image
Balraj Singh

Oh great, I'll check it out! And thank you.

Collapse
 
khremesi profile image
Tomasz Michal Juraszek

Shouldn't point 4 be:
if (count < 0) {
count++;
}

Example in point 6, makes no sense?

Collapse
 
balrajola profile image
Balraj Singh

Good observation! The intent behind the example was to prevent the count from decreasing below zero, which is why it checks if (count > 0). If the goal were to increment when the count is negative, your suggestion would be spot on. But in this case, it's about ensuring it doesn’t go below zero.

Collapse
 
aliasfoxkde profile image
Micheal Kinney

I had the same thought, that the two examples were not the same, but neither is this. If counting up, why would count be less than 0? You counting to 0?

Collapse
 
balrajola profile image
Balraj Singh

The example is meant to handle scenarios where the count might already be negative due to prior operations or external factors. In such cases, ensuring it doesn’t decrease further helps maintain logical consistency. Thanks for weighing in.

Collapse
 
hadil profile image
Hadil Ben Abdallah

Nice Article

Collapse
 
balrajola profile image
Balraj Singh

Thank you!

Collapse
 
michael_gbolahan_d05985e8 profile image
Michael Gbolahan

Nice one

Collapse
 
balrajola profile image
Balraj Singh

Thank you!

Collapse
 
estefancayuela profile image
Estefan Cayuela

I felt exposed in some aspects😅
I will do better, I promise

Collapse
 
balrajola profile image
Balraj Singh

Haha, we’ve all been there! The fact that you’re thinking about it means you’re already ahead. Keep going!

Collapse
 
ombir_sharma profile image
Ombir Sharma

Informative

Collapse
 
balrajola profile image
Balraj Singh

Thank you!

Collapse
 
nathan_jobe_7012c32d13414 profile image
Nathan Jobe • Edited

It is SOO!! Hard to get people to understand these principles. I constantly hear things like. "If it aint broke dont fix it". "Oh you just dont understand business" blah blah blah. And you always get new people in there trying to show you how "to get it done", when all they are doing is making a mess of things.

Collapse
 
sugrivlodhi profile image
Sugriv Lodhi

Good point! Maintaining a well-structured and high-quality codebase is crucial, not just for the current team but also for future developers. It ensures better readability, maintainability, and scalability. It also reduces a lot of pain as our codebase grows.

Collapse
 
sunilfgh profile image
Sunil kumar Dubey

Good article

Collapse
 
sophahum profile image
Hum Sopha

I think it's just readable

Collapse
 
rajatrajputdev profile image
Rajat Rajput

I feel attacked 😭

Thanks you so much for this!