I am back with some good tips related to make our code more readable, you can also read some important tips to make your code readable by reading my previous blog.
Now, let’s start, in my previous blog I tell you guys about refactoring your code.
Thumb rule of refactoring is function should have one and only one purpose.
let start with the Thumb rule, We see in most of the projects there are lots of lengthy methods that contain multiple if-else, multiple iterations, multiple object creation by using setter, All these things lead to increase the length of methods which lead to increase cognitive complexity.
Cognitive complexity is considered a measure for readable code, It is the entity by which we can decide is our code is readable or not. As per some content, I found that maximum value for it is 15 if your method had more than 15 then you need to think for refactoring it.
so here are some tips to reduce it,
Always tries to use Anonymous objects because it will save one unnecessary variable in our code.
Always think to avoid multiple if-else or nested if-else statement in your method, there is some way around which can be used as an option to multiple if-else, which can be a separate topic will cover in next blog.
Always think to Divide your code into small chunks that have only single responsibility.
It is always good to have max 2–3 parameters for a method if they are exceeding you can wrap all of then into a class and pass the object. refer to this example here we use lots of variables for a method but we can wrap all of this in one class called Employee and use it by getters.
Avoid using more than 2–3 parameters for the method
#software-development #refactoring #cognitive-complexity #clean-code #if-else