头图

Kotlin is known for its concise features, and in our practice, more concise means more efficient. In fact, as many as 67% of professional Android developers who use Kotlin say that Kotlin has helped them increase their productivity. In the following content, I will share some of the ways that Kotlin helps our partner engineers increase their productivity, and introduce you to Kotlin features that help this.

Among professional Android developers who use Kotlin, as many as 67% say that Kotlin has helped them increase their productivity.

Concise, simple and efficient

The simplicity of Kotlin has an impact on all stages of development:

  • as the code author: You can focus on the problem that needs to be solved (not the syntax). Less code means less testing, less debugging, and fewer opportunities to write bugs.
  • as a reviewer and maintainer: is less code you need to read, making it easier to understand the role of the code, and therefore easier to review and maintain the code.

The following example comes from Flipkart's team:

"In an internal survey, 50% of developers mentioned that for modules written in Kotlin, the estimated time required to complete the function will be reduced."

——Flipkart

Kotlin features and productivity

Because of Kotlin's simplicity and high readability, most of Kotlin's features can improve productivity. Let's look at some of the most commonly used functions below.

Default parameters and builder

In the Java programming language, when some of the parameters in your constructor are optional, you usually use one of the following two methods:

When using Kotlin, you do not need to use these two methods due to the existence of the default parameter function. The default parameters allow you to implement function overloading without additional boilerplate code.

's use of Kotlin allows the Cash App team to eliminate many builders, thereby reducing the amount of code they need to write. In some cases, the amount of code has been reduced by as much as 25%.

For example, the following code is an Task of a 0609b518ace97f object using the builder and default parameters. The only required parameter for this Task is the name of the 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
+)

You can learn more about the default parameters in Kotlin Vocabulary | Kotlin Default Parameters

object keyword and singleton

singleton mode probably one of the most commonly used design patterns for software developers, it can help us create a single instance of an object, and other objects can access and share the instance.

When creating a singleton, you need to control how the object is created, to ensure that there is only one instance, and to ensure the thread safety of the code. In Kotlin, you only need to use one keyword: 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++
+     }
+ }

operators, string templates and more

The conciseness and simplicity of the Kotlin language is also reflected in the operator overloads , deconstruction and string templates. These features make the code very easy to read.

For example, suppose we have a library and some books. Then the operation of removing books from the library and processing and printing book titles can be written as follows:

/* Copyright 2020 Google LLC.  
   SPDX-License-Identifier: Apache-2.0 */

fun borrow(){
    library -= book
    val (title, author) = book
    println("Borrowed $title")
}

The Kotlin functions used here are:

summary

Kotlin makes it easy to read and write code. It has built- singleton and delegate , which can help us remove code that may cause bugs or increase maintenance costs. And like 1609b518acee40 string template , lambda expression , extension function , operator overload can make the code more concise and clear. Writing less code means less reading of the code. It also means less code to maintain and fewer errors, which leads to higher productivity.

You can read Use Kotlin to create a better App to learn more, and you can also read learning use cases to understand how developers benefit from Kotlin. If you want to take the first step in using Kotlin ( one of the most popular languages in the world, ), please check our Getting Started page.


Android开发者
404 声望2k 粉丝

Android 最新开发技术更新,包括 Kotlin、Android Studio、Jetpack 和 Android 最新系统技术特性分享。更多内容,请关注 官方 Android 开发者文档。