if statement - Javascript: Reduce cyclomatic complexity -
function(response) { if (response.bmap && seltype === 'q') { setdefaultqq(response.bmap); } else if (response.bmap && seltype === 'a') { setdefaultaa(response.bmap); } else if (response.bmap && seltype === 'o') { setdefaultoo(response.bmap); } else if (response.mmap && seltype === 'm') { setdefaultmm(response.mmap); } else if (response.bmap && seltype === 'p') { setdefaultpp(response.bmap); } else if (response.emap && seltype === 'e') { setdefaultee(response.emap); } else { setdefaultdata(); showmodal(); } }
is there way reduce conditional statement shorter form , better readability
could use object map seltype specific methods
var methods = { 'a': setdefaultqq, 'o': setdefaultoo, 'm': setdefaultmm, .... } if(response.bmap && methods[seltype]){ methods[seltype](response.bmap); }else{ setdefaultdata(); showmodal(); }
Comments
Post a Comment