The above code is hard to read because there are different tests in the form of if-else conditions, and also the nesting is 3 to 4 levels deep! One way to improve this code is to replace such conditions with Boolean functions, and then move all the magic numbers in variables.
To refactor this code, we have to first understand the whole logic and define a pseudo code. It looks something like following,
Now, from the above code it is clear that,
- Each condition is basically checking that the given value falls in the range or not. So this is the second candidate for refactoring. We can have a function which gives us Boolean value confirming if range has the number or not.
So, let us do these two points first.
Ok, Now We have moved the data-points/configurable-numbers in the data object as shown, and converted the code to use that object. But this is still complex, I would say complex than the previous version of code! Also, if we want to add one more nested level, then this method would get complicated having 4th nested for loop inside!
If we closely look at this version, we can see that the for loop is repeated thrice. It is doing the same thing again and again – validating the number with the array, looping over the internal JSON object and keep iterating until it reaches the level where it can access the JSON key ‘fixedVal‘.
So we can say still this code is not following DRY (Do not Repeat Yourself). To fix it, we need to remove the repetition. Let us recall the factorial problem, we can compare it with that. In most of the programming languages, iteration with repetition can be replaced with recursion! So lets see if we can refactor this again using recursive function. For that we need to consider the following,
- For loop can be replace with the recursive function such that same function calls itself until done.
- There are three diff. variables which are tested for inclusion in the range for each nested for loop – myDataVal, amount and calculatedVal as per this line ‘this._isDataRangeMatches(myDataVal,keyRange1)’.
- So if we want to use recursion, we need to pass these variables in a way where the function can dynamically use them.
- Can we pass them as Array or JSON? Array could be simpler, but array would need to be ordered accordingly. But then how we are going to know that value from which index needs to be read for current recursion call?
- When we take a look at Array functions, there is one function called ‘.pop’ which gives the object last pushed and removes it from the array, exactly the stack behavior – LIFO. So that means, we can stack these three variables in the array in the order we want them to be popped, and call the recursive function!
- All other objects can be passed in as it is because we can always access the inner objects using JSON keys.
Now the code looks cleaner! We always need to be cautious about the simplicity of the code and use logical ideas to resolve complex problems!
At BoTree Technologies, we build web and mobile applications to add value to our client’s business. We align ourselves to ensure that our client benefits the most out of our engagement.
Drop us a line to discuss how can we help take your business to the next level.
Choose Your Language
Subscribe to Our Newsletter
- Digital Marketing
- IT Consulting
- Project Management