当前位置:网站首页>Android工程师,如何使用Kotlin提供生产力?
Android工程师,如何使用Kotlin提供生产力?
2022-07-28 11:53:00 【塞尔维亚大叔】
Kotlin 以其简洁的特性而闻名,而在我们的实践中,更加简洁就意味着更加高效。事实上,在使用 Kotlin 的专业 Android 开发者中,有多达 67% 的人表示 Kotlin 已经帮助他们提升了生产力。在接下来的内容中,我会分享一些 Kotlin 帮助我们的合作伙伴工程师们提高生产力的方式,并为您介绍有助于此的 Kotlin 功能。
在使用 Kotlin 的专业 Android 开发者中,有多达 67% 的人表示 Kotlin 已经帮助他们提升了生产力
简洁、简单且高效
Kotlin 的简洁性对开发的各个阶段都有影响:
- 作为代码作者: 您可以专注于需要解决的问题 (而不是语法)。更少的代码意味着更少地测试、更少地调试以及更少写出 Bug 的机会。
- 作为审阅和维护者: 您需要阅读的代码变少了,从而更容易理解代码的作用,也因此更容易审阅和维护代码。
以下例子来自 Flipkart 的团队:
“在一次内部调查中,50% 的开发人员提到,对于使用 Kotlin 编写的模块,预估完成功能所需的时间会有所减少。”
——Flipkart
Kotlin 的功能与生产力
由于 Kotlin 的简洁与高可读性,大多数 Kotlin 的功能都可以提高生产力。下面让我们来看一些最常用的功能。
默认参数与构建器
在 Java 编程语言中,当您的构造函数中的某些参数是可选参数时,您通常会采用下面两种方法之一:
- 添加多个构造函数;
- 实现 构建器模式。
在使用 Kotlin 时,由于默认参数功能的存在,您无需使用这两种方法。默认参数使您无需额外的样板代码便能实现函数重载。
对 Kotlin 的使用使得 Cash App 团队可以清除诸多构建器,从而减少了他们需要编写的代码量。在某些情况下,代码量被减少了 25% 之多。
举个例子,下面的代码是一个 Task 对象分别使用构建器及默认参数的实现方式。该 Task 唯一的必需参数是任务名 (name):
/* Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
3
- public class Task {
- private final String name;
- private final Date deadline;
- private final TaskPriority priority;
- private final boolean completed;
-
- private Task(String name, Date deadline, TaskPriority priority, boolean completed) {
- this.name = name;
- this.deadline = deadline;
- this.priority = priority;
- this.completed = completed;
- }
-
- public static class Builder {
- private final String name;
- private Date deadline;
- private TaskPriority priority;
- private boolean completed;
-
- public Builder(String name) {
- this.name = name;
- }
-
- public Builder setDeadline(Date deadline) {
- this.deadline = deadline;
- return this;
- }
-
- public Builder setPriority(TaskPriority priority) {
- this.priority = priority;
- return this;
- }
-
- public Builder setCompleted(boolean completed) {
- this.completed = completed;
- return this;
- }
-
- public Task build() {
- return new Task(name, deadline, priority, completed);
- }
- }
-}
+ data class Task(
+ val name: String,
+ val deadline: Date = DEFAULT_DEADLINE,
+ val priority: TaskPriority = TaskPriority.LOW,
+ val completed: Boolean = false
+)
您可以通过我们的这篇 Kotlin Vocabulary | Kotlin 默认参数 了解有关默认参数的更多信息。
object 关键字与单例
单例模式 大概是软件开发者最常用的设计模式之一,它可以帮助我们创建一个对象的单个实例,而其他对象可以访问和共享该实例。
创建单例时,您需要控制对象是如何被创建的,保证只存在一个实例并确保代码的线程安全。而在 Kotlin 中,您只需使用一个关键字: object。
/* Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
- public class Singleton{
- private static volatile Singleton INSTANCE;
- private Singleton(){}
- public static Singleton getInstance(){
- if (INSTANCE == null) { // Single Checked
- synchronized (Singleton.class) {
- if (INSTANCE == null) { // Double checked
- INSTANCE = new Singleton();
- }
- }
- }
- return INSTANCE;
- }
- private int count = 0;
- public int count(){ return count++; }
- }
+ object Singleton {
+ private var count = 0
+ fun count(): Int {
+ return count++
+ }
+ }
操作符、字符串模板及更多
Kotlin 语言简洁与简单的特性,同样体现在 操作符重载、解构 与字符串模板等功能中。这些功能使得代码变得十分易读。
举个例子,假设我们有一个图书馆和一些书。那么从图书馆移除书籍并处理和打印书籍标题的操作,便可编写为下面这样:
/* Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
fun borrow(){
library -= book
val (title, author) = book
println("Borrowed $title")
}
这里用到的 Kotlin 功能有:
总结
Kotlin 使得阅读和编写代码变得简单,它内建了诸如 单例 和 委托 这类模式的实现,从而可以帮助我们移除那些可能导致 bug 或增加维护成本的代码。而像 字符串模板、lambda 表达式、扩展函数、操作符重载 这类功能则可以使代码更加简洁明了。代码编写得更少意味着代码的阅读量变小,同时也意味着需要维护的代码更少、错误也更少,从而带来更高的生产力。
您可以阅读 使用 Kotlin 创建更好的 App 来了解更多,也可以通过阅读学习用例来了解开发者们是如何从 Kotlin 中获益的。如果您要迈出使用 Kotlin (世界上最受欢迎的语言之一) 的第一步,请查阅我们的 使用入门 页面。
边栏推荐
- Machine learning practice - logistic regression-19
- Installation and reinstallation of win11 system graphic version tutorial
- mysql limit 分页优化
- The openatom openharmony sub forum was successfully held, and ecological and industrial development entered a new journey
- How can non-standard automation equipment enterprises do well in product quality management with the help of ERP system?
- STM32F103 几个特殊引脚做普通io使用注意事项以及备份寄存器丢失数据问题1,2
- Leetcode 42. rainwater connection
- Brother bird talks about cloud native security best practices
- C# static的用法详解
- SQL most commonly used basic operation syntax
猜你喜欢

05 pyechars basic chart (example code + effect diagram)

Leetcode94. Middle order traversal of binary trees

一台电脑上 多个项目公用一个 公私钥对拉取gerrit服务器代码

FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys) will be depreca

How to open the power saving mode of win11 system computer

leetcode 1518. 换酒问题

Science heavyweight: AI design protein has made another breakthrough, and it can design specific functional proteins

Vs code is not in its original position after being updated

How to add PDF virtual printer in win11

Fundamentals of machine learning - principal component analysis pca-16
随机推荐
Zurich Federal Institute of technology | reference based image super resolution with deformable attention transformer (eccv2022))
Leetcode: array
合并表格行---三层for循环遍历数据
Summary: golang's ide:vscode usage
[embedded explanation] key scanning based on finite state machine and stm32
非标自动化设备企业如何借助ERP系统,做好产品质量管理?
Siemens docking Leuze BPS_ 304i notes
QT signal and slot mechanism (detailed)
Unity installs the device simulator
Communication example between upper computer and Mitsubishi fn2x
01 introduction to pyechars features, version and installation
04 pyechars 地理图表(示例代码+效果图)
C# static的用法详解
Change the document type in endnode and import it in word
Quick read in
Vs code is not in its original position after being updated
01 pyechars 特性、版本、安装介绍
机器学习基础-主成分分析PCA-16
XIII Actual combat - the role of common dependence
Qt 信号和槽机制( 详解 )