What are your favorite strategies for writing code that's easy to understand, maintain, and extend? Share your top tips and tricks for keeping your codebase sparkling clean and developer-friendly.
Follow the DEVteam for more discussions and online camaraderie!
![Cover image for What Are Your Best Clean Code Tips?](https://media.dev.to/cdn-cgi/image/width=1000,height=420,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcs7mwj9iixdsk7so29ye.png)
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (38)
Seperation of concerns: making sure one function does only one particular thing and is reusable if needed.
Clear and concise comments
Self descriptive variable and function names
On a lighter note, the above bullet points are not indented correctly 😀
BTW, great share 👍
Lol, in the markdown they were properly indented, but for some reason when I post the comment, it just did that. Tried editing it twice, eventually just left it like that😅
Self descriptive variable and function names >> I use this, thus reducing the need for additional comments.
My personal experience with the recommendation of low coupling is that it's helpful to write out a "plain English" version of requirements. For example, creating a report from data based on a 3rd party API may look something this:
After writing these out, it helps me think about how the code could be organized. For example, each of these steps should at the very least be in their own function and there's probably separate modules/classes that can be derived as well.
But I think the biggest advantage to operating this way is seeing when code may be mixing concerns. If I look at my code and it deviates from this list - eg the code for implementing the email logic is in the same function as the code that implements the API request logic - I've likely grouped things together that should be split out.
I use the same process. The act of jotting down that bulleted list might show that I'm missing a step or that some assumption won't work, which is best to catch before any code hits the screen.
Think about the big picture. It's rarely about the code you're staring at, but how it fits into the system.
This is my mantra. Knowing the big picture helps us to know when to stop making a piece of code cleaner and cleaner...
This might be a strange strategy to some, but I try to avoid abstractions as much as possible. IMO, it's much easier to go back and abstract something for re-usability when you need to, instead of constantly trying to throw everything into a function or class.
Actually, it’s well known software design principle known as YAGNI , which stands for “You Aren’t Gonna Need It.”
I use SOLID principles as the base of clean code strategy. Trying to make your code reusable is a great column too. And the last one I use the most is TDD, to clarify in every moment what I want and how to do it the best I can.
I agree, in general I try to keep my code as decoupled as possible. For instance, if you have a class in charge of sending emails, do not give that class the responsability of configuring the mailer. Instead, create another class to do that so you can decoupling sending from configuring.
descriptive names for variables and functions.
using functions to avoid writing same code again and again
importing functionalities from other files to separate like and unlike things
writing comments where ever required
keeping in mind, which is deprecated and using what's new and versatile
I think there is a great reason WHY several people have already said well named functions and variable names is a common clean code practice. I can't stress the importance of that concept enough. The problem with most code is INTENT. If you are clear with the INTENT of your code then you will have created clean and easily maintainable code. Usually that means just break out some code into a WELL named function. If you start to write comments around functionality then that's usually a good indicator (code smell) that intent isn't clear and it needs to be separated out.
Other things of note:
I agree. A lot of comments are usually a sign of code smell. Especially after the code has been modified several times, but the comments are never updated when someone is enhancing it or making a bug fix.
I have seen this way too many times in production. The CODE is ALWAYS the source of TRUTH, not the comments.
Function and variable names should be clear, succinct and avoid acronyms (that can lead to misinterpretation).
Also if you think there are not enough characters to give your function a meaningful name, your function is probably doing more than one thing, a sign that its time to refactor.
One last thing I would add is to also not waste time trying to optimise code before you are finished AND there is an actual need to make the program or process run faster. Otherwise you might waste a tonne of time on something that gets refactored anyway or thrown out all together in the end.
Thanks.
When working on the front end, avoid using two full-fledged libraries simultaneously, such as Bootstrap and Ant Design (antd).
Ensure that the same library is not included multiple times with different versions
If parts of the code have been replaced with a new version, ensure that the old version is not left commented out, as this can be avoided.
Cool.
I starter writing something in a comment, and I talked about so many things that I ended up considering writing an article.
How to be a better developer for you and the others
Christophe Colombier ・ Apr 7
I hope you will find it interesting
I think the main ones I'm thinking about while writing code would be:
map()
instead).I'm writing a book on clean code, where I talk about each (and many more) in great detail with lots of examples, most chapters are available online.
One thing I think can help beginners is to feed the code to LLM to refactor after it's written.
They are not always correct but it's not that LLM never suggests a good idea. And models are becoming smarter as time goes by.
So a balanced approach could be to get the suggestions from LLM, learn more about it if needed, evaluate it, implement or ignore. LLM is like static analysers but for larger refactoring.
Keep clean design, code we clean-up later
I think the main ones I'm thinking about while writing code would be:
map()
instead).I'm writing a book on clean code, where I talk about each (and many more) in great detail with lots of examples, most chapters are available online.
A fundamental of this is whether the code is going to be under continual frequent redevelopment, if it is then my advice is where you may be looking at adding greater functionality over time e.g. file output format for example ensure you use dependency inversion and get your interface for the abstraction right prior to developing your different output classes and seriously think about your output formatting classes and their single responsibility. People that are talking about comments, probably need to review their code not their comments as it probably needs refactoring and proper variable/function names. For comments relating to writing down steps in plain English, look at UML there are techniques within it to do this.
i have always follow to create reusable clean code