This is the 23rd day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021″

preface

Today, I saw a piece of code my friend wrote and I went crazy.

This year is his third year of work. He has been engaged in Java development, but all of them are outsourcing companies. As far as I know, there is no problem with their companies as long as the task is completed and the code is like shit, as long as the code and people can run together.

Code section

This part of the code is not the whole code, but a part of a method to calculate the user’s level, a whole method down to more than 500 lines, if you are annoyed by the code can be skipped:

MemberInfoEntity> memberInfoQuery = new LambdaQueryWrapper(); memberInfoQuery.eq(MemberInfoEntity::getUid, changeGradeDto.getUid()); MemberInfoEntity memberInfo = memberInfoService.getOne(memberInfoQuery); If (memberInfo = = null | | changeGradeDto. GetUid () = = null) {throw new BusinessException (" treatment failure: " + changeGradeDto.getUid()); List<MemberGradeEntity> memberGradeList = memberGradeService. List (); memberGradeList = memberGradeList.stream().sorted(Comparator.comparing(MemberGradeEntity::getSort).reversed()).collect(Collectors.toList() ); / / get the user behavior and the behavior frequency LambdaQueryWrapper < MemberGradeGrowthRecordEntity > memberGradeGrowthRecordQuery = new LambdaQueryWrapper(); memberGradeGrowthRecordQuery.eq(MemberGradeGrowthRecordEntity::getUid,memberInfo.getUid()); memberGradeGrowthRecordQuery.lt(MemberGradeGrowthRecordEntity::getExpirationAt,DateUtil.getIntCurrentTime()); List<MemberGradeGrowthRecordEntity> memberGradeGrowthRecordList = memberGradeGrowthRecordMapper.selectList(memberGradeGrowthRecordQuery); Integer positiveSumInt = memberGradeGrowthRecordList.stream() .filter(a -> a.getGrowthType().equals(GradeGrowthUnitEnum.GROWTH_UNIT_VALUE.getCode())) .mapToInt(MemberGradeGrowthRecordEntity::getGrowthValue) .sum(); Integer reduceCountInt = memberGradeGrowthRecordList.stream() .filter(a -> a.getGrowthType().equals(GradeGrowthUnitEnum.GROWTH_UNIT_COUNT.getCode())) .mapToInt(MemberGradeGrowthRecordEntity::getGrowthValue) .sum(); Long reduceCountLong = reduceCountInt.longValue(); if(null ! = changeGradeDto && changeGradeDto.getGradeGrowthScore() ! = null && changeGradeDto. GetGradeGrowthScore () > 0) {/ / behavior reduceCountLong = reduceCountLong + number changeGradeDto.getGradeGrowthScore(); } Long positiveSumLong = positiveSumInt.longValue(); if(null ! = changeGradeDto && changeGradeDto.getGradeGrowthScore() ! = null && changeGradeDto. GetGradeGrowthValue () > 0) {/ / positiveSumLong = positiveSumLong + behavior value changeGradeDto.getGradeGrowthValue(); }Copy the code

I want to ask you, what’s your first feeling when you see code like this?

My first instinct was not to look at the code a second time, for two reasons:

1. It seems that there is no problem with this code. There is no problem with the validation and stream operation.

2. What if the demand changes a month or two later? Maybe the person who wrote this part of the code is no longer in the company, the person who took over the pot will have to see this code from beginning to end, increasing the maintenance cost.

Modify this part of the code

The idea to modify this part of the code is to leave the current code and organize the same business logic code to generate small methods, and then let the current method to call these small methods, so that others see the code is not so tired — improve the code readability

If you don’t need to completely change the requirements later, just change the logic in the corresponding minimethod — reducing the maintenance cost of the code

I sorted out the logic of this part of the code, which can be roughly divided into the following

Break the big method into three smaller methods:

The following is the modified method. You only need to call these three small methods. If there is a bug in some part of the business code, you only need to focus on that part of the code

MemberInfoEntity memberInfo = getMemberInfo(changeGradeDto); / / query level table List < MemberGradeEntity > memberGradeList = getMemberGradeDescEntitys (); / / get number of user behaviour on the current value, behavior getValueAndScore (memberInfo changeGradeDto);Copy the code

Method of obtaining user information

Public MemberInfoEntity getMemberInfo(ChangeGradeDto ChangeGradeDto) {// Obtain user table information LambdaQueryWrapper<MemberInfoEntity>  memberInfoQuery = new LambdaQueryWrapper(); memberInfoQuery.eq(MemberInfoEntity::getUid, changeGradeDto.getUid()); MemberInfoEntity memberInfo = memberInfoService.getOne(memberInfoQuery); If (memberInfo = = null | | changeGradeDto. GetUid () = = null) {throw new BusinessException (" treatment failure: " + changeGradeDto.getUid()); } return memberInfo; }Copy the code

Query the level table information and sorting methods

Public List < MemberGradeEntity > getMemberGradeDescEntitys () {/ / query level table List < MemberGradeEntity > memberGradeList = memberGradeService.list(); memberGradeList = memberGradeList.stream() .sorted(Comparator.comparing(MemberGradeEntity::getSort).reversed()) .collect(Collectors.toList()); return memberGradeList; }Copy the code

Obtain the behavior value and behavior times and statistics

Private void getValueAndScore (MemberInfoEntity memberInfo, ChangeGradeDto ChangeGradeDto) {/ / get the user behavior points and behavior LambdaQueryWrapper<MemberGradeGrowthRecordEntity> memberGradeGrowthRecordQuery = new LambdaQueryWrapper(); memberGradeGrowthRecordQuery.eq(MemberGradeGrowthRecordEntity::getUid,memberInfo.getUid()); memberGradeGrowthRecordQuery.lt(MemberGradeGrowthRecordEntity::getExpirationAt,DateUtil.getIntCurrentTime()); List<MemberGradeGrowthRecordEntity> memberGradeGrowthRecordList = memberGradeGrowthRecordMapper.selectList(memberGradeGrowthRecordQuery); Integer positiveSumInt = memberGradeGrowthRecordList.stream() .filter(a -> a.getGrowthType().equals(GradeGrowthUnitEnum.GROWTH_UNIT_VALUE.getCode())) .mapToInt(MemberGradeGrowthRecordEntity::getGrowthValue) .sum(); Integer reduceCountInt = memberGradeGrowthRecordList.stream() .filter(a -> a.getGrowthType().equals(GradeGrowthUnitEnum.GROWTH_UNIT_COUNT.getCode())) .mapToInt(MemberGradeGrowthRecordEntity::getGrowthValue) .sum(); Long reduceCountLong = reduceCountInt.longValue(); if(null ! = changeGradeDto && changeGradeDto.getGradeGrowthScore() ! = null && changeGradeDto.getGradeGrowthScore() > 0){ reduceCountLong = reduceCountLong + changeGradeDto.getGradeGrowthScore(); } Long positiveSumLong = positiveSumInt.longValue(); if(null ! = changeGradeDto && changeGradeDto.getGradeGrowthScore() ! = null && changeGradeDto.getGradeGrowthValue() > 0){= positiveSumLong = positiveSumLong + changeGradeDto.getGradeGrowthValue(); }}Copy the code

summary

During development, we should not only focus on how the business should be implemented, but also on the readability of the code, its cleanliness, and subsequent maintenance costs. Don’t look at yourself writing a lot of code and feel a sense of accomplishment, afraid that the pile of code will become what everyone calls Shi Mountain.

If you take someone else’s code and you want it to be more readable, you don’t have to spend too much time combing through the logic