An anonymous reader writes: Have you ever opened up a file before and thought to yourself “WTF?”. This probably happened because it was hard to read, there is so much code and logic going on at once and you are just counting the “WTFs per minute”.
So it is very important to write clean code especially with languages like JavaScript. As programmers, engineers, developers, whatever.as we write code then we should make an effort to write clean code because we care about our craft.
A big part of writing software is to make it readable for others and more importantly yourself for when you have to revisit your code in the future. Most programmers think that once the code is working, they can move on and be done. However, the next and just as equally important step is to refactor your code and to clean it up. So here is a couple of tips I have learned.
Choose descriptive names
I have seen a lot of code on the web where they use single letter variable names or variable names that do not make sense. Having good variables can act as good documentation for your code and eliminate the need to write comments. Names in software are 90 percent of what make software readable. You need to take the time to choose them wisely and keep them relevant. Names are too important to treat carelessly.
Functions should only do one thing
It’s very important to write your functions as small as possible. It’s a lot easier to test a smaller function then a 100 line behemoth. Also, small functions are more focused. It’s a lot easier to understand what is going on the smaller the function is. The function should only do one thing and that’s it.
Classes should only be about one thing
As well as writing your functions as small as possible your classes should be written as small as possible as well. Classes or modules should have one and only one reason to change and have one responsibility. This forces our classes to be focused and this is known as the Single Responsibility Principle.
Refactor, Refactor, Refactor
If you need to add more code to the file you are working, it is completely fine to add in everything that you need to and fatten it up. Once everything is working, you should then refactor it. Look for areas where you can break things out into another class or move your methods around. The goal is to try and make the file smaller then when you first saw it. We want to have our code to be as organized as possible. It’s a lot easier to work with a bunch of smaller organized components then one massive unorganized code base.
Your code should be able to adapt to change, always.
Your code should be able to adapt to change. When I write code, I like to think of how lazy of a programmer I want to be in the future. If new changes are being done to my code, I want to be able to adapt and implement as quickly as possible and this can be done through having small classes and methods. Duplication is another reason why code can not be changed as easily. Try to avoid it as much as possible and you will see that you can make changes to your code much easier.
Always and I mean ALWAYS test your code
Code that is written using the TDD method is far better then code that is not tested. You can’t expect to have clean code if you do not have any test coverage. Remember, your code needs to be able to adapt to change and if there is no test coverage then you are most likely going to be afraid of changing your code. With test coverage you can make changes with confidence and be cool doing it.
——-
Every programmer should read these books Clean Code, Refactoring, Design Patterns. Once you are done reading these books. Reread them again. They will change the way you write your code.