๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

study

Kotlin youtube ์š”์•ฝ์ธ๋“ฏํ•œ ๊ฐ์ƒ๋ฌธ

์Šคํ”„๋ง์บ ํ”„ 2018 [TrackB Session2] : ์ฟ ํŒก Kotlin Backend ์ ์šฉ๊ธฐ

์ฝ”ํ‹€๋ฆฐ์˜ ์ข‹์€์ 

  • ์ž๋ฐ”์™€ ํ˜ธํ™˜์„ฑ 100% (@JvmXXX์–ด๋…ธํ…Œ์ด์…˜ ์‚ฌ์šฉ์‹œ)
  • Familiar Syntax
  • Smart Casts(’is,’as’)
    • ํƒ€์ž…์„ ์ค„์—ฌ ์“ธ ์ˆ˜ ์žˆ๋‹ค.
    • is / !is
      • if instanse of
    • as / as?
      • ? ๊ฐ€ ๋ถ™์œผ๋ฉด nullable
  • Data Class
    • copy ๋ผ๋Š” ํ•จ์ˆ˜
      • ์ฝ”ํ‹€๋ฆฐ์—์„œ๋Š” immutable ์ด ๊ธฐ๋ณธ์ด๋‹ค.
      • ํƒ€์ž…๋„ ์ค„๊ณ  ์ฝ๊ธฐ๋„ ํŽธํ•˜๋‹ค.
  • Default Argument
  • String Templates
