DEV Community

Alex
Alex

Posted on • Updated on

Clean code vs bad code

There are a lot of resources, books and opinions on how to write a good code.

Let's investigate what a "good code" is.

Purpose of the code

Code is intended to solve some goal, e.g. form that can be used for inquiry of something, platform that let you watch videos, forum that let you exchange opinions, internet shop where you can order goods, etc.

So first criteria - code should work.

Nothing ever perfect or "done"

Code is a software, code meant to be changed and extended. There is always a need to add a new feature, fix bugs or alter existing functionality.

So, second criteria - code should be easy enough to understand, you can't work with code without reading it, cumbersome codebase make it few times harder to do so.

Resilience to mistakes and changes

Since there is constant need to change the codebase, there is a need to be sure that existing features still work. That after adding new fancy thing, existing functionality still ok.

And can't rely on people's attention to detail, people will always misunderstand and make mistakes.

Comments got staled and ignored, most of the people lazy to read documentation, requirements spread across tasks, internal documents and many talks.

So, the third criteria - automated tests. Tests are cementing requirements and give you some certainty that code works as expected.

Summary

3 basic opinionated criteria for good code:

  • should work
  • should be easy enough to understand
  • should be tested

There are many more important things on how to achieve it, many different opinions. Can't possibly cover all in 1 post.

There are many hot and controversial topics.
For example, approach to write a good in JavaScript will be very different from .Net or Golang. So, good code is language specific, even framework specific.

Let me know what do you think, share your experience. Is it worth to share more of my knowledge?

Top comments (0)