I have two methods
methodOne
(e.g. for a GET on questionaire)methodTwo
(e.g. for a DELETE on questionaire)Both have almost same lines of code.
IntelliJ also underlines the code and shows a warning:
duplicated code fragment
methodOne
@Override
public QuestionnaireDTO methodOne(long projectManagerId, long questionnaireId) {
//the part I want to write into a helper method
ProjectManager projectManager = projectManagerRepository.findById(projectManagerId).orElseThrow(
() -> new ResourceNotFoundException("ProjectManager", "id", projectManagerId));
Questionnaire questionnaire = questionnaireRepository.findById(questionnaireId).orElseThrow(() ->
new ResourceNotFoundException("Questionnaire", "id", questionnaireId));
if(!questionnaire.getProjectManager().getId().equals(projectManager.getId())){
throw new QuestionnaireApiException(HttpStatus.BAD_REQUEST, "Questionnaire not belonging to Project Manager");
}
// end of helper method
// return of methodOne
return mapToDto(questionnaire);
}
methodTwo
@Override
public QuestionnaireDTO methodTwo(long projectManagerId, long questionnaireId) {
//the part I want to write into a helper method
ProjectManager projectManager = projectManagerRepository.findById(projectManagerId).orElseThrow(
() -> new ResourceNotFoundException("ProjectManager", "id", projectManagerId));
Questionnaire questionnaire = questionnaireRepository.findById(questionnaireId).orElseThrow(() ->
new ResourceNotFoundException("Questionnaire", "id", questionnaireId));
if(!questionnaire.getProjectManager().getId().equals(projectManager.getId())){
throw new QuestionnaireApiException(HttpStatus.BAD_REQUEST, "Questionnaire not belonging to Project Manager");
}
// end of helper method
// return of methodTwo
return questionnaireRepository.delete(questionnaire);
}
I want to write the duplicated part of the code (enclosed in comments above) into a helper method. I want to avoid duplicate code but I'm not sure how to structure the helper method.
How can I achieve it?
You can actually just move the code 1:1 to a new method:
public QuestionnaireDTO methodTwo(long projectManagerId, long questionnaireId) {
return mapToDto(findById(projectManagerId, questionnaireId));
}
public QuestionnaireDTO methodOne(long projectManagerId, long questionnaireId) {
return questionnaireRepository.delete(findById(projectManagerId, questionnaireId));
}
private QuestionnaireDTO findById(long projectManagerId, long questionnaireId) {
ProjectManager projectManager = projectManagerRepository.findById(projectManagerId).orElseThrow(
() -> new ResourceNotFoundException("ProjectManager", "id", projectManagerId));
Questionnaire questionnaire = questionnaireRepository.findById(questionnaireId).orElseThrow(() ->
new ResourceNotFoundException("Questionnaire", "id", questionnaireId));
if(!questionnaire.getProjectManager().getId().equals(projectManager.getId())){
throw new QuestionnaireApiException(HttpStatus.BAD_REQUEST, "Questionnaire not belonging to Project Manager");
}
return questionnaire;
}