블로그 이미지
암초보

calendar

1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28

Notice

Tag

02-07 17:38

'프로그래밍'에 해당되는 글 159

  1. 2011.09.24 예외처리 방법
  2. 2011.09.24 예외의 종류
  3. 2011.09.21 테스트...
  4. 2011.09.20 SOLID 원칙 관련 책
  5. 2011.09.19 STS(SpringSource Tool Suite) 다운로드
  6. 2011.09.19 Substitute Algorithm1
  7. 2011.09.19 Replace Method with Method Object
  8. 2011.09.19 Remove Assignments to Parameters
  9. 2011.09.19 Split Temporary Variable
  10. 2011.09.19 Introduce Explaining Variable
2011. 9. 24. 00:55 프로그래밍/Java

예외 복구
: 예외상황을 파악하고 문제를 해결해서 정상 상태로 돌려놓는 것
ex) 서버접속 실패시 정해진 횟수만큼 재시도

예외 회피
: catch 후 throw 문을 사용하여 rethrow

예외 전환
: 의미부여 또는 중첩예외(체크예외->런타임 예외)

'프로그래밍 > Java' 카테고리의 다른 글

추상화  (0) 2011.09.26
enum 활용  (0) 2011.09.25
예외의 종류  (0) 2011.09.24
Java 접근제한자  (0) 2011.09.18
Java transient  (0) 2011.09.05
posted by 암초보
2011. 9. 24. 00:45 프로그래밍/Java

체크 예외(checked exception)
:  Exception 클래스의 서브클래스이면서 RuntimeException 을 상속하지 않은 것

언체크  예외(unchecked exception) 
:  Exception 클래스의 서브클래스이면서 RuntimeException 클래스를 상속

'프로그래밍 > Java' 카테고리의 다른 글

enum 활용  (0) 2011.09.25
예외처리 방법  (0) 2011.09.24
Java 접근제한자  (0) 2011.09.18
Java transient  (0) 2011.09.05
Class.forName() 과 DriverManager  (0) 2011.08.31
posted by 암초보
2011. 9. 21. 03:47 프로그래밍/TDD

"특히 한가지 결과만 검증하고 마는 것은 상당히 위험하다. 이런 테스트는 마치 하루에 두 번은 정확히 맞는다는 시계와 같은 수도 있다. 죽은 시계 말이다." - 토비의 스프링3 중..

'프로그래밍 > TDD' 카테고리의 다른 글

TEST 용 import static 템플릿  (0) 2011.10.05
특정 테스트 메소드만 실행는 방법  (0) 2011.09.29
Mockito  (0) 2011.09.26
LineReader TDD 동영상  (0) 2010.11.03
JUnit 라이브러리 구조  (0) 2010.11.03
posted by 암초보
2011. 9. 20. 01:05 프로그래밍/책

밥마틴의
"Java 프로그래머를 위한 UML 실전에서는 이것만 쓴다"
"소프트웨어 개발의 지혜: 원칙, 디자인 패턴, 실천 방법(Agile Software Development, Prinsiples, Patterns, and Practices)"

'프로그래밍 > ' 카테고리의 다른 글

Clean Code  (0) 2014.11.21
zzzzzz  (0) 2011.09.15
볼책  (0) 2010.01.29
posted by 암초보
2011. 9. 19. 22:58 프로그래밍/Spring

'프로그래밍 > Spring' 카테고리의 다른 글

Advisor  (0) 2011.09.27
@ContextConfiguration 사용시  (0) 2011.09.27
프록시  (0) 2011.09.26
단일 책임 원칙  (0) 2011.09.26
스프링 빈으로 등록하기 전 검토사항  (0) 2011.09.26
posted by 암초보
2011. 9. 19. 04:04 프로그래밍/Refactoring

※ 알고리즘을 보다 명확한 것으로 바꾸고 싶을 때는, 메소드의 몸체를 새로운 알고리즘으로 바꾼다.

