if statement - Reducing the cyclomatic complexity of a java method -
i have following method:
private void setclientadditionalinfo(map map, client client, user user) { map additionalinfo = (map) map.get("additionalinfo"); if (checkmapproperty(additionalinfo, "gender")) { client.setgender(additionalinfo.get("gender").tostring()); } if (checkmapproperty(additionalinfo, "race")) { client.setrace(additionalinfo.get("race").tostring()); } if (checkmapproperty(additionalinfo, "ethnicity")) { client.setethnicity(additionalinfo.get("ethnicity").tostring()); } .....
12 more if statements used in similar way. difference being different setter method name , different parameter. now, same pattern repeated again , again, there way reduce code complexity?
not easily, , not without using reflection.
using reflection loop through list of values , call appropriate method in client object. rid of complexity , cleaner/more robust. perform slower.
fundamentally though have case doing not quite same operation on , over, that's tricky.
Comments
Post a Comment