Anti-Pattern

μΈμŠ€ν„΄μŠ€ 생성 사둀

λΆˆλ³€ (Immutable Class) 객체의 μž¬μ‚¬μš©μ„ ν•œλ‹€.

생성후 λ³€κ²½ λΆˆκ°€λŠ₯ν•œ κ°μ²΄λ‘œμ„œ λŒ€ν‘œμ μœΌλ‘œ String, Boolean, Integer, Float, Long 등등이 μžˆλ‹€.

Bad Code

String str = new String("bikini");

Better Code

String str = "bikini";

μ΅œμ‹  JVM μƒμ—μ„œλŠ” κ³ μ • λ¬Έμžμ—΄μΌλ•Œ μ»΄νŒŒμΌλŸ¬κ°€ μ²˜μŒλΆ€ν„° λ°”μ΄νŠΈ μ½”λ“œλ₯Ό StringBuilder 둜 λ³€κ²½λ˜μ–΄ μΈμŠ€ν„΄μŠ€κ°€ μƒμ„±λ˜μ§€ μ•ŠλŠ”λ‹€.
ν•˜μ§€λ§Œ 가급적 λ¬Έμžμ—΄ 연산이 λ§Žλ‹€λ©΄ StringBuilder λ₯Ό ꢌμž₯ 동기화λ₯Ό μ—Όλ €ν•œλ‹€λ©΄ StringBuffer λ₯Ό ꢌμž₯

μ˜€ν†  λ°•μ‹±

λ°•μ‹±λœ Warpper ν˜• λ³΄λ‹€λŠ” μ›μ‹œ κΈ°λ³Έ νƒ€μž…μ„ μ‚¬μš©ν•œλ‹€.

private static long sum() {
  Long sum = 0L;
  for (long i = 0; i <= Integer.MAX_VALUE; i++) {
    sum += i;
  }

  return sum;
}

Stack Class 클래슀의 λ©”λͺ¨λ¦¬ λˆ„μˆ˜

자기 λ©”λͺ¨λ¦¬λ₯Ό 직접 κ΄€λ¦¬ν•˜λŠ” 클래슀라면 λ©”λͺ¨λ¦¬ λˆ„μˆ˜μ— μ£Όμ˜ν•΄μ•Ό ν•œλ‹€.

  • μΊμ‹œμš©λ„λ‘œ μ‚¬μš©ν•˜λŠ” κ°μ²΄λŠ” WeackHashMap 을 μ‚¬μš©
  • μŠ€νƒκ³Ό 같은 κ°μ²΄λŠ” μ‚¬μš©ν›„ null 둜 μ°Έμ‘°ν•΄μ œλ₯Ό μ‹œμΌœμ£Όλ„λ‘ ν•œλ‹€.
public class Stack {
  private Object[] elements;
  ...
  public Object pop() {
    if (size == 0) {
      throw new EmptyStackException();
    }

    Object result = elements[--size];

    // λ‹€ μ“΄ μ°Έμ‘°λ₯Ό ν•΄μ œ 해주도둝 ν•˜μž
    elements[--size] = null;

    return result;
  }
}

Finalize Method

finalize λ©”μ†Œλ“œμ— μ˜ν•œ Collection 지연과 OOME (Out of Memory Exception) λ°œμƒ κ°€λŠ₯μ„± λ•Œλ¬Έμ— μ‚¬μš©μ„ μ§€μ–‘ν•œλ‹€.

νŠΉμ • Class μ—μ„œ finalize λ©”μ†Œλ“œκ°€ μ •μ˜λ˜μ–΄ μžˆλŠ” 경우 ν•΄λ‹Ή Class Type 의 Object λŠ” Gabege Collection λ°œμƒμ‹œ μ¦‰κ°μ μœΌλ‘œ Collction λ˜μ§€ μ•ŠλŠ”λ‹€.
μ΄λŠ” Finalization Queue 에 λ“€μ–΄κ°„ ν›„ Finalizer 에 μ˜ν•΄ μ •λ¦¬λ˜λŠ”λ° μ°Έμ‘°κ°€ ν•΄μ œλœ 객체에 λŒ€ν•΄μ„œλ„ finalize λ©”μ†Œλ“œμ— μ˜ν•΄ GC 싀행이 보μž₯λ˜μ§€ μ•ŠλŠ”λ‹€.

λŒ€μ•ˆ

AutoCloseable 을 κ΅¬ν˜„ν•΄μ£Όκ³  ν΄λž˜μŠ€μ—μ„œ μΈμŠ€ν„΄μŠ€λ₯Ό λ‹€ μ‚¬μš©ν•˜λ©΄ close λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•΄μ€€λ‹€.

λ¬Έμžμ—΄ μ‘°ν•©

Bad Code



Β 


Β 

String str = "";
for (Person p : persons) {
  str += ", " + p.getName();
}

s = s.substring(2);

