Android uses Kotlin for development experience

This year's TIOBE list selects the programming language of the year, and Kotlin has become a hot topic of competition. As one of the main languages ​​for Android development, Android developers should be happy. Although Kotlin is based on Java, we can finally stop confusing it with Java development (however, if it is web development, Kotlin also supports it, it depends on whether you use XD).


Since Google announced Kotlin as Android's development language, I have learned through the official Kotlin documentation, and I have written several articles highlighting some syntaxes that I personally think are more useful. In the later development, Java and Kotlin are used in different projects. Today, I will talk about my feelings when using Kotlin for Android development.


Kotlin can be perfectly integrated with Java, which adds a lot of popularity to Kotlin. Using Kotlin, you can use Java code and class libraries without any configuration. You don't have to worry about the lack of useful third-party class libraries. Java class libraries can be used for You use it; and with the help of Kotlin's class extension, it also provides us with a more convenient interface. For example, using String in Kotlin, we directly have the String.isNullOrEmpty() method without using any third-party class library.


Kotlin provides optional values, if the reference may be null, we have to define the variable type as optional, otherwise we cannot assign null to the variable. This implies that before defining the variable type, we need to seriously consider whether the variable may be null. However, this is not all the advantages of optional values. When we use optional values, we have two symbols !! and ? to use. Using !! means that we are very sure that the value is not null in the place where it is used. If there is a bug in the code, a NullPointerException will be thrown where we are sure it cannot be null; if we use ?, we don't have to use this variable here, we just try to use it, if it's not null, use it, otherwise don't use it. In Java, we have to pin our hopes on the level of programmers. Whether or not to nullify a reference is a conscious behavior of individuals. Kotlin restricts your behavior in terms of syntax, so that you have to go to the optional value when you use it. Thinking of using !! or ?.


Kotlin provides data classes, which I often use to define entity classes, and Kotlin does not limit the existence of only one class definition in a file, we can put related entity classes in one file. Data classes are simpler to define and less expensive to code. In addition, when using EventBus, we need to define our own Event class, which only takes a few lines of Kotlin code.


kotlin-extension provides us with a better way to operate View, we no longer need to write a lot of findViewById, a long time ago, I thought that findViewById is redundant code, but we still have to write it over and over again Go down, now finally free.


Java needs to consider a lot of issues to make a single instance mode, but Kotlin is different, you only need to use object to replace class to define a safe single instance.


The above are the advantages that I deeply experienced when using Kotlin, but I have to admit that Kotlin also has some shortcomings, at least some inconveniences I found in use. Let's talk about it below.


First of all, the code written by Kotlin feels loose, probably because Kotlin has more grammatical and semantic meanings. To put it bluntly, what Kotlin can do with one line of code may require many lines of Java code. This may not be regarded as a disadvantage in the strict sense, but when we are looking for bugs, there may be some obstacles. A statement does too many things. We need to think about the stage when the bug is executed to this statement. , which may require us to adjust parameters or use a finer-grained syntax.


Second, Kotlin does not enforce checked exceptions. I think this is Kotlin's retrogression compared to java. Personally, it is even a misinterpretation of the contents of the Effective Java book by the author of Kotlin? There are also no related articles on the Internet. You can go and have a look. If you know C#, you should know that C# does not force catching exceptions. There are also many discussions about C# and Java in this regard. Below is my opinion. Effective Java suggests that we should pay attention to catching exceptions in Java, and the way to capture them should be appropriate, do not ignore them casually, and if an exception is thrown, we must handle it appropriately. That is to say, throwing an exception needs to be checked, and we don't take all exceptions for granted and then print them. However, using Kotlin will not force the exception to be caught, and I have fallen into the pit more than once, especially when using some platform-related resources, the APP behaves differently on mobile phones of different manufacturers. In these places, the SDK often throws exceptions. If it is Java code, if it is forced to catch exceptions, we will notice that there may be pits here, but with Kotlin, we have no awareness of checking for pits. Not all APIs throw exceptions, and it is impossible for us to check the API documentation for all called APIs to determine whether exceptions are thrown. As a result, we can't test whether all models of phones on the market work well. Therefore, we use Kotlin to pay special attention to some exception capture issues.


In general, the advantages of Kotlin are still more than the disadvantages. It can be said that the disadvantages do not hide the advantages. In the future development, there is a chance that everyone can try to use it. There may also be different views and understandings. Since Google has set it as a first-level development language, it must be justified. We should try to adapt and embrace new things and keep up with Google's pace.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325581133&siteId=291194637