java - Introduce a new variable instead of reusing the parameter "entity" -


i solving sonarqube issues , in issues face below warning 1 please tell me how can fix it,

here class

public static agency updateentity(agencymodel model, agency entity) {          if (model == null || entity == null) {             return null;         }          if (entity.getagencyid() != model.getagencyid()) {             entity = new agency()   // above variable 'entity' warning, "introduce new     variable instead of reusing parameter "entity".       }          entity.setagencyid(model.getagencyid());         if (entity.getagencylogolarge() == null) {             entity.setagencylogolarge(new file());         }         entity.setagencylogolarge(filemodel.updateentity(model.getagencylogolarge(), entity.getagencylogolarge()));         if (entity.getagencylogosmall() == null) {             entity.setagencylogosmall(new file());         }         entity.setagencylogosmall(filemodel.updateentity(model.getagencylogosmall(), entity.getagencylogosmall()));         entity.setagencyname(model.getagencyname());         entity.setcontactpersons(                 agencycontactpersonmodel.updateentities(model.getcontactpersons(), entity.getcontactpersons()));         entity.setotherdetails(model.getotherdetails());         entity.setclassification(classificationmodel.updateentity(model.getclassification(), entity.getclassification()));         entity.setstatus(entity.getstatus());         entity.setcreatedby((model.getcreatedby() != null && model.getcreatedby() != 0) ? model.getcreatedby()                 : entity.getcreatedby());         entity.setupdatedby((model.getupdatedby() != null && model.getupdatedby() != 0) ? model.getupdatedby()                 : entity.getupdatedby());         entity.setupdateddate(new date());         entity.setstatus(constant.active);          return entity;     } 

in above method warning , 1 please tell me best approach solve above problem.

assigning value method argument indicates bug (even though not case in example), why sonarqube gives warning.

assuming have no way of disabling warning (or don't want to), can eliminate introducing new local variable:

public static agency updateentity(agencymodel model, agency entity) {      entity result;      if (model == null || entity == null) {         return null;     }      if (entity.getagencyid() != model.getagencyid()) {         result = new agency();     } else {         result = entity;     }      ... use result variable instead of entity variable ...      return result; } 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -