Having trouble deciding between two options for parsing URL parameters? Both seem suboptimal, but is there a better way to handle this situation? If you have any suggestions for a plausible Option #3, please share. Let's assume we are dealing with up to 40 parameters.
Option #1
Cons: The complexity is O(n*k), where n is the number of parameters and k is the number of switch cases. Also, the overall logic looks quite messy.
for(let param in params) {
let value = params[param];
switch(param){
case 'param1': {
doSomethingWithParam1(value);
break;
}
case 'param2': {
doSomethingWithParam2(value);
break;
}
}
}
Option #2
Pros: The complexity is reduced to O(k).
Cons: However, the code structure appears even more convoluted.
let param = '';
param = 'param1';
if(param in params){
let value = params[param];
doSomethingWithParam1(value);
}
param = 'param2';
if(param in params){
let value = params[param];
doSomethingWithParam2(value);
}