DEV Community

Cover image for How should I start to write clean code?
lurb3
lurb3

Posted on

How should I start to write clean code?

I've started writing code on a daily basis 4 months ago and I am struggling to get a clean flow of thinking and writing code.

In order to write better code I need to think of a problem's solution before actually writing it. That solution should be very clear in my mind so that I don't struggle when I am coding.

This should be my first step: think about the problem and achieve a possible solution.

The next step is to write the code. My code needs to follow the solution's "guideline" that I've come up with and should be as clear as possible. With this I mean the naming of the variables, functions, classes, etc. The flow of the code should represent that solution's thought process.

The next step should be to have clean code. Seperate code actions from general code. I should make functions to do a specific thing instead of doing a lot of things. It's better to have two or three extra functions than one function that has too many functionalities.

I should also use native code to simplify the reading: for example, use map function whenever possible instead of complex for loops, use array pop function instead of writing a function my own.

Finally, after all of this work, the code should be reviewed either by me or another person because I tend to be a little bit confusing, usually write more code than I need and there's also the need to catch any possible bugs.

Photo by Oliver Hale on Unsplash


Top comments (4)

Collapse
 
bradtaniguchi profile image
Brad • Edited

The only thing I see that could be a "next step" to writing clean code, is adding tests to the code you write so you can support changes to the code down the line so it stays clean. :D

Collapse
 
lurb3 profile image
lurb3

Hey, thanks for your reply!

Sorry for my ignorance but should I use any software for those tests? And those tests are for the written code or for the functioning of the apps?

Collapse
 
bradtaniguchi profile image
Brad

Basically every language has some kind of testing support, so you write code to test your own code. It might seem redundant, but this usually helps catch unexpected bugs, determine edge cases, document what the code is expected to do, and provide something to prevent regression down the line.

There are also usually different levels of testing, such as unit testing where you test small blocks of code (usually a function), to larger scale tests just as e2e tests where you run the full app and automate a program to go "do stuff" and verify everything works the same.

Usually unit-tests are the go-to method of preventing regression as they are the easiest to write, maintain and change when the code itself changes. It does require some discipline to write easily testable code, and time and effort to "upkeep" the tests, but usually its better to have some tests then no tests due to the fact code always changes and tests are there to make sure you don't break anything.

Finally, even if your code is "already working", adding some "smoke tests" will give you a at least some assurance that down the line if you need to change the code you have something to lean on to make sure you don't break anything.

I recommend looking into your language of choice's testing frameworks.

PS. here is a quote from Clean Code by Robert C Martin (Uncle Bob) on tests:

Code, without tests, is not clean. No matter how elegant it is, no matter how readable and accessible, if it hath not tests, it be unclean.

The book takes a pretty "hard line" against how clean code when it comes to tests, but it does give a lot of reasons as to why you want tests for your code to keep it clean.

Thread Thread
 
lurb3 profile image
lurb3

Damn, thanks for your answer. It's a bit shamefull bit i had no idea you could write code to test your code.
Thanks for the information, really appreciate it!