Hey Young Programmers!
Today’s GYAAN is all about writing CLEAN CODE.
It’s been quite a long time when I wrote my first bit of code, and from there till now, lots of things have changed.
I remember it quite vividly, how I used to write code back then when I started coding. I was always good with logics and knew how to accomplish a task that was put in front of me.
It was all good and blissful in the beginning when I only use to play with logics. But later when I dived into the real world, things were not the way it used to be.
Real world programming is a totally new paradigm. People will cherish you not because you can write logics quickly but how easy can you make it for others to understand it.
I would like to quote Woody Guthrie quote here-
[bctt tweet=”Any fool can make something complicated. It takes a Genius to make it Simple.” username=”vs_shrivastava”]
Never become someone’s nightmare, become someone’s acquaintance.
So, just to transfer my knowledge so that you guys can pick those things earlier in your career, I decided to write a blog on it.
Please comment below your tips, views and ways you follow to ensure clean code.
#1 Proper Indentation
This is the most basic.
You code should not scare the crap out of the programmer seeing it for the first time.
Infact, your code should not look like a code at all, it should look like a beautifully crafted piece of art. Or what I like to put it-
[bctt tweet=”Make Your Code Kissable” username=”vs_shrivastava”]
Your code should always flow like a poetry and person looking at your code should bless you.
He should not have any readability issues and it all should look systematic.
Now-a-days IDE (Integrated Development Environments) are there to make your job easy but then too, don’t rely on it completely. Make it your habit today.
#2 Proper Naming Conventions Must be Followed
In computer programming, a naming convention is a set of rules for choosing the name for your identifiers which denote variables, types, functions, and other entities in source code and documentation.
This is the most important aspect of a coders life. No IDE, no matter how advance, can help you in correcting the names for identifiers to make it more understandable.
Currently, I have been working as a programmer. Sometimes, I have to dig down into codes written by multiple developers.
And I must tell you, I have seen the worst…
At some point, it feels like punching the developer on the face. The work which should have taken only about 30 minutes extends the whole day.
The cost of labour increase by many folds because instead of writing
sum developer have chosen to write
s and instead of writing
mul it is m.
After reaching a certain point, you literally pull your hairs out of your scalp. So, please don’t use a,b,c to name variables. Use complete word or combination of words to make it readable and understandable.
I have also seen the best naming conventions.
Even if the logic is hard and complex, a good naming convention can make it pretty easy to understand.
It reduces the overall digging time and helps the analyst to search for the actual problem without having to deal with the stink of that code.
Following are the Reasons to use proper Naming Conventions:
- reduce the effort needed to read and understand source code;
- enable code reviews to focus more on the important aspects than arguing over syntax and naming standards.
- enable code quality review tools to focus their reporting mainly on significant issues other than syntax and style preferences.
- enhance source code appearance (for example, by disallowing over-long names or unclear abbreviations).
Don’t simply use c, d, ex, etc… to name your variables and methods and classes and interface and whatever damn thing it is.
Because, only you can understand what those variables do. And after-a-while, even you cannot understand what they does.
Then there will be no one to understand what they does.
It has also become an industry standard and plays a major role in hiring a potential programmer. So, make sure you use proper naming conventions every-time you write code.
#3 Comments where Necessary
This is one of the few things that you learn at the beginning of any new language. It is because these things play a vital role in any organization in which there are more than 1 developer working on a project.
As we all know that comments are used to provide additional meaning to a function, variable, process which is not easily understandable.
Also, comments are for humans. So, write for a human as a human with humanity.
One thing you should always keep in mind while commenting – never overdo it.
It can interfere with the readability of your code.
It should help others to understand the overall logic without at the cost of code’s readability. That is why there is a proper way to write comments as well.
Every programmer might have different views related to the comments.
Some say that your code must be self-explanatory, some prefer comments to give them an overall picture of the function before they dive in. There is no definite rule for comments.
What do I Think?
I think, a comment is necessary but not always.
If the logic of the function is complex and there are a lot of things that a function do (which a function should not) then there is a need for a comment to provide an overall picture.
Another thing that I like is that a comment should come at the very beginning of any function or a block.
In-line comments must only be used when there is an appropriate need for it and must not interfere with the original code Proper spacing should be kept in mind.
That’s my experience, different programmers may have different views.
The above-mentioned ethics will help you in long run. It will also make sure you become a good programmer.
[bctt tweet=”The difference between an ordinary and extraordinary is the EXTRA that he does.” username=”vs_shrivastava”]
Follow these conventions and write kissable code (as I say)
Here’s a great story I found on Quora while surfing and that’s when the idea popped in to write this blog. This is a real nightmare for any programmer. Do read the whole story, there’s lot to learn in it…
If you have any question regarding any of the information mentioned above, just feel free to comment below and let me know. Or if you think I missed something that must have been mentioned, then just mention below, I will make sure to add it.
- always try to write a code which uses optimal resources but never make the code complex. try to keep your code as simple as possible so that the next programmer after you would be able to understand it.
- Writing the code is not the hardest part, analysing the requirement is.
- A code can be written in many ways but the output will always be as per requirement.
I hope this article has touched the main points of a clean code. Keep coming back, as I’ll keep updating this article with more such practices. And yes the main part, please do share, like and comment your view below.