Have you ever heard the saying “code is poetry”? Probably. I think every engineer has heard that phrase before. I have known it for a long time, and I thought I knew what it meant. It wasn’t until sometime relatively recently (within the past year) that I really started to understand what it meant. Once I did, I started to see code in an entirely different light.
So, what is clean code?
Clean code is code that is easily read and understood by another person. The computer doesn’t care about the format of your code, or if it has any comments. It doesn’t care if you are using a shorthand if statement or a long one. The output will be the same. If another person looks at your code, can they (almost) instantly tell what the code is doing? Or what problem the code is there to solve? Structure & organization is important as well. If I want to find the functions related to a user, I should be able to very quickly scan the project and find that. Similarly, I shouldn’t have to sift through 2,000 lines of CSS to find the styles relating to a specific UI component.
Why should I care?
Clean code is maintainable code. Whether you are working with others or not, you should do your best to write clean code. Manipulating that multidimensional associative array of user information might seem self explanatory to you right now, but what about in a month when you need to alter it’s function or if your teammate takes over that project? If you work in a team, than your code should be something that your teammates can not only understand, but also add to. The cleaner the code, the easier it will be to maintain.
How can I write clean code?
Let me preface this by saying that this is variable. I definitely think that there are a number of basic principles that we can all agree on as being the base for clean code, but there are some specific attributes that might differ between individuals / companies. Here is my personal checklist for writing clean code:
- Follows some sort of indentation / coding style guide (e.g, Idiomatic CSS)
- Verbose naming conventions (e.g. `assign_random_value()` instead of `rand_val()`)
- Frequent and thorough comments (explain anything & everything)
- Enforces Single Responsibility Principle (give each file / function a single responsibility)
- Enforces D.R.Y (don’t repeat yourself)
Now go out there and clean up your code! If your code is already clean, give yourself a pat on the back and ask yourself how you can improve it! Be sure to let me know what clean code means to you, and how you go about writing the cleanest code possible!