Upgrade to PRO for Only $50/Yearโ€”Limited-Time Offer! ๐Ÿ”ฅ

Refactoring to Expressive Kotlin

Refactoring to Expressiveย Kotlin

2024.06.29
kotlinConf'24

DavidKwon7

June 28, 2024
Tweet

More Decks by DavidKwon7

Other Decks in Technology

Transcript

  1. Naming - ์„œ์ˆ ์ ์ธ ์ด๋ฆ„ ์ง“๊ธฐ - ๋ช…ํ™•์„ฑ - ๊ธฐ์กด์— ์•ฝ์†๋œ

    naming ์Šคํƒ€์ผ์— ๋งž์ถ”๊ธฐ - ๋ณ€์ˆ˜๋Š” ํŠน์ •์ ์ธ ๋‹จ์–ด, ํ•จ์ˆ˜๋Š” ๋™์‚ฌ ์„ ํ˜ธ
  2. this this ๊ฐ€ ์“ฐ์ด๋Š” ์ƒํ™ฉ this ์˜๋ฏธ class ๋‚ด๋ถ€ class

    ํ˜„์žฌ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐ ํ™•์žฅ ํ•จ์ˆ˜ ๋‚ด๋ถ€ . ์™ผ์ชฝ์— ์ „๋‹ฌ ๋œ ๋ฆฌ์‹œ๋ฒ„ ํŒŒ๋ผ๋ฏธํ„ฐ ์ฐธ์กฐ
  3. Kotlin - generics - Generics๋Š” ์ฝ”๋“œ์˜ ์žฌ์‚ฌ์šฉ์„ฑ๊ณผ ์œ ์—ฐ์„ฑ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด

    ์‚ฌ์šฉ - Generics๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ , ๋‹ค์–‘ํ•œ ํƒ€์ž…์˜ ๊ฐ์ฒด๋ฅผ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ํƒ€์ž… ์•ˆ์ •์„ฑ์„ ์œ ์ง€ํ•˜๋ฉด์„œ๋„ ์ผ๋ฐ˜ํ™”๋œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
  4. Kotlin - generics ์˜ˆ์ œ2 - Channel private val _viewEvent =

    Channel<Event<Any>>() val viewEvent: ReceiveChannel<Event<Any>> = _viewEvent fun viewEvent(content: Any) { viewModelScope.launch(BaseCoroutine. default) { _viewEvent.send(Event(content)) } }
  5. Kotlin - generics ์˜ˆ์ œ2 - Channel private val _viewEvent =

    Channel<Event<Any>>() val viewEvent: ReceiveChannel<Event<Any>> = _viewEvent fun viewEvent(content: Any) { viewModelScope.launch(BaseCoroutine. default) { _viewEvent.send(Event(content)) } }
  6. Kotlin - generics - ๋ฌด๊ณต๋ณ€์„ฑ : ๊ธฐ๋ณธ Generic. - out

    (๊ณต๋ณ€์„ฑ) : ์ œ๋„ค๋ฆญ ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ ํƒ€์ž…์œผ๋กœ ์‚ฌ์šฉ๋  ๋•Œ ์ง€์ •ํ•˜๋ฉฐ, ํ•˜์œ„ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. - in(๋ฐ˜๊ณต๋ณ€์„ฑ) : ์ œ๋„ค๋ฆญ ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉ๋  ๋•Œ ์ง€์ •ํ•˜๋ฉฐ, ์ƒ์œ„ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  7. Kotlin - generics out (๊ณต๋ณ€์„ฑ) - ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์ƒ์‚ฐ(produce) :

    ๋ฐ˜ํ™˜ํƒ€์ž…์œผ๋กœ ์‚ฌ์šฉ - ํ•˜์œ„ ํƒ€์ž… : ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ํ•˜์œ„ ํƒ€์ž…์œผ๋กœ ์ทจ๊ธ‰ ๊ฐ€๋Šฅ
  8. Kotlin - generics in (๋ฐ˜๊ณต๋ณ€์„ฑ) - ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์†Œ๋น„(consume) :

    ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉ๋  ๋•Œ - ์ƒ์œ„ ํƒ€์ž… : ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์ƒ์œ„ ํƒ€์ž…์œผ๋กœ ์ทจ๊ธ‰ ๊ฐ€๋Šฅ
  9. Scope function ๊ฐ์ฒด์˜ ๋ฒ”์œ„(scope)์—์„œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ฑฐ๋‚˜ ๊ฐ์ฒด๋ฅผ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋Š”

    ํŽธ๋ฆฌํ•œ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค . ์ฃผ๋กœ ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์„ ๋†’์ด๊ณ , ๊ฐ์ฒด ์ง€ํ–ฅ์ ์ธ ์„ค๊ณ„๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  10. apply private fun hikerDataSource(): HikariDataSource { val dataSource = HikariDataSource()

    dataSource.jdcUrl = jdcUrl.toString() dataSource.username = username dataSource.password = password dataSource.validate() return dataSource }
  11. apply private fun hikerDataSource(): HikariDataSource { val dataSource = HikariDataSource().apply

    { dcUrl = jdcUrl.toString() username = username password = password } dataSource.validate() return dataSource }
  12. apply inline fun <T> T.apply(block: T.() -> Unit): T -

    ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ฑฐ๋‚˜ ์„ค์ •ํ•  ๋•Œ ์œ ์šฉ - ๊ฐ์ฒด๋ฅผ ์ธ์ž๋กœ ๋ฐ›๊ณ , ๊ฐ์ฒด ์ž์‹ ์„ ๋ฐ˜ํ™˜
  13. with inline fun <T, R> with (receiver: T, block: T.()

    -> R) : R { return receiver.block() } - ๊ฐ์ฒด ๊ฐ’ ์ ‘๊ทผ ๋ฐ ์ˆ˜์ •ํ•˜๋Š” ๊ฒฝ์šฐ - ๊ฐ์ฒด๋ฅผ ์ธ์ž๋กœ ๋ฐ›๊ณ , ๋ธ”๋ก ๋‚ด์—์„œ ๊ฐ์ฒด ๋ฉค๋ฒ„์— ์ง์ ‘ ์ ‘๊ทผ ๊ฐ€๋Šฅ - this ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ์ ‘๊ทผ ๊ฐ€๋Šฅ
  14. run inline fun <T, R> T.run(block: T.() -> R): R

    { return block() } - ์ง€์—ญ ๋ณ€์ˆ˜์˜ ๋ฒ”์œ„ ์ œํ•œ (isolate a block of code) - ๊ฐ์ฒด์˜ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ฑฐ๋‚˜ ๊ฐ์ฒด ์ดˆ๊ธฐํ™” ์‹œ ์‚ฌ์šฉ - ๋ธ”๋ก ๋‚ด์—์„œ this ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด์— ์ ‘๊ทผ ๊ฐ€๋Šฅ - ๋ธ”๋ก์˜ ๋งˆ์ง€๋ง‰ ํ‘œํ˜„์‹ ๋ฐ˜ํ™˜
  15. also public inline fun <T>.also( block: (T) -> Unit ):

    T - ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์•„๋„ ๋˜๋Š” ๊ฒฝ์šฐ, ๋ถ€๊ฐ€์ ์ธ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ์œ ์šฉ. - ๊ฐ์ฒด๋ฅผ ์ธ์ž๋กœ ๋ฐ›๊ณ , ๋ธ”๋ก ๋‚ด์—์„œ ๊ฐ์ฒด๋ฅผ ์ˆ˜์ • - it ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด์— ์ ‘๊ทผ - ๊ฐ์ฒด ์ž์‹ ์„ ๋ฐ˜ํ™˜, ์ตœ์ข… ๊ฒฐ๊ณผ์— ์˜ํ–ฅ์„ ๋ผ์น˜์ง€ ์•Š์Œ.
  16. let public inline fun <T, R> T.let(block: (T) -> R):

    R - nullable ์ฒ˜๋ฆฌ - ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ - ๋ธ”๋ก ๋‚ด์—์„œ it ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด์— ์ ‘๊ทผ ๊ฐ€๋Šฅ
  17. Scope function Scope function ํŠน์ง• ๋ฐ˜ํ™˜๊ฐ’ let it ์„ ์‚ฌ์šฉํ•ด์„œ

    ๊ฐ์ฒด ์ ‘๊ทผ ๋ธ”๋ก์˜ ๊ฒฐ๊ณผ run this ์„ ์‚ฌ์šฉํ•ด์„œ ๊ฐ์ฒด ์ ‘๊ทผ ๋ธ”๋ก์˜ ๊ฒฐ๊ณผ with -๊ฐ์ฒด๋ฅผ ์ธ์ž๋กœ ๋ฐ›์Œ -this ์—†์ด ๊ฐ์ฒด์˜ ๋ฉค๋ฒ„์— ์ง์ ‘ ์ ‘๊ทผ ๊ฐ€๋Šฅ ๋ธ”๋ก์˜ ๊ฒฐ๊ณผ apply this ์—†์ด ๊ฐ์ฒด์˜ ๋ฉค๋ฒ„์— ์ง์ ‘ ์ ‘๊ทผ ๊ฐ€๋Šฅ ๊ฐ์ฒด ์ž์‹  also it ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด ์ ‘๊ทผ ๊ฐ์ฒด ์ž์‹