当前位置:网站首页>Android engineers, how to use kotlin to provide productivity?
Android engineers, how to use kotlin to provide productivity?
2022-07-28 13:02:00 【Serbian uncle】
Kotlin Known for its simplicity , And in our practice , Being more concise means being more efficient . in fact , In the use of Kotlin Major of Android Among developers , There are as many as 67% Person representation Kotlin Has helped them improve their productivity . In the following content , I will share some Kotlin Ways to help our partner engineers improve productivity , And I'd like to introduce you to Kotlin function .
In the use of Kotlin Major of Android Among developers , There are as many as 67% Person representation Kotlin Has helped them improve their productivity
concise 、 Simple and efficient
Kotlin Simplicity has an impact on every stage of development :
- As a code writer : You can focus on the problems that need to be solved ( Not grammar ). Less code means less testing 、 Less debugging and less writing Bug The opportunity of .
- As a reviewer and maintainer : There's less code you need to read , So it's easier to understand what code does , So it's easier to review and maintain the code .
The following example comes from Flipkart The team :
“ In an internal investigation ,50% The developers mentioned , For the use of Kotlin Write modules , It is estimated that the time required to complete the function will be reduced .”
——Flipkart
Kotlin The function and productivity of
because Kotlin Simplicity and high readability of , majority Kotlin All of these functions can improve productivity . Let's look at some of the most common features .
Default parameters and builders
stay Java In programming language , When some of the parameters in your constructor are optional , You usually use one of two methods :
- Add multiple constructors ;
- Realization Builder mode .
In the use of Kotlin when , Due to the existence of default parameter function , You don't need to use both methods . Default parameters allow you to overload functions without additional template code .
Yes Kotlin The use of Cash App Teams can clean up a lot of builders , This reduces the amount of code they need to write . In some cases , The amount of code is reduced 25% As much as .
for instance , The following code is a Task Objects are implemented using the builder and default parameters, respectively . The Task The only required parameter is the task name (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 go through our article Kotlin Vocabulary | Kotlin Default parameters Learn more about default parameters .
object Keywords and singletons
The singleton pattern It's probably one of the most common design patterns used by software developers , It can help us create a single instance of an object , Other objects can access and share the instance .
When creating a singleton , You need to control how objects are created , Ensure that there is only one instance and that the code is thread safe . And in the Kotlin in , 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++
+ }
+ }
The operator 、 String templates and more
Kotlin The simplicity and simplicity of language , It's also reflected in operators overloading 、 deconstruction And string template and other functions . These features make the code very readable .
for instance , Suppose we have a library and some books . So the operation of removing books from the library and processing and printing book titles , It 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")
}
What we use here Kotlin Functions include :
-=The implementation uses operators overloading ;val (title, author) = bookUsed deconstruction ;println ("Borrowed $title")Using a string template .
summary
Kotlin Make it easy to read and write code , It's built in with things like Single case and entrust The realization of this kind of pattern , This can help us remove those that might lead to bug Or code that increases maintenance costs . And like String template 、lambda expression 、 spread function 、 operators overloading This kind of function can make the code more concise and clear . Less code means less code to read , It also means less code to maintain 、 And fewer mistakes , To bring higher productivity .
You can read Use Kotlin Create better App To learn more about , You can also learn how developers learn from Kotlin Benefit from . If you want to use Kotlin ( One of the most popular languages in the world ) The first step , Please refer to our Getting started page .
边栏推荐
- Change the document type in endnode and import it in word
- LeetCode 移除元素&移动零
- 机器学习基础-支持向量机 SVM-17
- SQL most commonly used basic operation syntax
- 机器学习基础-决策树-12
- [graduation design teaching] ultrasonic ranging system based on single chip microcomputer - Internet of things embedded stm32
- 苏黎世联邦理工学院 | 具有可变形注意Transformer 的基于参考的图像超分辨率(ECCV2022))
- 2020-12-27
- Which big model is better? Openbmb releases bmlist to give you the answer!
- Fundamentals of machine learning Bayesian analysis-14
猜你喜欢

How to use databricks for data analysis on tidb cloud | tidb cloud User Guide

单调栈Monotonic Stack

Leetcode 1518. wine change

Code layered management of interface testing based on RF framework

2020-12-07

Flexpro software: measurement data analysis in production, research and development

Brief introduction to JS operator

DART 三维辐射传输模型申请及下载
![[pictures and texts] detailed tutorial of one click reinstallation of win11 system](/img/cc/749fe4095fc5afb1fc2c65df43d06c.png)
[pictures and texts] detailed tutorial of one click reinstallation of win11 system

Analysis of Andriod low on memory printing principle
随机推荐
BA autoboot plug-in of uniapp application boot
【嵌入式C基础】第6篇:超详细的常用的输入输出函数讲解
BiliBili Yang Zhou: above efficiency, efficient delivery
Leetcode:704 binary search
Sliding Window
如何在 TiDB Cloud 上使用 Databricks 进行数据分析 | TiDB Cloud 使用指南
[graduation design] smart home system based on ZigBee - single chip microcomputer Internet of things stm32
[error prone points of C language] Part 4: detailed rules for storing structures in memory
Linear classifier (ccf20200901)
LeetCode394 字符串解码
CCF201912-2 回收站选址
Detailed explanation of the usage of C # static
The largest rectangle in leetcode84 histogram
【嵌入式C基础】第3篇:常量和变量
Xampp Chinese tutorial guide
Datanode data block missing problem finding
Science heavyweight: AI design protein has made another breakthrough, and it can design specific functional proteins
Redefinition problem of defining int i variable in C for loop
机器学习实战-神经网络-21
【嵌入式C基础】第5篇:原码/反码/补码