-
Notifications
You must be signed in to change notification settings - Fork 227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
๐ 1๋จ๊ณ - ๋ฌธ์์ด ๋ง์ ๊ณ์ฐ๊ธฐ #776
Conversation
private ํจ์์ ํ์ ๋์ง ์๊ณ ์ผ๋ฐ์ ์ผ๋ก ๊ฐ์ด ๋ถ๋ณํ์ง ๋ณ๊ฒฝ ๊ฐ๋ฅํ์ง๋ง ํ๋จ ํด๋ณด๋ฉด ๋ ๊ฒ ๊ฐ์ต๋๋ค. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฌธ์์ด ๊ณ์ฐ๊ธฐ ๊ตฌํ ์ํด์ฃผ์
จ์ต๋๋ค ๐
๋ค์ ๋ฏธ์
์ ์งํํ๊ธฐ ์ ๋ช๊ฐ์ง ์๊ฐ๊ฑฐ๋ฆฌ๋ฅผ ๋จ๊ฒจ๋์์ผ๋ ํ์ธ ๋ถํ๋๋ ค์ ๐
private static final String DELIMITER = "//(.)\n(.*)"; | ||
|
||
public int add(final String text) { | ||
if (text == null || text.isEmpty()) return 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (text == null || text.isEmpty()) return 0; | |
if (text == null || text.isEmpty()) { | |
return 0; | |
} |
https://google.github.io/styleguide/javaguide.html#s4.1.1-braces-always-used
๊ตฌ๊ธ ์คํ์ผ ๊ฐ์ด๋๋ฅผ ์ฐธ๊ณ ํ์ฌ ์ค๊ดํธ๋ฅผ ์๋ตํ์ง ์๊ณ ์์ฑํด๋ณด์ธ์ ๐
try { | ||
String[] numbers = parse(text); | ||
return sumOfNumbers(numbers); | ||
} catch (Exception e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ต์์ Exception์ catch ํ๋ค๋ฉด ์ด๋ค ๋ฌธ์ ์ ์ด ์์๊น์ ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด๋ค ์์ธ์ธ์ง ์ ์ ์์ต๋๋ค.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ค ๋ง์ต๋๋ค ๐
์ด๋ค ์์ธ์ธ์ง ์์ ์์ ๋ฟ๋๋ผ ํ์ Exception์ ๋ฌด์ํด๋ฒ๋ฆฌ๋ ์ํฉ์ด ๋ฐ์ํ๊ฒ ๋ฉ๋๋ค.
์์ธ ์ฒ๋ฆฌ์ ๊ด๋ จํด์ ์๋ ๋ฌธ์๊ฐ ๋์์ด ๋์ค๊ฑฐ์์ ๐
https://jojoldu.tistory.com/734
} | ||
|
||
private String[] parse(String text) { | ||
Matcher m = Pattern.compile(DELIMITER).matcher(text); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋งค๋ฒ Pattern.compile(DELIMITER)
์ ํตํด ๊ฐ์ฒด๊ฐ ์์ฑ๋์ง ์๊ณ ์ฌ์ฌ์ฉํ ์ ์๋๋ก ๋ณ๊ฒฝํด๋ณด๋๊ฑด ์ด๋จ๊น์ ?
.sum(); | ||
} | ||
|
||
private int validate(String number) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด๋ฒ ๊ณผ์ ์ ์งํํ๋ฉฐ ์ต๋ํ ์์ ๋จ์์ ํด๋์ค๋ฅผ ๋์ถํด๋ณด๋๊ฒ์ ๊ถ์ฅ๋๋ฆฝ๋๋ค.
์์๋ฅผ ๋ณด์ฅํ ์ ์๋ ํด๋์ค๋ฅผ ์ถ๊ฐํด๋ณด์ธ์ ๐
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class StringCalculator { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฌธ์์ด ๊ณ์ฐ๊ธฐ ๊ตฌํ ์ํด์ฃผ์
จ์ต๋๋ค ๐
ํ๊ฐ์ง ๊ฐ์ ์ฌํญ์ผ๋ก ํ์ฌ StringCalculator
๊ฐ ๋๋ฌด ๋ง์ ์ญํ ์ ๋ด๋นํ๊ณ ์๋ค๊ณ ์๊ฐ๋ฉ๋๋ค.
๋ฌธ์์ด ํ์ฑ์ ๋ด๋นํ๋ ํด๋์ค, ๊ณ์ฐ์ ๋ด๋นํ๋ ํด๋์ค ๋ฑ์ผ๋ก ์ญํ ์ ๋๋๋ ์ฐ์ต์ ํด๋ณด์ธ์ !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์์ธํ ํด๋์ค ๋ถ๋ฆฌ ๋๋ฌด ์ํด์ฃผ์ จ์ต๋๋ค ๐
private int validate(String number) { | ||
try { | ||
int num = Integer.parseInt(number); | ||
if (num < 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋งค์ง๋๋ฒ๋ ์๋ฏธ๋ฅผ ๊ฐ์ง๋ ์์๋ก ์ ์ํ์ฌ ๊ฐ๋
์ฑ์ ๋ํ๋ณด์ธ์ ๐
https://www.slipp.net/questions/356
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํด์ฃผ์ ํผ๋๋ฐฑ ๋ฐํ์ผ๋ก ๋ฆฌํฉํ ๋งํ์์ต๋๋ค !
์์ธํ ํผ๋๋ฐฑ ํด์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค ๐
1, ๋ฌธ์์ด ํ์ฑ ํด๋์ค, ๊ณ์ฐ ๋ด๋น ํด๋์ค ์ญํ ๋ถ์ฐ
2. ์์ ๋ณด์ฅ ํด๋์ค ์์ฑ
3. ์์ธ์ฒ๋ฆฌ ์ธ๋ถํ
4. Pattern ์ฌ์ฌ์ฉ
์ํด ๋ณต ๋ง์ด ๋ฐ์ผ์ธ์ ๐ค
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํผ๋๋ฐฑ ๋ฐ์ ์ํด์ฃผ์
จ์ต๋๋ค ๐
์ฌ์ํ ์ฝ๋ฉํธ ๋ช๊ฐ์ง ๋จ๊ฒจ๋์๋๋ฐ ํ์ธ ๋ถํ ๋๋ฆฌ๋ฉฐ ๋ค์ ๋ฏธ์
๋ ํ์ดํ
ํด์ฃผ์ธ์ !
|
||
} | ||
|
||
class PositiveNumber { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์์๋ฅผ ๋ณด์ฅํ๋ ํด๋์ค ์ ์ธ ๐
public int getValue() { | ||
return value; | ||
} | ||
|
||
private PositiveNumber(int value) { | ||
if (value < 0) { | ||
throw new RuntimeException("์์ ์ ์๋ฅผ ์ ๋ ฅํ์ธ์."); | ||
} | ||
this.value = value; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๏ฟฝ์ ์ธ ์์๊ฐ ์๋ชป ๋์ด ์๋๊ฒ ๊ฐ์ต๋๋ค ๐
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get๋ฉ์๋๋ณด๋ค ์์ฑ์๊ฐ ๋ ์ฐ์ ์ผ๋ก ์ ์ธ๋์ด์ผ ํ๊ตฐ์ !
์ฃผ์ํ๊ฒ ์ต๋๋ค ๊ฐ์ฌํฉ๋๋ค ๐
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class StringCalculator { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์์ธํ ํด๋์ค ๋ถ๋ฆฌ ๋๋ฌด ์ํด์ฃผ์ จ์ต๋๋ค ๐
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
|
||
class StringCalculatorTest { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์๋กญ๊ฒ ํด๋์ค๊ฐ ๋ง์์ง์ ๋ฐ๋ผ ํ
์คํธ ์ฝ๋๋ ์์ฑํด๋ณด์ธ์ ๐
์ด๋ฒ ๊ณผ์ ์ ์งํํ๋ฉด์ ๋จ์ ํ
์คํธ ์์ฑ์ ๋ํ ์ฐ์ต์ ํด๋ณด๋๊ฒ์ ๊ถ์ฅ ๋๋ฆฝ๋๋ค !
์๋ ํ์ธ์.
์๊ตฌ์ฌํญ๋ณ๋ก ์ปค๋ฐํ๋ ค ํ์ผ๋, ๋ชจ๋ ํ ์คํธ ์กฐ๊ฑด์ ๋ง์กฑ์์ผ์ผ ํ๋ค๋ ์๊ฐ์ ๋ถ๋ฆฌํ์ฌ ์ปค๋ฐํ๋๊ฒ ์ด๋ ค์ ์ต๋๋ค.
add ๋ด ๋งค๊ฐ๋ณ์ text๋ ๋ณ๊ฒฝ ๋ถ๊ฐํ ๊ฐ์ด๋ฏ๋ก final ์ ์ธ์ด ๋ง๋ค๊ณ ์๊ฐํ์ต๋๋ค.
๋ด๋ถ์ private ํจ์๋ค ์ค text๋ฅผ ๋งค๊ฐ๋ณ์๋ก ๋ฐ๋ ๊ฒ๋ค๋ ๋ชจ๋ final์ ์ ์ธํด์ผํ๋์ง? ์๋๋ฉด, ์ด๋ฏธ add ๋งค๊ฐ๋ณ์์์ final text๋ก ๋ค์ด์์ ์๊ด์๋์ง ๊ถ๊ธํฉ๋๋ค.