Skip to content

Commit

Permalink
translate(ko): Functions
Browse files Browse the repository at this point in the history
  • Loading branch information
738 committed Apr 18, 2020
1 parent c1d21b8 commit 7559d4a
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ if (!isEmailUsed(node)) {

### 쑰건문을 ν”Όν•˜μ„Έμš”

This seems like an impossible task. Upon first hearing this, most people say, "how am I supposed to do anything without an `if` statement?" The answer is that you can use polymorphism to achieve the same task in many cases. The second question is usually, "well that's great but why would I want to do that?" The answer is a previous clean code concept we learned: a function should only do one thing. When you have classes and functions that have `if` statements, you are telling your user that your function does more than one thing. Remember, just do one thing.
λΆˆκ°€λŠ₯ν•œ μž‘μ—…μœΌλ‘œ 보일 수 μžˆμŠ΅λ‹ˆλ‹€. 이 말을 처음 λ“€μ—ˆμ„ λ•Œ, λŒ€λΆ€λΆ„μ€ "`if`λ¬Έ 없이 λ‚΄κ°€ 뭘 ν•  수 μžˆκ² μŠ΅λ‹ˆκΉŒ?" 라고 λ§ν•©λ‹ˆλ‹€. 해닡은 λ§Žμ€ κ²½μš°μ— μ΄λŸ¬ν•œ μž‘μ—…μ„ μ™„μˆ˜ν•˜κΈ° μœ„ν•΄ λ‹€ν˜•μ„±μ„ μ‚¬μš©ν•˜λ©΄ λœλ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€. 그러면 두 번째 μ§ˆλ¬ΈμœΌλ‘œλŠ” 주둜 "맀우 μ’‹μŠ΅λ‹ˆλ‹€. ν•˜μ§€λ§Œ μ™œ κ·Έλ ‡κ²Œ ν•΄μ•Όν•©λ‹ˆκΉŒ?" μž…λ‹ˆλ‹€. κ·Έ 해닡은 μš°λ¦¬κ°€ 이전에 배운 클린 μ½”λ“œ μ»¨μ…‰μž…λ‹ˆλ‹€: ν•¨μˆ˜λŠ” ν•œ 가지 일만 ν•΄μ•Όν•©λ‹ˆλ‹€. `if`문이 μžˆλŠ” ν΄λž˜μŠ€μ™€ ν•¨μˆ˜κ°€ μžˆλ‹€λ©΄, κ·Έ ν•¨μˆ˜λŠ” ν•œ 가지 μ΄μƒμ˜ 일을 ν•˜κ³  μžˆλ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€. ν•¨μˆ˜λŠ” ν•œ 가지 일만 ν•΄μ•Όν•œλ‹€λŠ” 것을 κΈ°μ–΅ν•˜μ„Έμš”.

**Bad:**

Expand Down Expand Up @@ -937,9 +937,9 @@ class Cessna extends Airplane {

### νƒ€μž… 체킹을 ν”Όν•˜μ„Έμš”

TypeScript is a strict syntactical superset of JavaScript and adds optional static type checking to the language.
Always prefer to specify types of variables, parameters and return values to leverage the full power of TypeScript features.
It makes refactoring more easier.
νƒ€μž…μŠ€ν¬λ¦½νŠΈλŠ” μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ μ—„κ²©ν•œ ꡬ문적 μƒμœ„ 집합이며 언어에 선택적인 정적 νƒ€μž… 검사 κΈ°λŠ₯을 μΆ”κ°€ν•©λ‹ˆλ‹€.
νƒ€μž…μŠ€ν¬λ¦½νŠΈμ˜ κΈ°λŠ₯을 μ΅œλŒ€ν•œ ν™œμš©ν•˜κΈ° μœ„ν•΄ 항상 λ³€μˆ˜μ˜ νƒ€μž…, λ§€κ°œλ³€μˆ˜, λ°˜ν™˜κ°’μ˜ νƒ€μž…μ„ μ§€μ •ν•˜λ„λ‘ ν•˜μ„Έμš”.
κ·Έλ ‡κ²Œ ν•˜λ©΄ λ¦¬νŒ©ν† λ§μ΄ 맀우 μ‰¬μ›Œμ§‘λ‹ˆλ‹€.

**Bad:**

Expand Down Expand Up @@ -967,13 +967,13 @@ function travelToTexas(vehicle: Vehicle) {

### ν•„μš” μ΄μƒμœΌλ‘œ μ΅œμ ν™”ν•˜μ§€ λ§ˆμ„Έμš”

Modern browsers do a lot of optimization under-the-hood at runtime. A lot of times, if you are optimizing then you are just wasting your time. There are good [resources](https://github.com/petkaantonov/bluebird/wiki/Optimization-killers) for seeing where optimization is lacking. Target those in the meantime, until they are fixed if they can be.
ν˜„λŒ€ λΈŒλΌμš°μ €λŠ” λŸ°νƒ€μž„μ—μ„œ λ§Žμ€ μ΅œμ ν™”λ₯Ό μˆ˜ν–‰ν•©λ‹ˆλ‹€. λ§Žμ€ μ‹œκ°„μ„ μ΅œμ ν™”ν•˜λŠ” 데에 μ‚¬μš©ν•˜κ³  μžˆλ‹€λ©΄ μ‹œκ°„ λ‚­λΉ„μž…λ‹ˆλ‹€. μ΅œμ ν™”κ°€ λΆ€μ‘±ν•œ 뢀뢄을 확인할 수 μžˆλŠ” 쒋은 [자료](https://github.com/petkaantonov/bluebird/wiki/Optimization-killers)κ°€ μžˆμŠ΅λ‹ˆλ‹€. 이것을 μ°Έμ‘°ν•˜μ—¬ μ΅œμ ν™”κ°€ λΆ€μ‘±ν•œ λΆ€λΆ„λ§Œ μ΅œμ ν™”ν•΄μ€„ 수 μžˆμŠ΅λ‹ˆλ‹€.

**Bad:**

```ts
// On old browsers, each iteration with uncached `list.length` would be costly
// because of `list.length` recomputation. In modern browsers, this is optimized.
// μ˜ˆμ „ λΈŒλΌμš°μ €μ—μ„œλŠ” μΊμ‹œλ˜μ§€ μ•Šμ€ `list.length`λ₯Ό μ‚¬μš©ν•œ 각 μˆœνšŒλŠ” λΉ„μš©μ΄ 많이 λ“€ κ²ƒμž…λ‹ˆλ‹€.
// `list.length`의 μž¬κ³„μ‚° λ•Œλ¬Έμž…λ‹ˆλ‹€. ν˜„λŒ€ λΈŒλΌμš°μ €μ—μ„œλŠ” 이 뢀뢄이 μ΅œμ ν™”λ©λ‹ˆλ‹€.
for (let i = 0, len = list.length; i < len; i++) {
// ...
}
Expand All @@ -991,8 +991,8 @@ for (let i = 0; i < list.length; i++) {

### ν•„μš”ν•˜μ§€ μ•ŠλŠ” μ½”λ“œλŠ” μ œκ±°ν•˜μ„Έμš”

Dead code is just as bad as duplicate code. There's no reason to keep it in your codebase.
If it's not being called, get rid of it! It will still be safe in your version history if you still need it.
μ‚¬μš©ν•˜μ§€ μ•Šμ€ μ½”λ“œλŠ” μ€‘λ³΅λœ μ½”λ“œλ§ŒνΌ λ‚˜μ©λ‹ˆλ‹€. λ‹Ήμ‹ μ˜ μ½”λ“œμ—μ„œ 이것을 μœ μ§€ν•  μ΄μœ λŠ” μ—†μŠ΅λ‹ˆλ‹€.
ν˜ΈμΆœλ˜μ§€ μ•Šμ€ μ½”λ“œκ°€ μžˆλ‹€λ©΄ μ œκ±°ν•˜μ„Έμš”! μ§€μš΄ μ½”λ“œλ₯Ό λ‹€μ‹œ 확인할 ν•„μš”κ°€ μžˆλ‹€λ©΄ 버전 κΈ°λ‘μ—μ„œ λ³Ό 수 μžˆμŠ΅λ‹ˆλ‹€.

**Bad:**

Expand Down Expand Up @@ -1024,14 +1024,13 @@ inventoryTracker('apples', req, 'www.inventory-awesome.io');

### μ΄ν„°λ ˆμ΄ν„°μ™€ μ œλ„ˆλ ˆμ΄ν„°λ₯Ό μ‚¬μš©ν•˜μ„Έμš”

Use generators and iterables when working with collections of data used like a stream.
There are some good reasons:
슀트림과 같이 μ‚¬μš©λ˜λŠ” 데이터 μ½œλ ‰μ…˜μ„ μ‚¬μš©ν•  λ•ŒλŠ” μ œλ„ˆλ ˆμ΄ν„°μ™€ μ΄ν„°λ ˆμ΄λΈ”μ„ μ‚¬μš©ν•˜μ„Έμš”.
λͺ‡ κ°€μ§€μ˜ 쒋은 μ΄μœ κ°€ μžˆμŠ΅λ‹ˆλ‹€:

- decouples the callee from the generator implementation in a sense that callee decides how many
items to access
- lazy execution, items are streamed on demand
- built-in support for iterating items using the `for-of` syntax
- iterables allow to implement optimized iterator patterns
- ν”Όν˜ΈμΆœμžκ°€ μ ‘κ·Όν•  μ•„μ΄ν…œ 수λ₯Ό κ²°μ •ν•œλ‹€λŠ” μ˜λ―Έμ—μ„œ ν”Όν˜ΈμΆœμžλ₯Ό μ œλ„ˆλ ˆμ΄ν„° κ΅¬ν˜„μœΌλ‘œλΆ€ν„° 뢄리할 수 μžˆμŠ΅λ‹ˆλ‹€.
- 지연 μ‹€ν–‰, μ•„μ΄ν…œμ€ μš”κ΅¬μ— μ˜ν•΄ 슀트림 처리될 수 μžˆμŠ΅λ‹ˆλ‹€.
- `for-of` ꡬ문을 μ‚¬μš©ν•΄ μ•„μ΄ν…œμ„ μˆœνšŒν•˜λŠ” λ‚΄μž₯ 지원이 μžˆμŠ΅λ‹ˆλ‹€.
- μ΄ν„°λ ˆμ΄λΈ”μ€ μ΅œμ ν™”λœ μ΄ν„°λ ˆμ΄ν„° νŒ¨ν„΄μ„ κ΅¬ν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

**Bad:**

Expand All @@ -1052,15 +1051,15 @@ function print(n: number) {
fibonacci(n).forEach(fib => console.log(fib));
}
// Print first 10 Fibonacci numbers.
// ν”Όλ³΄λ‚˜μΉ˜ 숫자의 첫 번째 10개 숫자λ₯Ό 좜λ ₯ν•©λ‹ˆλ‹€.
print(10);
```

**Good:**

```ts
// Generates an infinite stream of Fibonacci numbers.
// The generator doesn't keep the array of all numbers.
// ν”Όλ³΄λ‚˜μΉ˜ 숫자의 λ¬΄ν•œ μŠ€νŠΈλ¦Όμ„ μƒμ„±ν•©λ‹ˆλ‹€.
// μ œλ„ˆλ ˆμ΄ν„°λŠ” λͺ¨λ“  숫자의 배열을 μœ μ§€ν•˜κ³  μžˆμ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
function* fibonacci(): IterableIterator<number> {
let [a, b] = [0, 1];
Expand All @@ -1078,13 +1077,12 @@ function print(n: number) {
}
}
// Print first 10 Fibonacci numbers.
// ν”Όλ³΄λ‚˜μΉ˜ 숫자의 첫 번째 10개 숫자λ₯Ό 좜λ ₯ν•©λ‹ˆλ‹€.
print(10);
```

There are libraries that allow working with iterables in a similar way as with native arrays, by
chaining methods like `map`, `slice`, `forEach` etc. See [itiriri](https://www.npmjs.com/package/itiriri) for
an example of advanced manipulation with iterables (or [itiriri-async](https://www.npmjs.com/package/itiriri-async) for manipulation of async iterables).
`map`, `slice`, `forEach` λ“±κ³Ό 같은 λ©”μ†Œλ“œλ₯Ό μ—°κ²°ν•¨μœΌλ‘œμ¨ λ„€μ΄ν‹°λΈŒ 배열을 λΉ„μŠ·ν•œ λ°©λ²•μœΌλ‘œ μ΄ν„°λ ˆμ΄λΈ”λ‘œ μž‘μ—…ν•  수 있게 ν•˜λŠ” λΌμ΄λΈŒλŸ¬λ¦¬κ°€ μžˆμŠ΅λ‹ˆλ‹€.
μ΄ν„°λ ˆμ΄λΈ”μ˜ λ°œμ „λœ μ‘°μž‘μ˜ 사둀λ₯Ό μœ„ν•΄ [itiriri](https://www.npmjs.com/package/itiriri)λ₯Ό ν™•μΈν•΄μ£Όμ„Έμš”. (λ˜λŠ” 비동기 μ΄ν„°λ ˆμ΄λΈ”μ˜ μ‘°μž‘μ„ μœ„ν•΄μ„œ [itiriri-async](https://www.npmjs.com/package/itiriri-async)λ₯Ό ν™•μΈν•΄μ£Όμ„Έμš”.)

```ts
import itiriri from 'itiriri';
Expand Down

0 comments on commit 7559d4a

Please sign in to comment.