-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1228 from eastperson/master
- Loading branch information
Showing
1 changed file
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# Overview | ||
|
||
μ½νλ¦°μμλ checkμ requireλΌλ ν¨μλ₯Ό μ 곡νλ€. **[μ΄νν°λΈ μ½νλ¦° - μμ΄ν 5. μμΈλ₯Ό νμ©ν΄ μ½λμ μ νμ κ±Έμ΄λΌ]** μμ μμΈν λ΄μ©μ΄ κΈ°μ¬λμ΄μλ€. | ||
|
||
> By usingΒ `require`Β andΒ `check`Β we get three things | ||
> | ||
> 1. We are able to validate function arguments and state. | ||
> 2. We are able to throw particular exceptions depending on the case. | ||
> 3. We are able to write idiomatic Kotlin code that is clean and readable. | ||
- `check()`λ μμ μλ κ΅¬λ¬Έμ΄ `false`μΌ λ `IllegalStateException`μΒ λ°μμν¨λ€. | ||
- `require()`λ μμ μλ κ΅¬λ¬Έμ΄ `false`μΌ λ `IllegalArgumentException`μ λ°μμν¨λ€. | ||
- `checkNotNull`, `requireNotNull` λ‘ `null` 체ν¬λ₯Ό ν μ μμΌλ©° μ΄ν `null`μ΄ μλλΌκ³ κ°μ νλ€. λ°λΌμ μ€λ§νΈ μΊμ€νΈλ₯Ό μ¬μ©ν μ μλ€. | ||
|
||
check μμ | ||
|
||
```kotlin | ||
fun viewReview(reviewId: Long) { | ||
val review = reviewRepository.findById(reviewId) | ||
check(review.isPublic()) { "곡κ°λ λ¦¬λ·°κ° μλλλ€." } | ||
review.addViewCount() | ||
return review | ||
} | ||
``` | ||
|
||
require μμ | ||
|
||
```kotlin | ||
fun validateReviewModifyForm(reviewModifyRequest: ReviewModifyForm) { | ||
val review = reviewRepository.findById(reviewModifyRequest.reviewId) | ||
|
||
require(reviewModifyRequest.content.isNotBlank() && reviewModifyRequest.content.length < MAX_CONTENT_LENGTH) { | ||
"리뷰 λ΄μ©μ 곡백μ΄κ±°λ 1000μλ₯Ό μ΄κ³Όνλ©΄ μλ©λλ€." | ||
} | ||
review.modify(content = reviewModifyRequest.content) | ||
|
||
// μλ΅ | ||
} | ||
``` | ||
|
||
# μ΄νν°λΈ μ½νλ¦° μμ΄ν 5 - μμΈλ₯Ό νμ©ν΄ μ½λμ μ νμ κ±Έμ΄λΌ | ||
|
||
μ½λμ λμμ μ νμ κ±°λ λ°©λ² | ||
|
||
- require: argumentλ₯Ό μ νν μ μλ€. | ||
- check: μνμ κ΄λ ¨λ λμμ μ νν μ μλ€. | ||
- assert: μ΄λ€ κ²μ΄ trueμΈμ§ νμΈν μ μλ€. assert ν μ€νΈ λͺ¨λμμλ§ μλνλ€. | ||
- return λλ throwμ ν¨κ» νμ©νλ Elvis μ°μ°μ | ||
|
||
## μ₯μ | ||
|
||
- μ νμ κ±Έλ©΄ λ¬Έμλ₯Ό μ½μ§ μμ κ°λ°μλ λ¬Έμ λ₯Ό νμΈν μ μλ€. | ||
- λ¬Έμ κ° μμ κ²½μ° ν¨μκ° μμνμ§ λͺ»ν λμμ νμ§ μκ³ μμΈλ₯Ό λμ§λ€. μμνμ§ λͺ»ν λμμ νλ κ²μ μμΈλ₯Ό λμ§λ κ²λ³΄λ€ κ΅μ₯ν μννλ©° μνλ₯Ό κ΄λ¦¬νλ κ²μ΄ νλ€λ€. μ΄λ¬ν μ νμΌλ‘ μΈν΄ λ¬Έμ λ₯Ό λμΉμ§ μκ³ μ½λκ° λ μμ μ μΌλ‘ μλνλ€. | ||
- μ½λκ° μ΄λ μ λ μ체μ μΌλ‘ κ²μ¬ν΄μ λ¨μ ν μ€νΈλ₯Ό μ€μΌ μ μλ€. | ||
- μ€λ§νΈ μΊμ€νΈ κΈ°λ₯μ νμ©ν μ μκ² λλ―λ‘ νμ λ³νμ μ κ² ν μ μλ€. | ||
|
||
## Argument | ||
|
||
- μΈμλ‘ λ°μ κ°μ μ ν¨μ±μ κ²μ¬νλ λ‘μ§μΌλ‘λ `require()` ν¨μλ₯Ό μ¬μ©νλ€. μ νμ νμΈνκ³ λ§μ‘±μν€μ§ λͺ»ν κ²½μ° `IllegalArgumentException`μ λ°μμν¨λ€. | ||
- 쑰건μ λ§μ‘±νμ§ λͺ»ν λ λ°μμν€λ―λ‘ μ½λλ₯Ό μ¬μ©νλ μ¬λμ΄ μ μ μλ€. λν μ΄ λ΄μ©μ λ¬Έμμλ ν¬ν¨μ΄ λμ΄μΌ νλ€. | ||
- μμ | ||
- μ«μλ₯Ό μΈμλ‘ λ°μμ κ³μ°ν λ μ«μλ μμ μ μμ¬μΌ ν λ | ||
- μ’νλ₯Ό μΈμλ‘ λ°μμ ν΄λ¬μ€ν°λ₯Ό μ°Ύμ λ λΉμ΄μμ§ μμ μ’ν λͺ©λ‘μ΄ νμν λ | ||
- μ΄λ©μΌ μ£Όμλ₯Ό μ λ ₯λ°μ λ κ°μ΄ μ λ ₯λμ΄μλμ§ μ΄λ©μΌ νμμ΄ μ¬λ°λ₯Έμ§ νμΈν λ | ||
|
||
## μν | ||
|
||
- μ΄λ€ ꡬ체μ μΈ μ‘°κ±΄μ λ§μ‘±ν λλ§ ν¨μλ₯Ό μ¬μ©ν μ μκ² ν΄μΌν λ `check()` ν¨μλ₯Ό μ¬μ©νλ€. | ||
- μ§μ λ μμΈ‘μ λ§μ‘±νμ§ λͺ»ν λ `IllegalStateException`μ `throw` νλ€. μνκ° μ¬λ°λ₯Έμ§ νμΈν λ μ¬μ©νλ€. | ||
- μ¬μ©μκ° κ·μ½μ μ΄κΈ°κ³ μ¬μ©νλ©΄ μ λλ κ³³μμ ν¨μλ₯Ό νΈμΆνκ³ μλ€κ³ μμ¬λ λ νλ€. | ||
- μμ | ||
- μ΄λ€ κ°μ²΄κ° 미리 μ΄κΈ°ν λμ΄μμ΄μΌλ§ μ²λ¦¬λ₯Ό νκ² νκ³ μΆμ ν¨μ | ||
- μ¬μ©μκ° λ‘κ·ΈμΈνμ λλ§ μ²λ¦¬νκ² νκ³ μΆμ ν¨μ | ||
- κ°μ²΄λ₯Ό μ¬μ©ν μ μλ μμ μ μ¬μ©νκ³ μΆμ ν¨μ | ||
|
||
## Assert κ³μ΄ ν¨μ μ¬μ© | ||
|
||
- assert κ³μ΄ ν¨μλ₯Ό μ¬μ©νλ©΄ λͺ¨λ μν©μ λν΄ ν μ€νΈν μ μλ€. | ||
- μ΄ μ‘°κ±΄μ νμ¬ μ½νλ¦°/JVMμμλ§ νμ±νλλ©° -ea JVM μ΅μ μ νμ±νν΄μΌ νμΈν μ μλ€. | ||
- νλ‘λμ νκ²½μλ μμΈκ° λ°μνμ§ μκ³ ν μ€νΈν λλ§ νμ±νν μ μλ€. | ||
- λ§μ½ μ¬κ°ν κ²°κ³Όλ₯Ό μ΄λν μ μλ κ²½μ°μλ checkλ₯Ό μ¬μ©μ΄ μ’λ€. | ||
|
||
## Nullabilityμ μ€λ§νΈ μΊμ€ν | ||
|
||
- requireμ check λΈλ‘μΌλ‘ μ΄λ€ 쑰건μ νμΈν΄μ trueκ° λμλ€λ©΄ ν΄λΉ 쑰건μ μ΄νλ‘λ trueμΌ κ±°λΌκ³ κ°μ νλ€. | ||
- `requireNotNull`, `checkNotNull`μ νμ©ν΄λ μ’λ€. λ λ€ μ€λ§νΈ μΊμ€νΈλ₯Ό μ§μνλ―λ‘ λ³μλ₯Ό βμΈν©(unpack)βνλ μ©λλ‘ νμ©ν μ μλ€. | ||
- nullabilityλ₯Ό λͺ©μ μΌλ‘ Elvis μ°μ°μλ₯Ό νμ©νλ κ²½μ°κ° λ§λ€. | ||
|
||
# Reference | ||
|
||
[check - Kotlin Programming Language](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/check.html) | ||
|
||
[require - Kotlin Programming Language](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/require.html) | ||
|
||
[Using require and check Functions in Kotlin](https://hugomartins.io/essays/2021/02/using-require-and-check-in-kotlin/) | ||
|
||
[μ΄νν°λΈ μ½νλ¦°](https://book.naver.com/bookdb/book_detail.nhn?bid=21424027) |