※ 절차
1. 대체 알고리즘을 준비하여 적용후 컴파일
2. 알고리즘 테스트. 만약 결과가 같다면 작업은 끝
3. 만약 결과가 같지 않다면, 테스트에서 비교하기 위해 예전의 알고리즘을 사용하여 디버깅

Before
String findPerson(String[] people) {
  for (int i=0; i<people.length; i++) {
    if(people[i].equals("Don")) {
      return "Don";
    }
    if(people[i].equals("John")) {
      return "John";
    }
    if(people[i].equals("Kent")) {
      return "Kent";
    }
  }
  return " ";
}


After
String findPerson(String[] people) {
  List candidates = Arrays.asList(new String[] {"Don", "John", "Kent"});
  for (int i=0; i<people.length; i++) {
    if(candidates.contains(people[i]))
      return people[i];
  }
  return " ";
}



//////////////////////////////////////////////////////////////////////////////////////////
출처 : 마틴 파울러의 리펙토링
//////////////////////////////////////////////////////////////////////////////////////////

'프로그래밍 > Refactoring' 카테고리의 다른 글

Replace Method with Method Object  (0) 2011.09.19
Remove Assignments to Parameters  (0) 2011.09.19
Split Temporary Variable  (0) 2011.09.19
Introduce Explaining Variable  (0) 2011.09.19
Replace Temp with Query  (0) 2011.09.19
posted by 암초보
2011. 9. 19. 03:52 프로그래밍/Refactoring
※ 긴 메소드가 있는데, 지역변수 때문에 Extract Method를 적용할 수 없는 경우에는,
메소드를 그 자신을 위한 객체로 바꿔서 모든 지역변수가 그 객체의 필드가 되도록 한다. 이렇게 하면 메소드를 같은 객체 안의 여러 메소드로 분해할 수 있다.

※ 절차
1. 메소드의 이름을 따서 새로운 클래스를 생성
2. 새로운 클래스에 원래 메소드가 있던 객체(소스 객체)를 보관하기 위한 final 필드를 하나 만들고, 메소드에서 사용되는 임시변수와 파라미터를 위한 필드를 만든다.
3. 새로운 클래스에 소스 객체와 파라미터를 취하는 생성자를 만든다.
4. 새로운 클래스에 compute라는 이름의 메소드를 만든다.
5. 원래의 메소드를 compute 메소드로 복사한다. 원래의 객체에 있는 메소드를 사용하는 경우에는 소스 객체 필드를 사용하도록 바꾼다.
6. 컴파일한다.
7. 새로운 클래스의 객체를 만들고 원래의 메소드를 새로 만든 객체의 compute 메소드를 호출하도록 바꾼다.
8. 지역변수가 모두 필드로 빠뀌었으므로, 파라미터를 넘길 필요 없이 마음대로 메소드를 분해할 수 있다.


Before
 class Account ....
   int gamma (int inputVal, int quantity, int yearToDate) {
      int importantValue1 = (inputVal * quantity) + delta();
      int importantValue2 = (inputVal * yearToDate) + 100;
      if ((yearToDate - importantValue1) > 100)
         importantValue2 -= 20;
      int importantValue3 = importantValue2 * 7;
      // 기타 등등
      return importantValue3 - 2 * importantValue1;
   }

After
 class Gamma ...
   private final Account _account;
   private int inputVal;
   private int quantity;
   private int yearToDate;
   private int importantValue1;
   private int importantValue2;
   private int importantValue3;
   Gamma(Account source, int inputValArg, int quantityArg, int yearToDateArg) {
      _account = source;
      inputVal = inputValArg;
      quantity = quantityArg;
      yearToDate = yearToDateArg;
   }
   int compute() {
      int importantValue1 = (inputVal * quantity) + _account.delta();
      int importantValue2 = (inputVal * yearToDate) + 100;
      if ((yearToDate - importantValue1) > 100)
         importantValue2 -= 20;
      int importantValue3 = importantValue2 * 7;
      // 기타 등등
      return importantValue3 - 2 * importantValue1;

   }