fun read(b: ByteArray, offset:Int = 0, length:Int = b.size) {
	TODO("Not Imlemented")
}
val s = "abc"
val str = "$s.length is ${s.length}" // abc.length is 3
// no getter setter 
val tomAge = map["tom"] ?: 0 // get
map["jane"] = 29 // set
  • Extension Functions(C#)
    • ์ƒ์†์—†์ด ํ™•์žฅ ๊ฐ€๋Šฅ
  • Null Safety
if (text != null) text.length else -1
test?.length ?: -1
  • Coroutines

๋‚˜์œ์ 

  • No namespace
    • packagename ์ด ์—†๋‹ค → ๊ฐ€์ ธ๋‹ค ์“ธ ๋•Œ ํ—ท๊ฐˆ๋ฆด ์ˆ˜ ์žˆ์Œ
  • No static modifier
    • static ์˜ˆ์•ฝ์–ด๊ฐ€ ์—†๋‹ค
    • companion object ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•จ
  • Final by default
    • Java(Open) ๋ž‘ ๋‹ค๋ฅธ์  ์•ˆ์ •์„ฑ vs ์„ฑ๋Šฅ

Coroutines

  • Readable, clean code
  • Not exclusive, Cooperative
  • Experimental
  • Kotlin Way
suspend fun work(): String {
	delay(200L)
	return "done"
}

fun moreWork(): Deferred<Unit> = async {
	print("Work started")
	val str = work()
	print("Work completed. $str")
}
runBlocking {
	moreWork().await()
}

suspend, async, await ๋งŒ ๋นผ๋ฉด ๋™๊ธฐ๊ฐ€ ๋œ๋‹ค.

๋„์ž… tip

  • ๊ฐœ๋ฐœ์ž ์ˆ˜์ค€ ํŽธ์ฐจ๊ฐ€ ํฌ๋‹ค
  • ๋ณ€ํ™”๋ฅผ ์ถ”๊ตฌํ•˜๋ ค๋ฉด
    • ๊ฐ๊ด€์  ์‹œ๊ฐ
    • History ๋ฐ ํ˜„์‹ค ์ƒํ™ฉ์— ๋Œ€ํ•œ ์ดํ•ด
    • ํ˜„ ์กฐ์ง์˜ ๊ฐœ๋ฐœ ์—ญ๋Ÿ‰ ํ‰๊ฐ€
    • ๋™๊ธฐ๋ถ€์—ฌ - ํ•„์š”์„ฑ ์„ค๋“๋ณด๋‹ค๋Š” ์ž๊ฐํ•  ์ˆ˜ ์žˆ๋„๋ก ์ž๊ทน
    • ์ถฉ๋ถ„ํ•œ ํ•™์Šต์‹œ๊ฐ„
    • ๋ณ€ํ™” ๊ฒฝํ—˜ ๊ณต์œ 
  • Code Quality ์ค‘์š”์„ฑ ์ธ์‹
  • ์„ฑ๊ณต์‚ฌ๋ก€๋Š” ์ž‘์€ ๊ฒƒ ๋ถ€ํ„ฐ
  • ํšจ๊ณผ
    • Safety, Readable code
    • ์„ฑ๋Šฅํ–ฅ์ƒ
      • Latency ๊ฐ์†Œ, High throughput
      • asynchronous/non-blocking ์ ์šฉ ์šฉ์ด
    • ์œ ์ง€๋ณด์ˆ˜์„ฑ ํ–ฅ์ƒ → ์žฅ์•  ๊ฐ์†Œ

Tip

  • ๊ธฐ๋ณธ๊ธฐํ•™์Šต - 5์ผ๋งŒ ํˆฌ์žํ•ด๋ผ, ์ผ๋‹จ ์ฝ”๋”ฉX
  • ์‹ค๋งno Refactoring์€ ํ•„์ˆ˜๋‹ค
  • Coroutines์— ๋Œ€ํ•œ ํ•™์Šต ๋ฐ ํ…Œ์ŠคํŠธ
    • ์–ด์ฐจํ”ผ ๋น„๋™๊ธฐ๋Š” ์–ด๋ ต๋‹ค
    • Coroutines๋Š” ํ•˜๋‚˜์˜ ๋ฐฉ๋ฒ•์ผ ๋ฟ์ด๋‹ค.
๋”๋ณด๊ธฐ

๋ฌด๋ ค 2018๋…„ ์˜์ƒ...์ƒ๊ฐ๋ณด๋‹ค ์˜ˆ์ „๋ถ€ํ„ฐ ์ฝ”ํ”„๋ง์„ ์จ์™”๊ตฌ๋‚˜.. ์ฟ ํŒก๊ฐ™์€ ๋จธ๊ธฐ์—…์—์„œ๋„ ๋น„๋™๊ธฐ ์ฝ”๋“œ๋ฅผ ์ƒ๊ฐ๋ณด๋‹ค ๋งŽ์ด ์“ฐ๋Š”๊ตฌ๋‚˜... ๋ณด๋ฉด์„œ ์—ฌ๋Ÿฌ๋ฒˆ ์ถฉ๊ฒฉ๋ฐ›์•˜๋‹ค๐Ÿซ  +์ž๊ทน

์ด์ „์— Webflux ์“ฐ๋ฉด์„œ Java Reactive API ๋ฅผ ์“ฐ๋Š”๊ฒŒ ์™€๋‹ฟ์ง€(?) ์•Š์•„์„œ ์–ด๋ ค์› ๋˜ ๊ธฐ์–ต์ด ์žˆ์—ˆ๋Š”๋ฐ๐Ÿ˜– ์ฝ”ํ‹€๋ฆฐ ์ฝ”๋ฃจํ‹ด๊ณผ ํ•จ๊ป˜๋ผ๋ฉด..์–ด๋””๋“ ์ง€ ๐Ÿฅน ๊ฐˆ ์ˆ˜ ์žˆ,,,,,,์ž˜ ๋˜๊ฒ„์ง€

์ฝ”ํ‹€๋ฆฐ์˜ k๋„ ์ž˜ ๋ชฐ๋ž๋Š”๋ฐ (์Šคํ„ฐ๋””์—์„œ ์ข…์ข… final, immutable ํ‚ค์›Œ๋“œ๋ฅผ ๋“ฃ๊ธด ํ–ˆ๋‹ค.) ์ฝ”ํ‹€๋ฆฐ์ด ์™œ ์ข‹๊ณ  ์ฝ”๋ฃจํ‹ด์ด ๋ญ”์ง€๋„ ์š”์•ฝ ์ •๋ฆฌํ•ด์„œ ๋“ค์€ ๊ธฐ๋ถ„์ด๋‹ค. ์ด๋ฒˆ์ฃผ ์œ„ํด๋ฆฌ์˜€๋‚˜, ํŒ€ ์ปจ๋ฒค์…˜์— ๋Œ€ํ•ด ๋งค๊ฐœ๋ณ€์ˆ˜์— ๊ผญ final์„ ์จ์ค˜์•ผ ํ•˜๋Š”๊ฐ€์— ๋Œ€ํ•œ ์ž‘์€,,ํ† ๋ก ์ด ์žˆ์—ˆ๋Š”๋ฐ ์ฝ”ํ‹€๋ฆฐ์—์„œ๋Š” final, immutable์ด ๋‹น์—ฐํ•œ๊ฑฐ๋ผ๊ณ  ์•Œ๊ณ  ์žˆ๋‹ค,,,์„ฑ๋Šฅ์ด ํฌ๊ฒŒ ๋ฌธ์ œ๋˜์ง€ ์•Š๋Š” ์„ ์ด๋ผ๋ฉด final ์“ฐ๋Š”๊ฒŒ ์ข‹๊ฒ ๋”ฐ...! ๋ผ๊ณ  ์–˜๊ธฐํ•˜๊ณ  ์‹ถ์—ˆ๋Š”๋ฐ ๊ธฐ์–ต์ด ๊ฐ€๋ฌผ๊ฐ€๋ฌผํ•ด์„œ ๋ง์„ ๋ชปํ–ˆ๋ ,,, ์•„์ˆฉ