That Humans Can Understand. - Martin Fowler ": 4 Simple Ways To Make Your Code Cleaner!
That Humans Can Understand. - Martin Fowler ": 4 Simple Ways To Make Your Code Cleaner!
“Any fool can write code that a computer can understand. Good programmers write code
that humans can understand. -Martin Fowler “
In software development, clean code is a widely used term and to be a good software
developer, the concept of clean code is must-have knowledge. But it is not an easy task to be a
clean coder as software developers need a big amount of time to learn and practice clean code.
It is a myth to write clean code at the first attempt even if you have great knowledge of it. It is
an incremental approach to make the code clean which needs time to time refactoring. You
need to invest a good amount of time to learn and practice them. But there are few of them
which can make your code easy to understand and more maintainable with very little effort. I
must say these are the only basic level of refactoring approach to make your code clean.
We will discuss 4 of the easiest approach to make the code clean, understandable and easy to
maintain.
1. Meaningful Names
Developers sometimes hesitate to write long meaningful names to introduce variables, classes,
functions which makes code difficult to understand. Software codes are like writing literature,
so take your time and think carefully to write meaningful names. A meaningful name is the first
impression in any code to understand the intention of the programmer. So, don’t write any sign
word, rather write meaningful names whatever the length is.
2. Functions
We write functions everywhere in our code. So, writing functions effectively can make your
code very easy to understand and representative.
“the very first rule of functions is that they should be small”- Martin Fowler. Yes! If you
can make each of your functions smaller, then you can achieve the first level towards
clean code. Try to keep your function as smaller as possible and it should hardly ever
reach 15 lines but I recommend a maximum of 10 lines of code.
Your function should do only one thing. Review your functions and ask if that describes
more than one thing. If yes, then break it and asks again the same question and take
action.
Each of your function should express the same level of abstraction.
Your command and query task need to be separate
Try reducing the number of arguments to at most three or if possible two. If you need
a greater number of arguments, you can make it as object/model but if you cannot do
that, then they are a good candidate for separation with different functions.
Your function name should be self-descriptive that by reading the name anyone can
understand what it does. Even when you write the function name you can easily
understand if it does more than one thing and will prompt to break into two.
3. Classes
“One responsibility one class” is the first thing come to mind when thinking about a clean and
good design class. The length of the class will depend on the description of the class name. If
the class can be written to describe only one task then it will force the developer to write code
of the same abstraction level. Simple and clear, just write functions, properties that will
describe only one responsibility. Unfortunately, bad developers are afraid of breaking one class
into multiple which leads the code very undisciplined and hard to understand.
4. Design patterns
Design patterns is reusable solutions to solve some existing problems. There are few categories
of design pattern and very much important to write clean code. Among them try to learn and
implement the Singleton Pattern, Factory Pattern, Adapter Pattern, Decorator Pattern,
Strategy Pattern, Observer pattern are frequently used and easy to understand. Lean and apply
the concepts and your code will be more beautified and cleaner.
It’s not possible to achieve the goal of clean code for the first time. So, write them first to
complete the requirements and then take a little time to refactor codes with the above-
mentioned concepts.