iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 17
0
Mobile Development

老姐好像要用 Kotlin 寫專案,能撐30天嗎?系列 第 17

改善程式的第十七天:Kotlin Null Safety 別名「消滅 Null Check Conditions 計畫」

儘管身上滿載補班日的疲累,我還是向老姐展示了這幾天的成果。

沒想到老姐露出不贊同的神情,她說:「現在很多人都有社交帳號,也不用特地為了註冊功能綁上 Keycloak ,你沒有時間研究 Keycloak 的程式碼吧?也不知道裡面有沒有不妙的東西。比起裝 Keycloak 我更傾向捨棄註冊功能,而且註冊欄位這麼多,誰都不想填啦!」老姐沒壓住煩躁的情緒,語帶控訴。

「哎呀,後面才是你的真正想法吧。」因為現在每次重開系統,資料都會清空,變成每次都要重新註冊,非常麻煩。不過這讓我想起公司線上產品的活躍使用者,的確使用社交帳號登入的人數遠多於一般註冊。

「還有,我發現你 API 的 bug 囉。」老姐走到我的旁邊,指著我的程式碼。

get("/api/topics/{id}") {
            val topicId = call.parameters["id"]

            val topic = transaction {
                Topic.findById(UUID.fromString(topicId))?.let {
                    TopicDetailResponse(
                            id = it.id.value,
                            name = it.title,
                            avatar = "https://imgur.com/l0swFL1.jpg",
                            description = it.description,
                            tags = it.tags
                    )
                }
            }
            if (topic != null) {
                call.respond(topic)
            } else {
                call.response.status(HttpStatusCode.NotFound)
            }
        }

「如果這邊傳的路徑 id 不符合 UUID 格式會變成 HttpStatusCode 500 錯誤,你忘了處理這種情境吧?」老姐說完,用指節輕敲螢幕上 Topic.findById(UUID.fromString(topicId))?.let { 這行程式碼。

https://ithelp.ithome.com.tw/upload/images/20200926/20129197wxldUAq3ny.png

「喔,確實,我忘了這邊可能發生 Exception ,我來包一下 try-catch 。」

get("/api/topics/{id}") {
            val topicId = try { UUID.fromString(call.parameters["id"]) } catch(e: Exception) { null }
            topicId?.let {
                val topic = transaction {
                    Topic.findById(topicId)?.let {
                        TopicDetailResponse(
                                id = it.id.value,
                                name = it.title,
                                avatar ="https://imgur.com/l0swFL1.jpg",
                                description = it.description,
                                tags = it.tags
                        )
                    }
                }
                topic?.let { call.respond(topic) } ?: call.respond(HttpStatusCode.NotFound)
            } ?: call.respond(HttpStatusCode.NotFound)
        }

老姐端詳了一下新出爐的程式碼,揶揄道:「看起來你也愛上了 ?.let?:return lambda expression ?」

「對呀,看了 https://kotlinlang.org/docs/reference/null-safety.html 官方文件後正在努力習慣, 不寫 if (xxx == null) if (xxx != null) 。」

https://ithelp.ithome.com.tw/upload/images/20200926/20129197DEPoFPWVPS.png

「至於 return lambda expression 是試出來的,企圖在所有程式碼區塊最後一行試著不加 return ,如果 IDE 說不行再補上。」意猶未盡之下,我示意老姐看程式後面幾行,「我還發現 call.respond(HttpStatusCode.NotFound) 效果等價 call.response.status(HttpStatusCode.NotFound) ,所以就用比較短的那個取代之前的寫法了。 」

「善哉!程式碼品質變好,心情也特別愉快。」老姐和我相視一笑,今夜肯定有個好夢。

本次鐵人賽的作品在放進更多內容後已經成書,書名是《老姐要用Kotlin寫專案:從 Server 到 Android APP 的開發生存日記》,歡迎購買唷。https://www.tenlong.com.tw/products/9789864348978


上一篇
補班前夜的第十六天:把 Ktor 綁上 Keycloak 大船
下一篇
陰雨綿綿的第十八天:網站和 API 的不同
系列文
老姐好像要用 Kotlin 寫專案,能撐30天嗎?30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言