0

I have a method where I fetch user input, check if certain values exist, and based on that build my own custom input object that I would use to search in a database. The code for the search method is as follows.

public SearchDocumentResult searchData(EmployeeInput employeeInput) {
    EmployeeInput getEmployeeInputForSearch = buildApplicationInputForSearch(employeeInput);
    if (employeeInput != null) {
        return super.searchForExactData(getEmployeeInputForSearch);
    } else {
        return super.searchForCloseMatchData(getTestPersonInput);
    }
}

The methods with multiple if checks on the input are as follows. Both the below methods and the above method exist in the same class.

private Application buildApplicationInputForSearch(Application applicationInput) {
        Application.Builder applicationForSearch = Application.builder();
        String jobIdFromInput = applicationInput.getJobId();
        applicationForSearch.withIsTagged(applicationInput.isTagged());
        if (!StringUtils.isEmpty(jobIdFromInput)) {
            applicationForSearch.withJobId(jobIdFromInput);
        }
        FormSection formSectionInput = applicationInput.getFormSection();
        if (formSectionInput != null) {
            this.buildFormSectionInputForSearch(formSectionInput);
        }
        return applicationForSearch.build();
    }

    private FormSection buildFormSectionInputForSearch(FormSection formSectionInput) {
        FormSection.Builder formSectionForSearch = FormSection.builder();
        String formCountry = formSectionInput.getCountry();
        Map<String, String> employeeQuestions = formSectionInput.getEmployeeQuestions();
        Map<String, String> applicationQuestions = formSectionInput.getApplicationQuestions();
        List<String> formNames = formSectionInput.getNames();
        if (!StringUtils.isEmpty(formCountry)) {
            formSectionForSearch.withCountry(formCountry);
        }
        if (formNames.size() > 0) {
            formSectionForSearch.withNames(formNames);
        }
        if (employeeQuestions.size() > 0) {
            formSectionForSearch.withEmployeeQuestions(employeeQuestions);
        }
        if (applicationQuestions.size() > 0) {
            formSectionForSearch.withApplicationQuestions(applicationQuestions);
        }
        return formSectionForSearch.build();
    }

The EmployeeInput is a model class that gets generated through a library and therefore I cannot make that use Java Optional for fields that may or may not exist. Using this EmployeeInput object as it is, how can I make this code more readable, with less if conditions? Any help would be much appreciated.

AnOldSoul
  • 4,017
  • 12
  • 57
  • 118
  • 1
    I think it would be better to post it here as the code actually works. https://codereview.stackexchange.com/ – Gautham M Jun 08 '21 at 16:21
  • 4
    Before you post at [codereview.se], make sure to read [A guide to Code Review for Stack Overflow users](//codereview.meta.stackexchange.com/a/5778), as some things are done differently over there - e.g. question titles should simply say what the code *does*, as the question is always, "How can I improve this?". Be sure that the code works correctly; include your unit tests if possible. You'll likely get some suggestions on making it more efficient, easier to read, and better tested. – Toby Speight Jun 08 '21 at 16:44
  • 1
    Instead of `map.size() > 0` you can write `! map.isEmpty()`. But generally, you should design methods in a way that they do the right thing when being invoked with an empty map, so you don’t need such a precheck. And when you don’t return `null`, you don’t need to check the results for `null`. – Holger Jun 09 '21 at 06:39
  • Consider using the null object pattern: https://en.wikipedia.org/wiki/Null_object_pattern – Erez Ben Harush Jun 10 '21 at 07:31
  • @Holger Thanks so much for the feedback, I'll refactor the code a bit more with your comments – AnOldSoul Jun 10 '21 at 13:02
  • @ErezBenHarush Thanks I'll take a look at that – AnOldSoul Jun 10 '21 at 13:02
  • @TobySpeight Sure I'll move the code to CodeReview then :) – AnOldSoul Jun 10 '21 at 13:03

0 Answers0