What we will talk about
In this post, we will talk about the way to write clean code that will save you a lot of time and resources. This will make your workflow more seamless and less hectic. I will discuss for the Tips to write clean code. After going through this article you will go one step further in development for sure.
Why clean code is important?
When you are working on a big project you go back to the previously written code and make some edits again and again because you might be adding a new feature to your project. If your code is not readable by humans easily then this will waste a lot of time and It will be difficult for you to work in a team, as your teammate cannot understand what your code does.
Redundancy in code is also a time-wasting thing. Redundancy means that you are writing the same code again and again. Similarly, there are many mistakes programmers do in day-to-day programming and developing software
We will discuss some very simple to implement tips to write clean code so that you can work efficiently and work with a team
We will discuss
Naming convention
Functions
Comments
Switch statements
Project Architecture
Code Abstraction
1. Naming Convection
- The name of a variable should tell what it stores.
// wrong variable name
var x
var aString
// correct variable name
var currentValue
var amountInWallet
The name should be camelCase or in some standard convection.
camel case is the way to write the name in which there are multiple words in the name
you have to write all the letters of the first word in small letters and then write the first letter of each word after the first word should be in capital
// wrong variable name
var CurrentVALUE
// correct variable name
var currentValue // Current Value => currentValue
var amountInWallet
The name should be easy to understand and maybe sometimes show the scope of the variable
It should be of positive nature
// wrong variable name var isDisabled var isHidden // correct variable name var isEnables var isVisible
always try to make global constants in all Caps
var MOD = 10000007 var PI = 3.14 var PORT = 3000
The length of the Variable should be dependent upon the scope
Global Scope:- Name can be big as it tells the very specific term
Local Scope:- Name can be not as big as the global scope it can be two or three words long
Line Scope:- If the variable has only scope in the one like like for loop or while loop then uses the single words or some letters.
2. Functions
Functions are the most important tool while developing software. but when you should make a function what is the correct way to make a function? We will discuss in here.
Length of function
A function should always do only one thing. Each function should have some specific task and it should not do anything other than that. Functions should not be much indented if it is so much indented then they could be refactored.
Naming of Function
The function should have very specific names. The name should always tell what the function does.
Names should always be a verb because a function always do
Arguments in the Function
You should try to keep arguments not more than 3 because it becomes hard to remember more arguments while programming.
Try not to pass the Booleans in the function. Booleans create the if else statement in the function (you can use it if it’s important)
3. Comments
Comments are sometimes very time-saving for yourself and others but overdoing them creates clutter in the code and we start ignoring them and which makes it easy to ignore important comments.
Try to comment only on those lines which do not explain themselves, where there’s unavoidable complexity in your code. Always seek the relevance of code comments.
// bad comment
var PI = 3.14 // value of pi
//good comment
// explain why you did that if its is not explaining itself
4. Switch Statement
Do not use switch statements until it’s very necessary to use. them. Switch statements are very difficult to maintain. Try to use If else statements but if they are too many or like in game development it becomes important to use a switch statement. so always make a wise decision whether to use a switch statement or not
5. Project Architecture
Project Architecture is an important part of the project as your whole workflow depends on that let’s understand how?
Always follow a good architecture for your project. Architecture means how you maintain the files in the project. Files should be given the proper name and place in the project.
This makes it easy to explore and work as the project becomes big and has many files that are not easy to maintain.
There are different kinds of architecture for different projects.
you can also create an architecture specific to your project but make sure that it is efficient and easy to maintain. while creating you should make sure that you keep your business logic code separate from UI code in UI-based projects.
You should choose it before start working on your project because it is very important to keep your project maintained from the beginning.
6. Code Abstraction
An abstraction is a tool that enables a designer to consider a component at an abstract level without bothering about the internal details of the implementation. Abstraction can be used for existing elements as well as the component being designed.
Abstraction helps you to save so much time by not going into details while doing work in UI and you can make reusable code already and call it in the system as you need not go into that logic again and again.
Conclusion
Make sure that you follow these steps in your project. It will save you plenty of time while developing an application and invest this time in your precious life.