TIL

아이템 8 적절하게 null을 처리하라

null을 안전하게 처리하기

val printerName1 = printer?.name ?: "Unnamed"
val printerName2 = printer?.name ?: return
val printerName3 = printer?.name ?: throw Error("Printer must be name")

오류 throw 하기

not-null assertion(!!)과 관련된 문제

의미 없는 nullability 피하기

lateinit 프로퍼티와 notNull 델리게이트

class UserControllerTest {
	private lateinit var dao: UserDao
	private lateinit var controller: UserController

	@BeforeEach
	fun init() {
		dao = mockk()
		controller = UserController(dao)
	}
}
class DoctorActivity: Activity() {
	private val doctorId: Int by Delegates.notNull()

	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		doctorId = intent.extras.getInt(DOCTOR_ID_ARG)
	}
}
class DoctorActivity: Activity() {
	private val doctorId: Int by arg(DOCTOR_ID_ARG)
}