4 Tips On how to start typing clean code
Writing clean code isn’t an easy task and there are a lot of benefits of writing a clean code. But there are so many subjects, ways and golden rules or say best practices to write clean code that this can become very confusing for anyone to choose a way to write clean.
So, at first, we are going to go through a small discussion about the benefits of writing clean code then we are going to jump right into the ways you can try.
The benefits of writing clean code
Let’s start talking about the benefits of writing clean code.Main benefit to writing clean code is that being able to minimize the time we spend reading the code and understand the stuff. Messy and unreadable code has a tendency to slow down the overall work process.
And sometimes if things are too messed up then developers decide to stop and start from scratch which isn’t good thing at all.
A clean code can really help in better team collaboration. When you are in a team, sharing a set of files written in different languages, if those codes are really “clean”, that will help other developers in your team to be able to just open and start doing their work on that!
Maybe code is essentially a set of lines that is doing a particular thing that you wanted it to do. No what if you have to pass the code to other developer and you can verbally or in any other way explain what that code of you was supposed to do, how will that person will know what that code is about? Now your code needs to be “clean” for that to happen.So how you can make your code clean?
1.Use meaningful variables names for your functions and modules
Want to add a caption to this image? Click the Settings icon.
What does meaningful mean? Meaningful names are names descriptive enough for other peoples to understand what that names are about or what that name is representing. So, in short, your variable or functions should have enough of a relative and descriptive name.
For example:
// Bad const fnm = ‘Tom’; const lnm = ‘Hanks’; const x = 31; const l = lstnm.length; const boo = false; const curr = true; const sfn = ‘Remember the name’; const dbl = [‘1984’, ‘1987’, ‘1989’, ‘1991’].map((i) => { return i * 2; });
// Better const firstName = ‘Tom’; const lastName = ‘Hanks’ const age = 31; const lastNameLength = lastName.length; const isComplete = false; const isCurrentlyActive = true; const songFileName = ‘Remember the name’; const yearsDoubled = [‘1984’, ‘1987’, ‘1989’, ‘1991’].map((year) => { return year * 2; });
However, there is something that we need to keep in mind that as a descriptive name you cannot just use as many words as you can. A good rule of thumb is to use three or four words. If you need more than four words to name, maybe you are doing too many things at once and we need to check that.
2.Letting each function do one task
When I started to code, I used to write functions that was like swiss army knife. They used to do more than one or even more task and then naming them was something that was kind of other worldly because naming was kind of impossible. Someone told me to use functions that will e only one task at a time. And using this technique led to good naming and good understanding of where to put the functions, how to edit them according to rest of the codes.
3.Using comments to DESCRIBE THE CODE
Want to add a caption to this image? Click the Settings icon.
It doesn’t matter how much well you name your code; just good name cannot replace the usefulness of commenting. Dedicating a line or two for s=describing what your function or that particular line that you wrote does using commenting can really help not only other developers but also can help your self while reviewing that code the next day!
4.Your own way of being consistent
When we a specific coding practice or way of writing best suited to our way of work, we must stick to that. Using different types and ways of writing your code in different projects can really be problematic in long term. The best thing to do is to pickup a best practice solution and try that out in different projects to see which one is for you. Sticking these best practices can really help while going back to old codes or sharing with your partner for reviewing. Experimenting with different best practices will work well if it gets examined in various other projects and looking for if there is any kind of in consistency that can affect a project later on. Giving time to choose is a key thing to remember.
Conclusion
No matter how much these points from the article helped you, the key point is to start. So, start today by choosing that one practice that suits you and don’t forget to comment your code!!!
On Twitter
Want to add a caption to this image? Click the Settings icon.
Clear and purposeful variable names.
Want to add a caption to this image? Click the Settings icon.
As i mentioned before! Variable naming is something and then naming them the way that serves as a mini description for the variable is very important.
Joshua Taylor says
Joshua Taylor saysJoshua Taylording clean is one of the things I always tell people to do who are first learning. Even something so small as proper
indentaton of blocks can make a huge difference in readability, although I have been known to come up with some dumb variable names indentation of blocks can make a huge difference in readability, although I have been known to come up with some dumb variable names