SmartNews 運用 Kotlin 將程式碼行數降低 20%,並提升團隊道德量
透過集合功能整理內容
你可以依據偏好儲存及分類內容。

SmartNews 分享各種新聞來源的即時新聞,協助數百萬人發現自己的世界。該公司於 2012 年在日本創立,目前全球下載量已超過 5,000 萬次。
2019 年 6 月,該團隊發現 Android 開發作業會先改為 Kotlin,並決定自行測試 Kotlin。他們想要利用以 Kotlin 優先的 Jetpack 程式庫,減少必須維護的程式碼量,同時受益於 Kotlin 淺顯易懂的語法。
具體措施
SmartNews 的團隊很熟悉 Java,因此很容易開始以 Kotlin 編寫。由於 Kotlin 與 Java 100% 的互通性,因此可以在現有的程式碼集內,輕鬆使用 Kotlin 編寫新功能,例如 Weather Radar and News From All Sides。他們使用協同程式,管理專利天氣雷達功能中的圖片下載和快取。協同程式調度工具可以為管理工作提供實用的抽象化機制,而這也協助 SmartNews 工程師擺脫管理原始執行緒的陷阱。
該團隊也重構部分 Java 程式碼,並對 Kotlin 的空值安全功能留下印象。Kotlin 用來識別可變動性、是否可為空值的語法,以及有助於團隊及早發現錯誤,將程式碼變更的檢查時間縮短了 10%。使用 Kotlin 簡單且有效率的語法,他們也提高了程式碼集的可讀性,隨著公司持續成長,程式碼集也會更易於維護。
成果
使用 Kotlin 編寫程式碼已從實作提升到推出服務的整體工作效率。他們發現,使用 Kotlin 編寫時,最大的改善就是將轉換的程式碼行減少 20%。約半數的 SmartNews 應用程式目前採用 Kotlin,開發團隊則打算編寫 Kotlin 中的所有新功能,以及重構部分現有的程式碼,以便持續提升程式碼可維護性。
當開發團隊實作 Kotlin 並減少樣板程式碼時,他們發現團隊士氣增加。能夠更有效率地表達想法,並讓程式碼在日後更容易理解。SmartNews 工程經理 sho Ohashi 發現 Kotlin 協助推動工程招募工作。「候選者最常詢問的問題是,您正在使用 Kotlin 嗎?您使用的頻率為何?好像現在有許多工程師都有意遷移至 Kotlin,並且想提供支援。」雖然團隊士氣和招募方面的改善並非他們團隊採用 Kotlin 的主要原因,但這些正向改變將對公司在競爭激烈的產業中持續成長茁壯。
開始使用
進一步瞭解如何使用 Kotlin 開發 Android 應用程式。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2021-01-06 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2021-01-06 (世界標準時間)。"],[],[],null,["# SmartNews reduces lines of code by 20% and improves team morale with Kotlin\n\n[SmartNews](https://play.google.com/store/apps/details?id=jp.gocro.smartnews.android)\nhelps millions of people discover their world everyday by sharing timely news\nfrom a diverse set of news sources. The company was founded in 2012 in Japan,\nand it now has over 50 million downloads globally.\n\nIn June 2019, the team saw Android development moving towards Kotlin first and\ndecided to start testing Kotlin themselves. They wanted to take advantage of\nKotlin-first Jetpack libraries, reduce the amount of code they had to maintain,\nand benefit from Kotlin's expressive and easy-to-understand syntax.\n\nWhat they did\n-------------\n\nThe team at SmartNews is well versed in Java, so it was fairly easy for them to\nstart writing in Kotlin. As Kotlin is 100% interoperable with Java, they could\neasily start writing new features such as\n[Weather Radar and News From All Sides](https://about.smartnews.com/en/2020/09/17/20200917/)\nin Kotlin, while working in their existing codebase. They used coroutines to\nmanage image downloading and caching within the patented Weather Radar feature.\nCoroutine dispatchers provide a useful abstraction for managing tasks, and this\nhelped the SmartNews engineers avoid the pitfalls that come from managing raw\nthreads.\n\nThe team also refactored some of their Java code, and was impressed with\nKotlin's null safety features. Kotlin's syntax for identifying mutability,\nnullability, and initialization helped the team catch errors early on,\nand **reduced the amount of time to review code changes by 10%**.\nUsing Kotlin's succinct and efficient syntax, they were also able to increase\nthe readability of their codebase, which has made their code easier to maintain\nas the company continues to grow.\n\nResults\n-------\n\nWriting in Kotlin has **improved their overall productivity from\nimplementation to launch** . The biggest improvement they saw was that\nwriting in Kotlin **reduced their converted lines of code by 20%**.\nApproximately half of the SmartNews app is currently in Kotlin, and the\ndevelopment team plans on writing all new features in Kotlin as well as\nrefactoring some of their existing code in order to continue increasing their\ncode maintainability.\n\nAs the development team was implementing Kotlin and reducing boilerplate code,\nthey noticed an **increase in team morale** . The team was excited\nto be able to express their ideas in a more efficient way, and have their code\nbe more readable for the future. Hideo Ohashi, the Engineering Manager for\nSmartNews noticed **Kotlin has helped their engineering recruiting\nefforts** . *\"The most frequent questions from candidates are 'are you\nusing Kotlin? How often do you use it?' It seems now that many engineers are\ninterested in migrating to Kotlin and want to support it.\"* While these\nimprovements in team morale and recruitment were not the main reasons the team\nadopted Kotlin, these positive changes will help the company as it continues to\ngrow in this competitive industry.\n\nGet started\n-----------\n\nLearn more about [developing an Android app with Kotlin](/kotlin)."]]