“It’s easy to write code that a computer can understand. A good programmer
writes code that humans can understand” Martin Fowler
In every programmer’s life comes the time when you stare at a code you must work on with functions of hundreds of lines, multiple loops, and poorly named variables. You spend hours and hours trying to understand it, and if you must come back to it again months later, you will relive the same ordeal again. You do not want to be the person who writes code that is a pain to work on, so in this blog post, you can learn about clean code and how to achieve it.
“It’s easy to write code that a computer can understand. A good programmer writes code that humans can understand”
This quote encapsulates the essence of clean code really well. We want to write maintainable, understandable, and easy-to-scale code.
Making the code clean is not always easy; it takes time to improve it, but it pays off long term by the time saved in the future that was not spent on understanding a messy code. I will tell you a few easy tricks to improve your software.
Meaningful names
Names are everywhere in software. We name our functions, variables, arguments, and classes. Because we do it so much, we better do it well. Here are a few things we should think about when naming things:
How to Write Functions
Code Organization & Formatting
Comments
While using comments, we should never forget that comments do not make up for poorly written code. We write a module, and we know it is confusing and disorganized. We know it is a mess, so we write comments. Clear and expressive code with few comments is far superior to cluttered and complex code with many comments. Rather than spend your time writing comments explaining the mess you have made, spend it cleaning it.
Conclusion
The Boy Scouts of America have a simple rule we can apply to our profession: “Always leave the campground cleaner than you found it.”
If we all checked in our code a little cleaner than when we checked it out, the code would not rot. The cleanup does not have to be something big. Change one variable name for the better, break up one function that is too large, and eliminate one tiny bit of duplication. Trust me, the small things will add up with time into a cleaner codebase.
... and PS
I strongly recommend that everyone interested read the book “Clean code – A Handbook of Agile Software Craftsmanship” by Robert C. Martin. It is informative, has an easy-to-read style, and you can improve your code by following the principles explained in the book.
SHARE
0 comments