SmartNews 使用 Kotlin 将代码行缩减了 20% 并提升了团队士气
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。

SmartNews 通过分享来自各种新闻媒体的及时新闻,帮助数百万人每天探索世界。该公司于 2012 年在日本成立,目前其应用在全球的下载量已超过 5000 万次。
2019 年 6 月,SmartNews 团队注意到 Android 开发正在转向采用“Kotlin 优先”方法,并决定开始自行测试 Kotlin。他们想充分利用 Kotlin 优先的 Jetpack 库,减少必须维护的代码量,并从 Kotlin 更具表达性且易于理解的语法中获益。
策略
SmartNews 团队精通 Java,因此开始使用 Kotlin 编写代码对他们来说很容易。由于 Kotlin 可完全与 Java 互操作,因此他们可以在使用现有代码库的同时,轻松地开始使用 Kotlin 编写新功能,例如 Weather Radar 和 News From All Sides。他们使用协程来管理已获专利的 Weather Radar 功能中的图片下载和缓存。协程调度程序提供了一个实用的抽象函数来管理任务,这可帮助 SmartNews 工程师避免管理原生线程时出现的陷阱。
该团队还重构了部分 Java 代码,Kotlin 的 null 安全功能给他们留下了深刻的印象。Kotlin 用于识别可变性、是否可为 null 和初始化的语法帮助该团队尽早发现了错误,并将审核代码变更的时间缩短了 10%。利用 Kotlin 简洁高效的语法,他们还能够增强其代码库的可读性,从而在公司不断发展壮大的同时让其代码更易于维护。
结果
采用 Kotlin 编写代码后,该团队在从实现到发布的整个流程中的工作效率得到了提升。采用 Kotlin 编写代码后,该团队将转换后的代码行缩减了 20%,这是他们看到的一项最重大的改进。目前,SmartNews 应用中有一半的功能是采用 Kotlin 编写的,开发团队计划用 Kotlin 编写所有新功能,并重构部分现有代码以便继续提高其代码的可维护性。
开发团队在实现 Kotlin 并减少样板代码的过程中发现,团队士气得到了很大提升。团队成员都非常兴奋,因为他们能够更高效地表达自己的想法,同时提高了代码的可读性,更便于日后阅读。SmartNews 的工程经理 Hideo Ohashi 注意到,Kotlin 对工程师招募工作大有裨益。“应聘者最常提问的问题是‘贵公司使用 Kotlin 吗?使用频率是怎样的?’现在,似乎很多工程师都有意迁移到 Kotlin,并希望提供这方面的支持。”尽管团队士气的提高和在招募方面的改进并不是该团队采用 Kotlin 的主要原因,但这些积极的改变将帮助该公司在这个竞争激烈的行业中不断发展壮大。
开始使用
详细了解使用 Kotlin 开发 Android 应用。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2021-01-06。
[null,null,["最后更新时间 (UTC):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)."]]