class Account ....
   int gamma (int inputVal, int quantity, int yearToDate) {
     return new Gamma(this, inputVal, yearToDate).compute(); 
   }


※ compute 메소드에서 파라미터를 넘겨주는 것에 대한 걱정 없이 쉽게 Extract Method를 사용할 수 있다.




//////////////////////////////////////////////////////////////////////////////////////////
출처 : 마틴 파울러의 리팩토링
//////////////////////////////////////////////////////////////////////////////////////////

'프로그래밍 > Refactoring' 카테고리의 다른 글

Substitute Algorithm  (1) 2011.09.19
Remove Assignments to Parameters  (0) 2011.09.19
Split Temporary Variable  (0) 2011.09.19
Introduce Explaining Variable  (0) 2011.09.19
Replace Temp with Query  (0) 2011.09.19
posted by 암초보
2011. 9. 19. 03:32 프로그래밍/Refactoring
※ 파라미터에 값을 대입하는 코드가 있으면, 대신 임시변수를 사용하도록 하라.

※ 자바에서는 값에 의한 전달만 사용, 참조에 의한 전달은 사용 안함

※ 절차
1. 파라미터를 위한 임시변수를 만든다.
2. 파라미터에 값을 대입한 코드 이후에서 파라미터에 대한 참조를 임시변수로 바꾼다.
3. 파라미터에 대입하는 값을 임시변수에 대입하도록 바꾼다.
4. 컴파일 및 테스트

Before
int discount (int inputVal, int quantity, int yearToDate) {
   if (inputVal > 50) inputVal -= 2;
   if (quantity > 100) inputVal -= 1;
   if (yearToDate > 10000) inputVal -= 4;
   return inputVal;
}

After
int discount (int inputVal, int quantity, int yearToDate) {
   int result = inputVal;
   if (inputVal > 50) result -= 2;
   if (quantity > 100) result -= 1;
   if (yearToDate > 10000) result -= 4;
   return result;
}

More....
파라미터를 final로 선언하여 이 관례를 강제할 수 있지만,
(자바 1.1부터 파라미터를 fianl로 하는 것이 가능해짐)
짧은 메소드에 대해서는 코드를 명확하게 하는데 별로 큰도움이 되지 않으므로 사용 안함.
int discount (final int inputVal, final int quantity, final int yearToDate) {
   int result = inputVal;
   if (inputVal > 50) result -= 2;
   if (quantity > 100) result -= 1;
   if (yearToDate > 10000) result -= 4;
   return result;
}




//////////////////////////////////////////////////////////////////////////////////////////
출처 : 마틴 파울러의 리팩토링
//////////////////////////////////////////////////////////////////////////////////////////

'프로그래밍 > Refactoring' 카테고리의 다른 글

Substitute Algorithm  (1) 2011.09.19
Replace Method with Method Object  (0) 2011.09.19
Split Temporary Variable  (0) 2011.09.19
Introduce Explaining Variable  (0) 2011.09.19
Replace Temp with Query  (0) 2011.09.19
posted by 암초보
2011. 9. 19. 03:16 프로그래밍/Refactoring

※ 루프 안에 있는 변수나 collecting temporary variable도 아닌 임시변수에 값을 여러 번 대입하는 경우에는, 각각의 대입에 대해서 따로따로 임시변수를 만들어라.

※ 하나의 임시변수를 두 가지 이상의 용도로 사용하면 코드를 보는 사람이 매우 혼란스러움

※ 절차
1. 임시변수가 처음 선언된 곳과 임시변수에 값이 처음 대입된 곳에서 변수의 이름을 바꿈
2. 임시변수를 final로 선언
3. 임시변수에 두 번째로 대입하는 곳의 직전까지 원래 임시변수를 참조하는 곳을 모두 바꾼다.
4. 임시변수에 두 두번째로 대입하는 곳에서 변수를 선언한다.
5. 컴파일 및 테스트
6. 각 단계를 반복

Before
double temp = 2 * (_height + _width);
System.out.println(temp);
temp = _height * _width;
System.out.println(temp); 

