Nowadays the term Programming is becoming very popular! Specially among those who are involved in any profession related to Computer Science or, Web Development & Design. Most of them do programming more or less everyday! But, not everyone who code is a good programmer!
One of the biggest reason behind this is to be a good programmer you need to follow good coding practices. It should become your second nature. Good coding practices require efforts & most of the time it takes years to adopt them properly! In some startup or in companies with fast paced environment time is pretty scarce, and developers don’t have the luxury to spend their time on learning best practices as they are always chasing deadlines! Interesting thing is, sometime bad buggy, unorganized codes seem to be good enough to get things moving which plays a negative role of discouraging people to improve their coding. This often leads to a giant legacy code base full of patches & workarounds. For example, it is quite common to see company still maintaining a large code base of 10 years old just because it still works!
And the maintainers of those code bases usually spend fair amount of time roaming from one desk to others, scratching their heads or even biting their nails (gross & unhygienic) The struggles of these code maintainers continues until the code becomes obsolete and no longer maintainable!
You might be wondering is it really true? Well lets talk about one of the tech giant called ‘Facebook’ When they launched for the first time in 2004. It was purely based on PHP. With the gain of popularity code base also had a growth spurt! It became extremely difficult for them to get rid of PHP and move on to some other stacks or, trending frameworks of that time (for example Django). To stop the growth spurt and make the code more maintainable the facebook team had to invent a programming language named ‘Hack‘. Surely, they had to spend a lot of time to invent hack on top of PHP that wraps php with security features & some cool properties of functional programming. The biggest plus point was that hack was compatible with PHP. Hence, the whole php codebase didn’t become useless right away
So it seems like spending those extra hours did pay off to Facebook!
When I got my first laptop, I used to type with my two index fingers by looking at keyboards all the time. My Typing speed was around 10-15 words per min. Then after a lot of practice in next few months I reached 35-40 words per minute. After that the gain of speed reduced drastically. I could get my work done with this speed but, it was extremely hard to reach a speed of 50 words per minute (dark rooms were nightmares)!
Then one day I decided to bring an end to this. So started to practice touch typing. The beginning days were extremely painful, my speed dropped to 5-10 words per min. To type 10 words I had to press backspace 1000 times. And, way too time consuming! I keep on trying and learned the basics key positions within next month. Slowly the number of backspaces reduced and, so do the struggles, I started to gain speed and accuracy!
Even though you might find the above story irrelevant to the current context but the fact is almost identical with learning best practices in programming. In early days, you will have to struggle a lot and invest days and hours from your busy schedule. But, eventually you will see the improvement. Ultimately it will make a great difference as you will become more productive.
So, How to recognize Good Code?
The answer is quite simple! Take a look at the following picture:
It means you can get a sense of your code quality as soon as your co developers or, colleagues start to review/use your codes. Sometimes going through your old code base might have the similar effect. Don’t get frustrated after finding out you don’t even understand a thing about the purpose of your code or, why you have wrote it such a way! This type of frustration is quite common among the developers and you are not alone! Better worry if you don’t find any improvement when you review a six month old code again. If such thing happens then there should be two cases either you didn’t improve a single bit or, you are already a legendary programmer.
Okay! Lets get back to the original question ‘how to recognize good code?’ Good code will have the following properties:
- Elegant and Carefully Written Code – Good Code will be fascinating to read. It will read as if it is crafted like a beautiful novel. It will make you feel the pampers and cares that someone put on it.
- Focused Coding – This is also important for Good coding practice. Each and every part of a good code such as functions, classes or, modules etc will be focused on a single specific goal, it will accomplish it gracefully!
- Test – A good code will always bear some good test codes with it that run and check vital parts of that particular code!
- No Repetition – The code will have no duplication! It will be based on the DRY principle i.e. Don’t repeat yourself.
- Simple & Optimal – The code will be minimal. It will do exactly what it needs to do in the most optimal way no more no less!
- Less or No Comments– Wise Programmers say Good code doesn’t require any comments! When you look at good code to find out how do they do a particular thing it will seems like they did it in the most obvious way!
How to write ‘Good Code’?
Now that we know the properties of Good code, we will jump write into the introduction of Good Coding practices. To start writing good codes we will have to build a habit of doing the following:
Writing Meaningful Variable Names
The name of the variable should tell what it supposed to do. The name should be short but explicit. For example, checkout the following code snippet:
s = 60000
m = 12
y = s * m
Now take a look at the following example:
salary = 60000
month = 12
yearly_income = salary * month
which one is more readable? If you carefully analyze both codes, both are somewhat identical in structure even the operators used in both codes are same! Only difference is the name of the variables। Now, lets look at another variation of the first code:
s = 60000 // Salary of the user
m = 12 // number of months
y = s * m // calculated yearly income
This variation is better than the initial iteration. But, the problem is still same! Lets say, you are using the same variable again after somewhere say 299 number lines below. The reader of the code will have to scroll up to check what these variables
y do! Either they will have to memorize these variables with the context or, you will have to write another comment to make it clear for your reader. You can utilize this idea to improve your coding. When coding if you find a situation where you have to write a comment for your variable pause for some moments and brain storm and refactor your variable name with a better one that don’t require any comments! Trust me it will save you and your fellow readers hours!
Function Name – Most important property of function is to do one thing and do it well! So, the function name must have a verb accompanied with a noun. Example:
Class Name – Unlike function names class names should be noun, instead of verb. Use of proper noun is better than any other nouns. Example:
AbstractUser But, it is better to avoid generic names such as –
Manager, Info etc.
Beware of the naming inconsistency! Inconsistent naming is worse than anything else! Lets say you are designing an API. And, you are using some common words such as:
delete etc. in some of parts. Again, in some other parts you are using words
remove in the same api. Then the users of your API will go nuts as they will have to go through the api doc over and over just to find the appropriate names i.e. whether to use
add or … So, just stick with one common word throughout the whole code base.
More about Functions
Before starting to write a function, Keep in mind that function needs to be small and tidy. It is applicable for all the inner loops or if else blocks within the function। It could contain another function call but, bottom line it need not exceed a few line. Another thing is function should do one thing and do it well! If you see that, your function is multi tasking say using more than 3 arguments Then you should immediately try to make it more concise or split to multiple small functions!
You should take this article with a pinch of salt! The most important thing about good coding practices is to be consistent so that the reader of your code can understand what your code do. Also, There are tons of other ways to improve your coding skills with good programming practices. But, that is the topic of another day!
An Introvert with Exception!