DEV Community

Discussion on: How should I start to write clean code?

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!