1

I'm coding some CRUD application, a page show data from a database table and should join with two very similar table (These two very similar table cannot optimized to one table for history reason or too expensive time). I do optimized with switch statement and java 8 Functions, but Intellij IDEA give me Duplicated Code Fragment warning between two switch statement that I don't known how to fix it.

I'm NOT asking for optimizing designing but refactoring Duplicated Code Fragment and fix this warning.

I have a search method for searching data from databases,and use seperate logic from different table by TaxCustomerType enum:

    public PageUtils<IndividualTaxActorVO> search(TaxCustomerType type, IndividualTaxActorQuery query, UserDO principal) {
        //... ignore for conciseness
        Function<ActorCondition, Integer> counter;
        Function<ActorCondition, Collection<IndividualTaxActorVO>> searcher;
        switch (type) {
            case INDIVIDUAL: {
                counter = individualTaxActorDao::countIndividual;
                searcher = individualTaxActorDao::searchIndividual;
                break;
            }
            case CORPORATE: {
                counter = individualTaxActorDao::countCorporate;
                searcher = individualTaxActorDao::searchCorporate;
                break;
            }
            default:
                throw new RuntimeException("不能识别的个税客户类型:" + type);
        }
        int total = counter.apply(condition);
        if (total == 0) {
            return PageUtils.empty();
        }
        Collection<IndividualTaxActorVO> list = searcher.apply(condition);
        return new PageUtils<>(list, total);
    }

Also I have a link method for insert into table and do some checks, use seperate logic by TaxCustomerType enum too:

    public void link(TaxCustomerType type, String customerId, UserDO principal) {
        Function<String,IdAndTaxCodeAndDisabledOnly> findById;
        BiFunction<String,String,Integer> existsCounter;
        switch (type){
            case INDIVIDUAL: {
                findById = individualCustomerService::findTaxCodeAndDisabledById;
                existsCounter = individualTaxActorDao::countCorporateByCompanyIdAndTaxCode;
                break;
            }
            case CORPORATE: {
                findById = customerService::findTaxCodeAndDisabledById;
                existsCounter = individualTaxActorDao::countIndividualByCompanyIdAndTaxCode;
                break;
            }
            default:
                throw new RuntimeException("不能识别的个税客户类型:" + type);
        }
        IdAndTaxCodeAndDisabledOnly customer = findById.apply(customerId);
        if (customer == null) {
            throw new ApplicationRuntimeException(HttpStatus.NOT_FOUND, "客户不存在");
        }
        if(customer.isDisabled()){
            throw new ApplicationRuntimeException(HttpStatus.FAILED_DEPENDENCY,"客户已禁用");
        }
        int exists=existsCounter.apply(principal.getCompanyid(),customerId);
        if(exists>0){
            throw new ApplicationRuntimeException(HttpStatus.CONFLICT,"已是智能个税客户,不可重复添加。");
        }
        // insert into table ... ignore for conciseness
    }

The absolute Duplicated Code Fragment lines is:

in search method:

        switch (type) {
            case INDIVIDUAL: {
                counter = individualTaxActorDao::countIndividual;
                searcher = individualTaxActorDao::searchIndividual;
                break;
            }
            case CORPORATE: {
                counter = individualTaxActorDao::countCorporate;
                searcher = individualTaxActorDao::searchCorporate;
                break;
            }
            default:
                throw new RuntimeException("不能识别的个税客户类型:" + type);
        }

and in link method:

        switch (type){
            case INDIVIDUAL: {
                findById = individualCustomerService::findTaxCodeAndDisabledById;
                existsCounter = individualTaxActorDao::countCorporateByCompanyIdAndTaxCode;
                break;
            }
            case CORPORATE: {
                findById = customerService::findTaxCodeAndDisabledById;
                existsCounter = individualTaxActorDao::countIndividualByCompanyIdAndTaxCode;
                break;
            }
            default:
                throw new RuntimeException("不能识别的个税客户类型:" + type);
        }

I think the two code snippet have absolute different logic but same switch pattern, I can't stomach the IDE warning,please help me fix it😊.

2 Answers 2

2

The code fragments you compare are not strictly speaking duplicates. They look similar, but are quite distinct. There is no good way to optimize this, in terms of readability (assuming you want to keep the fundamental structure with the switches).

Also, your switch 'returns' two results, both very distinct in their types and values. If this were different, you would have been able to extract a method and return a Pair (Apache library) of Functions, but since the Functions are completely different (one is even a BiFunction), this is not possible.

I would stick with the code you have.

1

Idea from TreffnonX , I made wrapper class of "return" functions.

private static class SearchFunctions{
        Function<ActorCondition, Integer> counter;
        Function<ActorCondition, Collection<IndividualTaxActorVO>> searcher;
        //ignore ...
}
private static class LinkFunctions{
        Function<String, IdAndTaxCodeAndDisabledOnly> findById;
        BiFunction<String, String, Integer> existsCounter;
}

And in search method:

        switch (type) {
            case INDIVIDUAL: {
                functions=new SearchFunctions(individualTaxActorDao::countIndividual,
                        individualTaxActorDao::searchIndividual);
                break;
            }
            case CORPORATE: {
                functions=new SearchFunctions(individualTaxActorDao::countCorporate,
                        individualTaxActorDao::searchCorporate);
                break;
            }
            default:
                throw new RuntimeException("不能识别的个税客户类型:" + type);
        }

and in link method:

        switch (type) {
            case INDIVIDUAL: {
                functions=new LinkFunctions(individualCustomerService::findTaxCodeAndDisabledById,
                        individualTaxActorDao::countCorporateByCompanyIdAndTaxCode);
                break;
            }
            case CORPORATE: {
                functions=new LinkFunctions(customerService::findTaxCodeAndDisabledById,
                        individualTaxActorDao::countIndividualByCompanyIdAndTaxCode);
                break;
            }
            default:
                throw new RuntimeException("不能识别的个税客户类型:" + type);
        }

Yes , that's works! No Duplicated Code Fragment warning now!

But I stick think cost too much! Decision should be make.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.