Last active
May 17, 2017 18:57
-
-
Save ParthBarot-BoTreeConsulting/6ce2bb1a8a1b008ddf8a3b90d18d43f4 to your computer and use it in GitHub Desktop.
Javascript Refactoring - Nested conditions to readable code using recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Final code | |
var MayApp = {}; | |
MyApp.Base = { | |
data: { | |
rules: { | |
FirstSet:{ | |
"0-12": { | |
"0-12000":{ | |
"0-90" :{ | |
fixedVal: 0.4 | |
}, | |
"91-*" :{ | |
fixedVal: 0.7 | |
} | |
}, | |
"12001-*":{ | |
"0-78" :{ | |
fixedVal: 0.4 | |
}, | |
"78.01-90" :{ | |
fixedVal: 0.7 | |
}, | |
"91-*" :{ | |
fixedVal: 0.9 | |
} | |
} | |
}, | |
"13-*": { | |
"0-12000":{ | |
"0-95" :{ | |
fixedVal: 0.8 | |
}, | |
"96-*" :{ | |
fixedVal: 0.7 | |
} | |
}, | |
"12001-*":{ | |
"0-95" :{ | |
fixedVal: 1.0 | |
}, | |
"96-*" :{ | |
fixedVal: 1.1 | |
} | |
} | |
} | |
} | |
} | |
}, | |
_isDataRangeMatches: function(dataVal,dataRange){ | |
if(dataRange[1] == '*'){ | |
return (dataVal >= parseInt(dataRange[0])); | |
} else { | |
return (dataVal >= parseInt(dataRange[0]) && dataVal <= parseInt(dataRange[1])); | |
} | |
}, | |
_enumerateFirstSetRules: function(dataValArr,jsonData){ | |
var dataVal = dataValArr.pop(); | |
for (var key in jsonData){ | |
if(jsonData.hasOwnProperty(key) && key != 'fixedVal'){ | |
var keyRange = key.split('-'); | |
if(this._isDataRangeMatches(dataVal,keyRange)){ | |
if(jsonData.hasOwnProperty('fixedVal')){ | |
return jsonData.fixedVal; | |
}else{ | |
return this._enumerateFirstSetRules(dataValArr, jsonData[key]); | |
} | |
} | |
}else if(jsonData.hasOwnProperty('fixedVal')){ | |
return jsonData.fixedVal; | |
} | |
} | |
}, | |
getFixedVal: function(myDataVal, amount, calculatedVal){ | |
var rulesData = MyApp.Base.data.rules.FirstSet; | |
return this._enumerateFirstSetRules([calculatedVal,amount,myDataVal], rulesData); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MyApp = {}; | |
MyApp.Base = { | |
getFixedVal: function(){ | |
//Javascript code before refactoring | |
var myDataVal = someCalculationHere(); | |
var amount = someInputFieldValue; | |
var fixedVal = 0; | |
if(myDataVal > 12){ | |
if(0 < amount && amount <= 12000){ | |
if(calculatedVal <= 95){ | |
fixedVal = 0.8; | |
}else{ | |
fixedVal = 0.7; | |
} | |
}else{ | |
if(calculatedVal <= 95){ | |
fixedVal = 1.0; | |
}else{ | |
fixedVal = 1.1; | |
} | |
} | |
} else if(0 < myDataVal && myDataVal < 12) { | |
if(0 < amount && amount <= 12000){ | |
if(calculatedVal <= 90){ | |
fixedVal = 0.4; | |
}else{ | |
fixedVal = 0.7; | |
} | |
}else{ | |
if(calculatedVal <= 78){ | |
fixedVal = 0.4; | |
}else if(calculatedVal >= 78.01 && calculatedVal <= 90){ | |
fixedVal = 0.7; | |
}else if(calculatedVal > 90){ | |
fixedVal = 0.9; | |
} | |
} | |
} | |
return fixedVal; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DEFINE amount, fixedVal, myDataVal | |
IF RANGE(0,12) INCLUDES myDataVal | |
IF RANGE(0,12000) INCLUDES amount | |
IF RANGE(0,95) INCLUDES calculatedVal | |
RETURN 0.8 | |
ELSEIF RANGE(95,*) INCLUDES calculatedVal | |
RETURN 0.7 | |
ENDIF | |
ELSE | |
IF RANGE(0,95) INCLUDES calculatedVal | |
RETURN 1.0 | |
ELSEIF RANGE(95,*) INCLUDES calculatedVal | |
RETURN 1.1 | |
ENDIF | |
ENDIF | |
ELSEIF RANGE(12,*) INCLUDES myDataVal | |
IF RANGE(0,12000) INCLUDES amount | |
IF RANGE(0,90) INCLUDES calculatedVal | |
RETURN 0.4 | |
ELSEIF RANGE(90,*) INCLUDES calculatedVal | |
RETURN 0.7 | |
ENDIF | |
ELSE | |
IF RANGE(0,78) INCLUDES calculatedVal | |
RETURN 0.4 | |
ELSEIF RANGE(78,90) INCLUDES calculatedVal | |
RETURN 0.7 | |
ELSEIF RANGE(90,*) INCLUDES calculatedVal | |
RETURN 0.9 | |
ENDIF | |
ENDIF | |
ENDIF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// intermediate code | |
var MyApp = {}; | |
MyApp.Base = { | |
data: { | |
rules: { | |
FirstSet:{ | |
"0-12": { | |
"0-12000":{ | |
"0-90" :{ | |
fixedVal: 0.4 | |
}, | |
"91-*" :{ | |
fixedVal: 0.7 | |
} | |
}, | |
"12001-*":{ | |
"0-78" :{ | |
fixedVal: 0.4 | |
}, | |
"78.01-90" :{ | |
fixedVal: 0.7 | |
}, | |
"91-*" :{ | |
fixedVal: 0.9 | |
} | |
} | |
}, | |
"13-*": { | |
"0-12000":{ | |
"0-95" :{ | |
fixedVal: 0.8 | |
}, | |
"96-*" :{ | |
fixedVal: 0.7 | |
} | |
}, | |
"12001-*":{ | |
"0-95" :{ | |
fixedVal: 1.0 | |
}, | |
"96-*" :{ | |
fixedVal: 1.1 | |
} | |
} | |
} | |
} | |
} | |
}, | |
_isDataRangeMatches: function(dataVal,dataRange){ | |
if(dataRange[1] == '*'){ | |
return (dataVal >= parseInt(dataRange[0])); | |
} else { | |
return (dataVal >= parseInt(dataRange[0]) && dataVal <= parseInt(dataRange[1])); | |
} | |
}, | |
getFixedVal: function(myDataVal, amount, calculatedVal){ | |
var jsonData1 = MyApp.Base.data.rules.FirstSet; | |
// Loop on the first level keys - "0-12" and "13-*" | |
for (var key in jsonData1){ | |
if(jsonData1.hasOwnProperty(key)){ //check if the json object has this key | |
var keyRange1 = key.split('-'); | |
if(this._isDataRangeMatches(myDataVal,keyRange1)){//Call our reusable function if the value is included | |
var jsonData2 = jsonData1[key]; //Then, get the inner JSON object, say value of this key - "0-12" | |
//Loop on the second level keys now - "0-12000" and "12001-*" | |
for (var key1 in jsonData2){ | |
if(jsonData2.hasOwnProperty(key1)){ | |
var keyRange2 = key1.split('-'); | |
if(this._isDataRangeMatches(amount,keyRange2)){//Call our reusable function if the value is included | |
var jsonData3 = jsonData2[key1];//Then, get the inner JSON object, say value of this key - "0-12000" | |
//Loop on the third level keys now - "0-90" and "91-*" | |
for (var key2 in jsonData3){ | |
if(jsonData3.hasOwnProperty(key2)){ | |
var keyRange3 = key2.split('-'); | |
if(this._isDataRangeMatches(calculatedVal,keyRange3)){ | |
//The final check, if it has the fixedVal key then return it. | |
if(jsonData3[key2].hasOwnProperty('fixedVal')){ | |
return jsonData3[key2].fixedVal; | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment