当前位置:网站首页>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 .
边栏推荐
- [July 5 event preview] Flink Summit
- Solution to the binary tree problem of niuke.com
- Databinding+LiveData轻松实现无重启换肤
- How many times can the WordPress user name be changed? Attach the method of changing user name
- 子线程更新UI全解
- VS code更新后不在原来位置
- Black cat takes you to learn EMMC Protocol Part 24: detailed explanation of EMMC bus test program (cmd19 & cmd14)
- Introduction to border border attribute
- 机器学习实战-决策树-22
- How to view win11 system and reserved space?
猜你喜欢

Phpstudy steps to quickly build a website (teach you to build it by hand)

Machine learning practice - neural network-21

Summary: golang's ide:vscode usage
![[basic teaching of Bi design] detailed explanation of OLED screen use - single chip microcomputer Internet of things](/img/76/820d4e357206f936b33da92a5e2b5b.png)
[basic teaching of Bi design] detailed explanation of OLED screen use - single chip microcomputer Internet of things

Problem solving during copilot trial

机器学习实战-神经网络-21

BiliBili Yang Zhou: above efficiency, efficient delivery

归并排序

Unity installs the device simulator

Analysis of Andriod low on memory printing principle
随机推荐
Leetcode 1518. wine change
Science heavyweight: AI design protein has made another breakthrough, and it can design specific functional proteins
STM32 loopback structure receives and processes serial port data
Code layered management of interface testing based on RF framework
Solution to using json.tojsonstring to display question marks in Chinese in Servlet
Sliding Window
.NET的求复杂类型集合的差集、交集、并集
Rolling update strategy of deployment.
Leetcode94. Middle order traversal of binary trees
[pictures and texts] detailed tutorial of one click reinstallation of win11 system
QT signal and slot mechanism (detailed)
Redefinition problem of defining int i variable in C for loop
How to open the power saving mode of win11 system computer
2020-12-13
Brother bird talks about cloud native security best practices
How to view win11 system and reserved space?
Jetpack 全家桶之 LiveData 使用及源码篇
云原生—运行时环境
Fundamentals of machine learning - principal component analysis pca-16
机器学习基础-决策树-12