Skip to Content
자바에서 코틀린으로
book

자바에서 코틀린으로

by 덩컨 맥그레거, 냇 프라이스, 오현석
November 2022
Beginner to intermediate
544 pages
12h 39m
Korean
Hanbit Media, Inc.
Content preview from 자바에서 코틀린으로
74
자바에서 코틀린으로
4.3
코틀린다운 코드로 리팩터링하기
옵셔널에서 널이 될 수 있는 타입으로 마이그레이션하는 과정에서 본 것처럼, 이 예제의 모든
코드가 코틀린으로 변경됐다. 여기서 멈출 수도 있지만, 변환 후에도 조금 더 리팩터링을 진행
했던 이 책의 정책을 따라서 이렇게 변경한 코드에서 무엇을 더 배울 수 있는지 더 살펴보자.
다음은 현재 버전의
Legs
이다.
예제
4.12
[nullability.9:src/main/java/travelator/Legs.kt]
object Legs {
fun longestLegOver(
legs: List<Leg>,
duration: Duration
): Leg? {
var result: Leg? = null
for (leg in legs) {
if (isLongerThan(leg, duration))
if (result == null ||
isLongerThan(leg, result.plannedDuration))
result = leg
}
return result
}
private fun isLongerThan(leg: Leg, duration: Duration): Boolean {
return leg.plannedDuration.compareTo(duration) > 0
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

고성능 파이썬(2판)

고성능 파이썬(2판)

오현석, 미샤 고렐릭, 이안 오스발트
러닝 타입스크립트

러닝 타입스크립트

조시 골드버그
실리콘밸리 리더십

실리콘밸리 리더십

김정혜, 마이클 롭

Publisher Resources

ISBN: 9791169210447