본문 바로가기
  • SDXL 1.0 + 한복 LoRA
  • SDXL 1.0 + 한복 LoRA
Development/JAVA

[디비 파기 | PMD] FinalFieldCouldBeStatic 외 4건

by 마즈다 2016. 3. 26.
반응형



FinalFieldCouldBeStatic


우선순위 : 3

 

컴파일 타임에 상수가 할당되는 final 필드는 자동으로 static으로 변환된다.
따라서 이러한 필드들은 runtime시의 부하를 줄여주기 위해 static을 명시적으로
지정해주는 것이 좋다.

 

샘플 코드



OptimizableToArrayCall


우선순위 : 3

Calls to a collection’s toArray() method should specify target arrays sized to match the size of the collection. Initial arrays that are too small are discarded in favour of new ones that have to be created that are the proper size.

컬렉션 객체의 toArray() 메소드를 호출할 때에는 대상 배열 크기를 명확하게 컬렉션
객체의 크기와 동일하게 맞춰야 한다.

너무 작은 크기의 초기 배열은 알맞은 크기의 배열이 생성되도록 하기 위해
그냥 버려지기 때문에 비효율적이다.

 

샘플 코드



ConfusingTernary


우선순위 : 3

Avoid negation within an “if” expression with an “else” clause. For example, rephrase:

if (x != y) diff(); else same(); as: if (x == y) same(); else diff();

Most “if (x != y)” cases without an “else” are often return cases, so consistent use of this rule makes the code easier to read. Also, this resolves trivial ordering problems, such as “does the error case go first?” or “does the common case go first?”.

 

비교구문에 부정연산자(!)를 사용하는 것은 피하는 것이 좋다.
어차피 if (x != y) diff(); else same();와 if (x == y) same(); else diff();는
동일하며 대체로 “if (x != y)”로 표현하는 경우에는 else문 없이 if문 안에서 바로
리턴하는 경우가 많기 때문에 이러한 룰을 일관성 있게 지켜서 코딩하는 것이
가독성을 높이는 방법 중 하나다.

 

그리고 이러한 것들이 사소하게는 문제 처리의 순서
즉,에러를 먼저 처리할 것인지, 정상 코드를 먼저 처리할 것인지에 대한 부분도
해결해 줄 수 있다.

 

샘플 코드



SimpleDateFormatNeedsLocale


우선순위 : 3

Be sure to specify a Locale when creating SimpleDateFormat instances to ensure that locale-appropriate formatting is used.

 

적절한 지역화 포맷이 필요한 경우에는 SimpleDateFormat의 인스턴스를 생성할 때
확실하게 locale을 지정하라.

 

샘플 코드



SimplifyConditional


우선순위 : 3

No need to check for null before an instanceof; the instanceof keyword returns false when given a null argument.

 

조건절에 instanceof 연산자를 사용할 경우에는 그 앞에 null 체크를 할 필요가 없다.
instanceof 연산자는 비교 대상이 null인 경우 false를 리턴한다.

 

샘플 코드

 

부연 설명

오라클의 자바 공식 문서에 보면 instanceof 연산자는 연산 대상으로 참조 타입이나
null 타입을 사용할 수 있다고 합니다. 그렇지 않은 경우 컴파일시에 오류가 발생을
합니다.

 

따라서 연산자로 null이 사용되는 경우에는 문제가 없으나 int, String, boolean 같은
기본형을 대상으로 하는 경우에는 컴파일 에러가 발생합니다.


반응형