3

[ Java ] 方法里有很多判断需要提前结束,很多重复代码,求大神!

 2 years ago
source link: https://www.v2ex.com/t/809503
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  程序员

[ Java ] 方法里有很多判断需要提前结束,很多重复代码,求大神!

  nicepink · 6 小时 43 分钟前 · 1052 次点击

因为几个查询条件耗时久查到结果就提前结束,但是有重复的地方觉得不够优雅 if 判断那一块

    private void prepareBookingOffice(BrChangedEvent brChangedEvent) {
        BrGeneralInfoDto brGeneralInfoDto = Optional.ofNullable(brChangedEvent)
                .map(BrChangedEvent::getBrDto)
                .map(BrDto::getGeneralInfo)
                .orElse(BrGeneralInfoDto.builder().build());

        if (StringUtils.isNotEmpty(brGeneralInfoDto.getBookingOfficeCode())) {
            return;
        }

        // 耗时
        String officeCode = getOfficeCodeByOfficeUnlLocCode(brGeneralInfoDto);
        // 重复
        if (StringUtils.isNotEmpty(officeCode)) {
            brGeneralInfoDto.setBookingOfficeCode(officeCode);
            return;
        }

        // 耗时
        officeCode = getOfficeCodeByPor(brChangedEvent);
        // 重复
        if (StringUtils.isNotEmpty(officeCode)) {
            brGeneralInfoDto.setBookingOfficeCode(officeCode);
            return;
        }

        // 耗时
        officeCode = getOfficeCodeByFnd(brChangedEvent);
        // 重复
        if (StringUtils.isNotEmpty(officeCode)) {
            brGeneralInfoDto.setBookingOfficeCode(officeCode);
            return;
        }

        // 耗时
        officeCode = getOfficeCodeByPol(brChangedEvent);
        // 重复
        if (StringUtils.isNotEmpty(officeCode)) {
            brGeneralInfoDto.setBookingOfficeCode(officeCode);
        }
    }

第 1 条附言  ·  4 小时 41 分钟前

有些回复有些不太理解(知识储备不够...),后续再研究研究

目前先尝试了这位老哥的写法:

public class OptionalChain<T> {

    private T value;

    private OptionalChain(T val) {
        value = val;
    }

    public static <T> OptionalChain<T> of(Supplier<T> supplier) {
        return new OptionalChain<>(supplier.get());
    }

    public OptionalChain<T> or(Supplier<T> supplier) {
        if (StringUtils.isEmpty(value)) {
            value = supplier.get();
        }
        return this;
    }

    public T get() {
        return value;
    }

}

private void prepareBookingOffice(BrChangedEvent brChangedEvent) {
    BrGeneralInfoDto brGeneralInfoDto = Optional.ofNullable(brChangedEvent)
            .map(BrChangedEvent::getBrDto)
            .map(BrDto::getGeneralInfo)
            .orElse(BrGeneralInfoDto.builder().build());

    brGeneralInfoDto.setBookingOfficeCode(OptionalChain.of(() -> brGeneralInfoDto.getBookingOfficeCode())
            .or(() -> getOfficeCodeByOfficeUnlLocCode(brGeneralInfoDto))
            .or(() -> getOfficeCodeByPor(brChangedEvent))
            .or(() -> getOfficeCodeByFnd(brChangedEvent))
            .or(() -> getOfficeCodeByPol(brChangedEvent))
            .or(() -> StringUtils.EMPTY)
            .get());

}

补充一下:

  • 这个问题起因是因为昨天 code review 的时候同事觉得我这里可以改进,但是没有思路
  • 这里耗时的地方其实也就是 call 其他服务去拿数据,顺序要固定,是业务上的要求
  • 有些老哥说觉得没必要改,其实我也在想有没有必要,主要是想让代码既清楚又不冗余

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK