Lets clean your code
Have you ever want start to code but can’t seem to figure out what any of the code means? It’s even worse when it’s your own code! It’s probably because you don’t practice clean code when programming.
“Clean code is code that has been taken care of. Someone has taken the time to keep it simple and orderly. They have paid appropriate attention to details. They have cared.” — Robert C. Martin
What is it?
Clean code doesn’t actually have a definitive meaning since every programmer have a different and unique take on it, but they all have basically the same core which is the code that you make has to be easy to understand and easy to change. Robert G. Martin also has a good point on his book, he said that the code that we wrote is not only for the machine to process, but also for the humans to understand. Yes it may be easier to make a simple code that only you’d understand, but if someone else need to make modification or even when you want to make modification and you forget what you wrote it would be disastrous.
What is the Benefit?
One of the benefit of clean code is that the program has a clear flow and generally easy to understand program, which would benefit everyone. When your code has bugs and you need help debugging it, but your code is so messy that the person helping gets confused and can’t even help you at all since they’re to busy trying to understand it.
Another benefit is to have a clear understanding of what each class represents, since we all practice agile *wink on my previous article* we would have a lot of change and refactoring in our code. If we don’t have a clear understanding of which classes needed and which classes isn’t, the process of refactoring would take a very long time and holding up production on all sections.
Same as the classes, the methods need to be clearly defined and easily understandable. Changing a method would be disastrous if it’s not done carefully, and with the help of a clean code changing a method would be far safer since we would know exactly which methods and classes would be effected.
How to do it?
There’s a really basic 4 step or guide to do clean code successfully.
First, You should always use meaningful names. If we name a getter method with “haiya” for example, it would confuse everyone and even yourself if you don’t have a photographic memory *it’s an over exaggeration*.
Second, You should always make a function for a single purpose only! Nowadays a lot of programmers make a function that do everything, firstly it’s not very agile and secondly it would be really hard to understand what exactly the function do.
Third, You should always avoid writing unnecessary comment. The comments could easily distract you and other programmers, the best code is always the ones that doesn’t need comments.
Fourth, You should always test the system as a whole. Sometimes if you test in a small portion, a lot of the key features are not used and you could end up with a bug.