최대 1 분 소요

소나큐브를 이용해 코드 정적 분석을 실시하였다.
아래와 같이 수많은 Code Smell과 Bug 또한 발견되었다.
스크린샷 2023-03-20 오후 6 31 43

이제부터 코드를 수정하면서 기억에 남는 것 위주로 기록하려 한다.

[Bug] A “NullPointerException” could be thrown; “pet” is nullable here.

기존

private Pet findPet(Pet reqPet, User user) {
  Pet pet = null;
  if (reqPet != null) {
      pet = petRepository.findByPetNameAndOwner(reqPet.getPetName(), user)
              .orElseThrow(() -> new CustomException(PET_NOT_FOUND));
  }
  return pet;
}

변경

private Pet findPet(Pet reqPet, User user) {
  return petRepository.findByPetNameAndOwner(reqPet.getPetName(), user)
              .orElseThrow(() -> new CustomException(PET_NOT_FOUND));
}

reqPet 자리에는 protectionEndReqDto.getPet()가 들어오는데,
해당 Dto는 @NotNull(message = "대상 동물은 필수 선택 값입니다.") 처리를 해놓았으므로,
만약 Pet에 값이 없다면 컨트롤러에서 @Valid에 걸려 서비스 단으로 넘어오지 못할 것이다.

따라서 pet에 미리 null을 넣어두고 조건문을 통해 값을 대입할 필요 없이, 바로 값을 대입하도록 변경하였다.

[Code Smell] Rename this variable to not match a restricted identifier.

기존

Record record = Record.createRecord(pet,StampType.WALK, walkDiaries);

변경

Record walkRecord = Record.createRecord(pet,StampType.WALK, walkDiaries);

record가 제한된 식별자라 walkRecord로 변경하였다.

[Code Smell] Merge this if statement with the enclosing one.

기존

if (updateRequestDto.getStamps() != null) {
  if (!oldDiaryStamps.isEmpty()) {
    DiaryStamp.removeDiaryStamp(oldDiaryStamps);
    stampRepository.deleteAll(oldStamps);
  }
}

변경

if (updateRequestDto.getStamps() != null && !oldDiaryStamps.isEmpty()) {
    DiaryStamp.removeDiaryStamp(oldDiaryStamps);
    stampRepository.deleteAll(oldStamps);
}

##



💛 개인 공부 기록용 블로그입니다. 👻

맨 위로 이동하기

태그:

카테고리:

업데이트: