Google IO 2017 has announced that Kotlin will become the official Android development language. All of a sudden, friends circle and Android circle were flooded. Of course, I took the opportunity to post an article called “Why I Switched to Kotlin,” which generated a lot of buzz (though it was scorned). The Android community is so feral right now that to ignore Kotlin would make you look like an Android programmer.

This article tries to look at this matter from an objective and comprehensive point of view, and tries to provide a more rational point of view for everyone’s reference.

Why Kotlin

I think there are two reasons why Google chose Kotlin.

  1. To gradually get rid of the patent troll Oracle. The switch to OpenJDK last year and the current support for Kotlin as an official language is partly an attempt to get rid of Oracle, which extorts astronomical compensation for nine lines of code.

  2. Kotlin deserves it, and it deserves it. Kotlin has certainly won over a number of companies and developers overseas for its effectiveness and efficiency. Jake At Square, for example, has been pushing Kotlin. Kotlin has at least 2 years of practice in a production environment abroad (non-JetBrains internal practice application). In mobile development, Android developers are always lucky compared to iOS developers because we have a lot of great tools available (Android Studio, etc.), and the choice of Kotlin is consistent with Google’s approach to providing developers with efficient development tools.

What does it mean to be the official Android development language

  • Official: Tool support (Android Studio 3.0 comes with Kotlin), official publicity (instructional videos, keynote speeches, etc.)
  • For the community, Kotlin versions of libraries and frameworks have sprung up
  • For Java, once with the help of Android this spring breeze, really let it resplendent, now and in the future in the Field of Android can be said to be mutually assured, Java in the Android development language market share will decline. But this is not necessarily a bad thing, there is competition to make better progress.
  • For Android developers, we have a new language option for Android development, which can be used by those who are interested in Kotlin because of the previously uncertain future. Having Kotlin means development efficiency should improve.
  • For teams, this often brings up a choice. Use Kotlin or not, That’s a question. There are always people on the team who want to try Kotlin, and others who are not so keen. Because of the historical baggage and interest of the team members, a long-term coexistence of Kotlin and Java was a practical solution for existing projects. New projects should encourage the use of Kotlin, but it needs to be combined with the capabilities of the team and other factors.

What is Kotlin’s appeal

Kotlin has many features, such as simplicity, security and utility, high development efficiency and improved readability, and better functional programming support.

1. Simplicity. Kotlin’s code is indeed more concise than Java’s, such as type inference, leaving out trailing semicolons, and so on, but that’s far from the reason we switched to Kotlin.

2. Security, which is an important feature of Kotlin. Kotlin is null-pointer safe, and JetBrains did a clever thing by bringing run-time null-pointer detection forward to compile time, mainly by adding Any? With this nullable type, the null pointer of our program will be significantly improved by using Kotlin.

3. Practical and efficient. Kotlin’s practical aspects are as follows

  • The introduction of Object facilitates us to better apply the singleton pattern
  • The introduction of the Data class eliminates the need for hand-written getters/setters /toString methods
  • The introduction of parameter defaults and named parameters eliminates unnecessary method overloading
  • Support for extension methods allows us to save a lot of necessary code

4.Kotlin introduced Lambda, Streams APIS, and functional programming support.

  • Lambda expressions can save us a lot of code to create anonymous inner classes. (Note that since Kotlin is currently based on JVM6, Lambda expressions are still translated into inner class form at the bytecode stage.)
  • The Streams API combines Lambda expressions and method references to let our code process an event as a description rather than as a command implementation.
  • Kotlin supports OOP(object-oriented programming) and FP(functional programming). The language itself has no limits, giving us freedom of choice. Kotlin’s friendly support for FP makes it easy to write more stable, easily tested, and side-effect free methods and code

5. Readability Objectively, Kotlin’s syntax and features make code more descriptive. But it must be pointed out that code readability mainly depends on the coding quality and ability of the writer.

For me, higher-order functions and method extensions are really useful. Method extensions give me a sense of creativity that Java’s Util methods can’t match.

Whether Kotlin will replace Java

It’s hard to say, because no good thing in this world will necessarily be widely used. A thing is promoted by a group of people, but the resistance often comes from a group of people, just not the same people.

From a personal subjective point of view, I would prefer to see this happen. Not because I like Kotlin better, but because I like to see things improve and get better, so EVEN if Kotlin is replaced by a better language someday, I would welcome it.

Who should be the first to use Kotlin

Kotlin works on multiple platforms and is not limited to learners. Anyone who is willing to learn can acquire the language.

However, considering that Kotlin resources in China are not rich enough, and the Internet is not smooth enough, many people turn into melon eaters to watch.

However, for a project or team, there is always a need for the first person to move forward. And the process has not been smooth, including

  • First, you need to know Kotlin well enough
  • You need to convince the team, and during this time you will receive many challenges, and sometimes you will get frustrated and angry
  • Do you need to provide a range of information or sharing, such as how to configure, Sample Code, Troubleshooting, etc.
  • Sometimes you even work part-time as a mentor, which means your other work needs to be interrupted

Kotlin’s advance is partly based on Life is Great and Everything Will Be Ok, Christina Lee Kotlin is Here (Google I/O ’17)

As good as Kotlin is, it’s not easy to push Kotlin into the project, because it’s normal for something new, like the Meiji Restoration, it looks bright and successful, but it’s not a smooth process of change, first of all, there was a bloody downcast movement, To the southwest War, which broke out in the sixth year of the Meiji era and marked the end of the samurai era, it took decades to achieve success.

So in terms of who should be the first to use Kotlin, I think there are a few things you need to know

  • It is important that Java technology and fundamentals are good
  • English is good, because most of Kotlin’s materials are in English, of course, English is also recommended
  • Willing to take charge of project team, patient, challenging and responsible

Some concerns about the Kotlin project application

Some concerns about Kotlin’s application in the project come to mind. These concerns are not comprehensive at present, but put forward, I hope you can avoid and improve.

1. The written code is not Kotlin style. To solve this problem, we need to study and think more

2. Abuse of extension methods. Kotlin’s extension method is very good. We can extend many methods to make up for some imperfections in the Framework, but we need to be careful when extending. The specific needs of the following two points

  • Choose the most abstract class addition method within a reasonable range. For example, if we want to add a longToast to an Activity, we should consider whether it would be better to put it in a more abstract Context
  • We also don’t want to add methods that are not relevant to the current class for convenience, such as adding an extension method that displays a simple dialog for each Context, which obviously doesn’t make sense because it’s problematic for non-UI contexts.

What are the core strengths of Android programmers

When Kotlin came out, he heard two different sounds:

  • Great, finally there’s a reason to switch to Kotlin and write code more efficiently. It’s time to start giving up on Java.
  • WTF, I have to learn a new language, I feel so tired, I wonder if Kotlin will pass the interview.

These two different voices raise questions about the core competence of Android programmers. So what is the core competency of an Android programmer?

Android programmers are no different than any other programmer or even any other profession, and I think that competitiveness comes in the ability to solve problems. This ability depends heavily on our accurate understanding and solid foundation of problems and technologies.

Programming languages are tools in nature, and good tools can bring better results, but how to use them well to maximize efficiency and quality depends more on the ability of developers.

The selection of good tools, more emphasis on consolidating the foundation and strengthening the ability to understand the essence of things, I think this can make our competitiveness stronger.

In summary, Kotlin is a much better tool, and its absence doesn’t affect our day-to-day Android development. However, I recommend that developers and teams try the language and take advantage of the small luck that is so close at hand.

Additional words

Things are getting out of hand, and the Kotlin promoters and the Kotlin pessimists are diverging, or more accurately, the issue is on the verge of turning from a case to a case.

When Kotlin became the official language of Android, there were a lot of people who saw Kotlin as a speculative favorite, as well as some who didn’t like the idea that Kotlin was being promoted. As a result, the discussion tends to move from the matter to the person. The point I’m trying to make is that it’s normal for speculators to get hot and get criticized, just like businessmen chasing profits and capitalists grabbing surplus value, but we can’t let the discussion get away from the essence of the problem. We need to get back to it.

A lot of people say that Kotlin is just a lot of grammatical sugar, not a breakthrough

Yes, Kotlin has lots and lots of grammatical sugar. It is necessary to briefly popularize the concept of grammatical sugar (from Wikipedia below)

In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language “sweeter” for human use: things can be expressed more clearly, more concisely, or in an alternative style that some may prefer.

By definition, the purpose of syntactic sugar is to make code simpler and more readable.

Another reason for Kotlin to use so much syntactic sugar, besides being concise and readable, is that the class files Kotlin compiled were targeted to JVM 6 (a tradeoff based on JVM 6). For example, we used Lambda in Kotlin, It is impossible to compile into an Invokedynamic instruction because that would make it unrecognizable on JVM6, so it is often translated into an inner class.

What does it matter if you use syntactic sugar? Its purpose is good, because it really introduces you to the amount of code a developer has.

Kotlin is a practical language, that’s in its DNA, it’s not an academic language, and its goal is to lighten the burden on developers. It works well for Android because most Android programmers do engineering.

What’s wrong with being jVM-based

Kotlin is a JVM-based programming language, but there’s nothing wrong with a JVM-based programming language. (J) The emergence of VM is nothing more than a result of balance. Some abstraction is provided at the expense of some execution efficiency, speeding up developer efficiency. This tradeoff is actually more favorable to the human side, which is the trend and goal of programming language development

Kotlin has a long way to go in China. Therefore, there is a need for real practice. People who step on the pit start to go on board and produce more articles and shares that can really promote Kotlin’s application.