μœ„ μ½”λ“œλŠ” loop μ•ˆμ—μ„œ String 의 concatenation 을 λ°˜λ³΅ν•˜λ©° ν•„μš”μ—†λŠ” array copy 와 garbage λ₯Ό λ‚¨λ°œν•œλ‹€.
λ§ˆμ§€λ§‰μ—λŠ” 콀마λ₯Ό μ œκ±°ν•˜λŠ” 연산을 ν•œ 번 더 ν•΄μ€˜μ•Ό ν•œλ‹€.

Better Code

StringBuilder sb = new StringBuilder(persons.size() * 16);
for (Person p : persons) {
  if (sb.length() > 0) sb.append(", ");
  sb.append(p.getName());
}

StringBuffer μ„±λŠ₯

Bad Code

Β 

Β 




StringBuffer sb = new StringBuffer();
sb.append("Name : ");
sb.append(name + '\n');
sb.append("!");
...
String s = sb.toString();

μœ„μ—μ„œ 3λΌμΈμ—μ„œ String concatenation 이 μΌμ–΄λ‚œλ‹€. λ˜ν•œ buffer μ‚¬μ΄μ¦ˆλ₯Ό μ΄ˆκΈ°ν™” ν•˜μ§€ μ•ŠμŒμœΌλ‘œ 인해 λΆˆν•„μš”ν•œ resizing(array copy) κ°€ 일어날 수 μžˆλ‹€.

J2SE 5 μ΄μƒμ—μ„œλŠ” synchronization 이 ν•„μš”μ—†λŠ” 둜컬 λ³€μˆ˜μ—λŠ” StringBuffer κ°€ μ•„λ‹Œ StringBuilder 둜 μ‚¬μš©ν•΄μ•Ό ν•œλ‹€.

Good Code

StringBuilder sb = new StringBuilder(100);
sb.append("Name : ");
sb.append(name);
sb.append('\n');
sb.append('!');

String s = sb.toString();

String 비ꡐ

Bed Code

if (name.compareTo("John") == 0) { /* statement */ }
if (name == "John") { /* statement */ }
if (name.equals("John")) { /* statement */ }
if ("".equals(name)) { /* statement */ }

== μ—°μ‚°μžλŠ” 객체 λ™μΉ˜μ— λŒ€ν•œ 연산을 μˆ˜ν–‰ν•œλ‹€. equals() λ©”μ„œλ“œμ˜ λ³€μˆ˜μ™€ μƒμˆ˜μ˜ μœ„μΉ˜λ₯Ό λ°”κΎΈλ©΄ NPE (Null Pointer Exception) λ₯Ό ν”Όν• μˆ˜ μžˆλŠ” μ•ˆμ •μ„±λ„ μ–»μ„μˆ˜ μžˆλ‹€. 빈 λ¬Έμžμ—΄ 체크할 λ•ŒλŠ” λ¬Έμžμ—΄ 길이λ₯Ό μ²΄ν¬ν•˜λŠ” 것이 λΉ λ₯Έλ° equals() 의 λ©”μ„œλ“œκ°€ hash code λ₯Ό λ¨Όμ € κ³„μ‚°ν•˜κΈ° λ•Œλ¬Έμ΄λ‹€.

Better Code

if ("John".equals(name)) { /* statement */ }
if (name.length() == 0) { /* statement */ }

숫자λ₯Ό λ¬Έμžμ—΄λ‘œ λ³€ν™˜

Bad Code

"" + set.size();
new Integer(set.size()).toString();

1번 λΌμΈμ—μ„œ String concatenation 연산을 λΆˆν•„μš”ν•˜κ²Œ ν•΄μ€˜μ•Ό ν•œλ‹€. 2번 λΌμΈμ—μ„œλŠ” toString() λ©”μ„œλ“œλ₯Ό μˆ˜ν–‰ν•˜κΈ° μœ„ν•΄μ„œ Integer 클래슀λ₯Ό μƒμ„±ν•˜κΈ°κΉŒμ§€ ν•œλ‹€.

Better Code

String.valueOf(set.size());

λΆˆλ³€ 객체 μ‚¬μš©

Bad Code

new Integer(0);
return Boolean.valueOf("true");

Integer ν΄λž˜μŠ€μ™€ Boolean ν΄λž˜μŠ€λŠ” λΆˆλ³€ 객체 (Immutable Object) 이닀.
λ•Œλ¬Έμ— 뢈 ν•„μš”ν•˜κ²Œ 객체λ₯Ό 생성할 ν•„μš”κ°€ μ—†λ‹€.

Integer 와 Boolean ν΄λž˜μŠ€λ“€μ€ 자주 μ‚¬μš©ν•˜λŠ” Instance 에 λŒ€ν•΄μ„œ Built-In Cache κ°€ μ‘΄μž¬ν•œλ‹€.

Boolean 클래슀의 κ²½μš°μ—λŠ” 값이 μ˜€λ‘œμ§€ TRUE / FALSE 두가지 뿐이닀.

Better Code

Integer.valueOf(0);
return Boolean.TRUE;

참고자료

https://jnylove.tistory.com/192