블로그 이미지
암초보

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 29 30 31

Notice

Tag

05-02 10:04
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 암초보