What is Clean Code?👁🗨
Developers' life mainly revolves around writing code💻, reviewing, refactoring and writing again. Most of the time we start a new project, our mind is filled with its feature, layout, functionality and effectiveness. So we go through those layouts and features, work on their design, and then write code to make it real. Since no project we code for one time, we have to go through those codes multiple times and make changes according to the demand and situations. But if we spend most of our time just finding the correct positions to insert new code and make changes, then it means we don't know about the code or the code is not correctly written.
Codes which take time to show their functionality and features are not clean codes🤢🤮. Those are neither properly indented, nor used proper names for the variables, functions, methods, and classes involved in the structure of functionality are poor in conveying their existence.
Also, clean code means how to write the code, not how to place the code or maintain the code. It comes in Code Architecture, which handles the entire project and its functionality. Clean code is just concerned with writing legible code which ultimately helps in maintaining the code. It focuses on a single problem at hand.
How the codes should be written?🙄
Codes should be legible, easy to understand and maintain🥱. Some common features of clean codes are as follows...
The code should have a proper indentation. Those codes which are written line by line does not give an attractive feeling and also hard to concentrate on their functionality🧐.
Clean code does not mean long names and redundant placeholders and variables. It will make code more bulky and hard to maintain. Clean code means proper names which convey their function and existence.
Code should follow the naming conventions and common best practices and patterns of the language used. It shows consistency and a proper understanding of the project.
It should reduce cognitive load😫. It does not take time to convey its position where it is placed and used.
It should be concise and 'to the point'🏹🎯.
Codes should avoid unintuitive names, complex nesting and big code blocks.
Codes should be fun to write🤓 and maintain.
Lastly, it should be like a story and you're its author😍.
Where do we mostly Mess up?🤮
The key pain points where most coders mess up in following clean code practice are:
The naming of Variables, Functions and Classes,
Structuring and commenting in code: Code formatting, Bad comments etc.,
In declaring functions: about its scope and parameters,
Conditionals and Error Handling: Deep nesting, missing proper Error handling,
In using Classes and Data structures.
However, clean codes don't require strong type assignments because those are trivial things to know the type of variable we use. Some languages require strong typing before declaring the variable like Java. In some places, we can comment out the output-response type. We can also store those types in variables by giving them proper names i.e, if it is a boolean we can give it a variable name like isValid, response🆗, etc. Well, we talk more about it later, here we are mainly concerned with the clean code.
Helping hand in Clean Code🤝
Since no developer can write optimal and cleanest code because codes are not dead but always alive. We have to maintain it and keep on changing it according to the demand. Seniors who know already have gone through the codes multiple times should or must know how we can make those codes clean.
Writing clean code is an art💃, and anyone can acquire it through practice💪 and conviction. By showing consistency in code formatting and clean code, it will become your creative work and it always follows along with you. Juniors follow your path and colleague appreciate👏 your art of writing clean code.
Since interns👦 do not have much experience in coding and have learned coding without following clean code good practices should be guided by the seniors👨💼. And if seniors do not know these good practices of clean code, they should start embracing these practices by following this blog.😎
Why Clean Code is important?👩🏻⚖️
Coding is not a one-time shot that we played and scored🏌🏻♀️ but it is more like a tennis shot which will come again and again⛹🏻♂️. So if you want to play a winning shot, you have to ensure it will be your best shot🏌🏻♀️. For that, you have to make code legible, maintainable and cognitive.
Clean coding practice will take time, definitely, but once you embrace it in your coding life, it will be your helping hand in the future. You will not be haunted by illegible and undeciphered codes🥶 in the time of fixing bugs or even in broken production codes. That time is more crucial than the time taken in writing code during development.
So we should focus on writing clean code and with practice, we can acquire optimal speed in delivering projects on time. The importance of clean code vs quick code is also derived from the following graph.
This graph shows how dry code, which is written just to deliver to the production ultimately dies in future😥 due to the high cost of maintenance, but clean code which is easy to maintain and easy to deliver will be productive and open to change. If we consider Productivity in X-axis, the new graph📈📉 also shows the same behaviour for clean code and quick code.
Clean code does not advocate for late project delivery, but for living codes which always help🧞♂️ in maintaining and are easy to change. Do not say to your senior that you're writing clean code which will take time⌛, but your code should be clean and delivered on time which will make seniors happy💋. Those things which are attractive and deliver good feelings while creating do not take much time. When you code like a story, you can deliver it on time. Just improve your storytelling ability.
What will be in the next blog?🤷🏻♂️
We will dive deep into the Naming conventions and understand the following topics.
'Why Names matters'. 🆔
Which name is suitable for variables, functions, and classes?
Importance of refactoring of code.♻
How to approach Clean Code.
Name Casing convention of different languages,
How we can derive our own style🕺🏻 of naming the variable which ultimately showcases your work?
And tons of other things...
Express your feeling about this blog. This blog is inspired by the clean code initiative of coders. Also, comment on how you think about clean code.