Skip to content

Commit d964c70

Browse files
committed
[아이템 51] 메서드 시그니처를 신중히 설계하라
1 parent 7904b6a commit d964c70

File tree

9 files changed

+269
-0
lines changed

9 files changed

+269
-0
lines changed

.idea/aws.xml

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/codeStyles/Project.xml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/codeStyles/codeStyleConfig.xml

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/effective-java.iml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

+98
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chapter08/item51/README.md

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
## 메서드 이름을 신중히 짓자
2+
3+
### 항상 표준 명명 규칙을 따라야 한다.
4+
그니까 add, remove 를 자신이 만든 자료구조에 적용한다고 했을때, 일반적으로 add 는 자료구조에 추가하는것, remove 는 자료구조에서 없어지는것과 같이. 일반적으로 통용되는 단어들로 작성하는것이 옳다
5+
6+
### 편의 메서드를 너무 많이 만들지 마라
7+
모든 메서드는 각각 자신의 소임을 다해야 한다. 근데 사실 음.. 이부분은 이해가 가지않는다. 관심사 분리가 덜 되어서 메소드가 많이 생기지 말라하는건지, 아니면 그냥 메소드가 많은건 안좋다고 하는건지
8+
9+
### 매개변수 목록은 짧게 유지하자. 4개 이하가 좋다.
10+
만약에 매개변수가 6개라면 다른 메소드로 쪼개어 4 / 2 로 나누거나 2 / 2 / 2 로 나누어 쪼개는 것이 좋다. <br>
11+
사실 관심사를 잘 나누자? 라는 이야기 같다. 도우미 클래스 등등을 생성하여
12+
13+
### 도우미 클래스
14+
15+
예전에 코드스쿼드 과제중에 나는 매개변수 getRange(x1,y1,x2,y2,x3,y3) 로 받았지만 파이로는 Point 라는 도우미 클래스를 만든뒤 <br>
16+
17+
```
18+
ap = Point(x1,y1)
19+
bp = Point(x2,y2)
20+
cp = Point(x3,y3)
21+
```
22+
23+
getRange(ab,bp,cp) 로 받았던 기억이난다. 그 이후로 나도 도우미 클래스를 많이 이용해봐야지 라는 생각이 들곤했다. <br>
24+
25+
### 빌더 패턴
26+
27+
Builder 패턴에 관한 이야기도 나오는데 뭐 앞에서 공부한사람들도 알겠지만, 선택 매개변수를 선언이 가능하거나, 매개변수가 많을때 사용하면 좋은 방법인데,
28+
간단한 구현은 아래코드와 같다.
29+
30+
```java
31+
public class NutritionFactsBuilder {
32+
33+
34+
private final int servingSize;
35+
private final int servings;
36+
private final int calories;
37+
private final int fat;
38+
private final int sodium;
39+
private final int carbohydrate;
40+
41+
public static class Builder {
42+
// 필수 매개변수
43+
private final int servingSize;
44+
private final int servings;
45+
46+
// 선택 매개변수 => 기본값으로 초기화 해줘야함.
47+
private int calories = 0;
48+
private int fat = 0;
49+
private int sodium = 0;
50+
private int carbohydrate = 0;
51+
52+
public Builder(int servingSize, int servings) {
53+
this.servingSize = servingSize;
54+
this.servings = servings;
55+
}
56+
57+
public Builder calories(int calories){
58+
this.calories = calories;
59+
return this;
60+
}
61+
62+
public Builder fat(int fat){
63+
this.fat = fat;
64+
return this;
65+
}
66+
67+
public Builder sodium(int sodium){
68+
if(sodium < 0){
69+
throw new IllegalArgumentException();
70+
}
71+
this.sodium = sodium;
72+
return this;
73+
}
74+
75+
public Builder carbohydrate(int carbohydrate){
76+
this.carbohydrate = carbohydrate;
77+
return this;
78+
}
79+
80+
public NutritionFactsBuilder build(){
81+
return new NutritionFactsBuilder(this);
82+
}
83+
}
84+
85+
public NutritionFactsBuilder(Builder builder){
86+
servings = builder.servingSize;
87+
servingSize = builder.servingSize;
88+
calories = builder.calories;
89+
fat = builder.fat;
90+
sodium = builder.sodium;
91+
carbohydrate = builder.carbohydrate;
92+
}
93+
}
94+
95+
---- main
96+
97+
public static void main(String[] args) {
98+
NutritionFactsBuilder build = new NutritionFactsBuilder.Builder(20, 84)
99+
.calories(10).fat(20).carbohydrate(30).sodium(40).build();
100+
}
101+
102+
```
103+
104+
### 매개변수의 타입으로는 인터페이스가 낫다
105+
106+
이건 조금 색달랐는데, 항상 DI 방식 말고는 인터페이스로 넘겨볼 생각을 잘 못했던것 같다. <br>
107+
예를들면 (HashSet set) 이런것보다는 (Set set) 으로 했을때, 어차피 구현체는 바뀌어도 로직상에 문제는 생기지 않으니까.. <br>
108+
이런걸 너무 늦게 알지는 않았나 싶다..?
109+
110+
### boolean 보다는 원소 2개짜리 열거타입이 낫다
111+
112+
```
113+
enum TemperatureScale {FAHRENHEIT, CELSIUS}
114+
115+
Thermometer.newInstance(true);
116+
Thermometer.newInstance(TemperatureScale.FAHRENHEIT);
117+
```
118+
119+
저게더 보기 좋을때는 저렇게 사용하라는 뜻이다. 근데 true 를 받아줘야 작동되는 로직에는 true 를 쓰는게 좋다

0 commit comments

Comments
 (0)