2. Introducing Android App Development with Kotlin

Benefit 1 Conciseness
ทำให้เขียน code ได้เร็วขึ้น
class – properties , constructor เพื่อ Mx data <- user object ถูกสร้าง
getter , setter
override toString fn – เพื่อ display
override and implement hashCode , equal method
ด้านบนเป็นขั้นตอน java -> เปลี่ยน kotlin
create class , ไม่มี curly braces , parenthesis – ใช้แบบ Mutable variable
ใส่ data หน้า class ให้เป็น data attribute
data class functionality

Android architecture – resource – layouts
– external – APIs
– source code อาจจะเป็น java , kotlin

ใช้ constrain layout ถือว่า ดีสุด
เปรียบเทียบ MainActivity ของ .java vs .kotlin
update welcome message – java มี 2 step process
java – TextView variable ,assign – ใช้ findViewById
จากนั้น .setText
ส่วน kotlin ใช้ ของ Jetpack library
โดยรวม kotlin ลดไป 40% ของlines

Benefit 2 : Improve Null Safety
ต้องการลด number pointer exceptions
ได้มีการสร้าง null reference ซึ่งถือว่าผิดครั้งยิ่งใหญ่มาก~ 2009 , ทำให้เกิด error มาก
ทดสอบ property , constructor , getname , มีทั้ง first , last name, แต่ลองให้เป็น null-> ปรากฎว่าพัง
เพราะ .concat(Last) ต้องเป็น string, แต่พอเป็น null ก็เลยพัง
แต่ถ้าใช้ kotlin มันจะ alarm เตือนให้ตอนเขียน code เลย
แต่ก็มี rare case ที่จะทำ force throw exception

Benefit 3 : Function programming

Discover more from อรรถพรคลินิก ศัลยกรรมตกแต่ง Attaporn Plastic Surgery clinic 阿塔蓬博士,曼谷整形外科医生

Subscribe now to keep reading and get access to the full archive.

Continue reading