After
final double perimeter = 2 * (_height + _width);
System.out.println(perimeter);
final double area = _height * _width;
System.out.println(area); 





//////////////////////////////////////////////////////////////////////////////////////////
출처 : 마틴 파울러의 리팩토링
//////////////////////////////////////////////////////////////////////////////////////////

'프로그래밍 > Refactoring' 카테고리의 다른 글

Replace Method with Method Object  (0) 2011.09.19
Remove Assignments to Parameters  (0) 2011.09.19
Introduce Explaining Variable  (0) 2011.09.19
Replace Temp with Query  (0) 2011.09.19
Inline Temp  (0) 2011.09.18
posted by 암초보
2011. 9. 19. 02:54 프로그래밍/Refactoring

※ 복잡한 수식이 있는 경우에는, 수식의 결과나 또는 수식의 일부에 자신의 목적을 잘 설명하는 이름으로 된 임시변수를 사용하라.

※ 보통 Extract Method로 해결 - 임시변수는 한 메소드의 컨텍스트 내에서만 유용하지만 메소드는 객체의 모든 부분, 다른 객체에서도 유용
※ 지역변수 때문에 Extract Method의 사용이 어려운 경우 Introduce Explaining Variable 사용

※ 절차

1. final 변수를 선언하고, 복잡한 수식의 일부를 이 변수에 대입
2. 원래의 복잡한 수식에서, 임시변수에 대입한 수식을 임시변수로 바꾼다.
3. 컴파일 및 테스트
4. 수식의 다른 부분에 대해서도 위의 작업 반복

Before

double price() {
   // price = (base price) - (quantity discount) + (shipping);
   return _quantity * itemPrice -
      Math.max(0, _quantity - 500) * _itemPrice * 0.05 +
      Math.min(_quantity * itemPrice * 0.1, 100.0);
}

Step 1.
double price() {
   // price = (base price) - (quantity discount) + (shipping);
   final double basePrice = _quantity * itemPrice;
   return basePrice -
      Math.max(0, _quantity - 500) * _itemPrice * 0.05 +
      Math.min(_quantity * itemPrice * 0.1, 100.0);
}


Step 2.
double price() {
   // price = (base price) - (quantity discount) + (shipping);
   final double basePrice = _quantity * itemPrice;
   return basePrice -
      Math.max(0, _quantity - 500) * _itemPrice * 0.05 +
      Math.min(basePrice * 0.1, 100.0);
}

Step 3. (동일하게 모두 적용)
double price() {
   // price = (base price) - (quantity discount) + (shipping);
   final double basePrice = _quantity * itemPrice;
   final double quantityDiscount = Math.max(0, _quantity - 500) * _itemPrice * 0.05;
   final double shipping = Math.min(basePrice * 0.1, 100.0);
   return basePrice - quantityDiscount + shipping;
}



☞ Extract Method를 사용한 경우
Before
double price() {
   // price = (base price) - (quantity discount) + (shipping);
   return _quantity * itemPrice -
      Math.max(0, _quantity - 500) * _itemPrice * 0.05 +
      Math.min(_quantity * itemPrice * 0.1, 100.0);
}

After
double price() {
   // price = (base price) - (quantity discount) + (shipping);
   return basePrice() - quantityDiscount() + shipping();
}
private double quantityDiscount() {
   return Math.max(0, _quantity - 500) * _itemPrice * 0.05 ;
}
private double shipping() {
   return Math.min(basePrice() * 0.1, 100.0);
}
private double basePrice() {
   return _quantity * itemPrice;
}




//////////////////////////////////////////////////////////////////////////////////////////
출처 : 마틴 파울러의 리팩토링
//////////////////////////////////////////////////////////////////////////////////////////

'프로그래밍 > Refactoring' 카테고리의 다른 글

Remove Assignments to Parameters  (0) 2011.09.19
Split Temporary Variable  (0) 2011.09.19
Replace Temp with Query  (0) 2011.09.19
Inline Temp  (0) 2011.09.18
Inline Method  (0) 2011.09.18
posted by 암초보