📜 ⬆️ ⬇️

Are you still Java code? It's time to change

image

For a long time I did not want to look towards Kotlin. But when I was scrolling through the list of vacancies, I began to notice more and more often the column: Knowledge of Kotlin would also be an advantage . And one evening I saw this article in Habré: 8 educational projects . I liked the cryptocurrency tracker there. But somehow it was too simple: an ordinary Get-request is not at all interesting. And here I decided to try to write all this on Kotlin. The cryptocurrency tracker became a training ground for studying Kotlin. After creating this attachment, several important lessons were learned.

Anyone interested is asking for cat.

Briefly about me


When I started doing all this, I had a lot of preconceptions about this language. That he is not comfortable, that he is not handsome, that he is bad and slowed down. It was all because I didn’t want to leave my comfort zone. I know Java is good, and in general, why change anything, let everything be as it is, why strain. But interest took its toll. And I can say that I was very wrong.
')
What is so special about Kotlin that is not in Java?

1. Lack of semicolons


It caught my eye from the very beginning. Their absence seemed just a nice bonus, although the hand still twitched :). But the presence / absence of such trifles is not a reason to love / hate the language (although when you have thousands of lines of code it’s not a trifle).

2. Binding views


As soon as I finished rivet layouts there was a question about how to bind a view. I got to google: "Butterknife kotlin". That's when I swam. It turned out that using ButterKnife in Kotlin simply makes no sense. You just need to take and use views, while not having the slightest risk of getting NPE.

If you write code in Java, we’ll get it
public class MainActivity { private TextView textView; //1-  @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.textView);//2-  textView.setText("Hello world!");//3-  } } 


And now we will write with ButterKnife
 public class MainActivity...{ @BindView(R.id.textView)TextView textView; //1-  @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this);//2-  textView.setText("Hello world!");//3-  } } 


And now Kotlin
 class MainActivity: AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) textView.text = "Hello world!"//1-    } } 


3. Lack of NPE


NPE is a legend in the programming world. And in Kotlin, it is simply not there before compiling possible NullPointerException locations are considered a syntax error. You are simply forced to check for null.

4. The code is much shorter


According to statistics, the code in Kotlin is 40% shorter, and this is easily seen. In order to parse the answer, it was necessary to make models. I never thought that a model with so many fields could be so short.

Beautiful and short model in Kotlin
 data class ResponseItem(@SerializedName("id") @Expose var id: String?, @SerializedName("name") @Expose var name: String?, @SerializedName("symbol") @Expose var symbol: String?, @SerializedName("rank") @Expose var rank: String?, @SerializedName("price_usd") @Expose var priceUsd: String?, @SerializedName("price_btc") @Expose var priceBtc: String?, @SerializedName("24h_volume_usd") @Expose var _24hVolumeUsd: String?, @SerializedName("market_cap_usd") @Expose var marketCapUsd: String?, @SerializedName("available_supply") @Expose var availableSupply: String?, @SerializedName("total_supply") @Expose var totalSupply: String?, @SerializedName("max_supply") @Expose var maxSupply: String?, @SerializedName("percent_change_1h") @Expose var percentChange1h: String?, @SerializedName("percent_change_24h") @Expose var percentChange24h: String?, @SerializedName("percent_change_7d") @Expose var percentChange7d: String?, @SerializedName("last_updated") @Expose var lastUpdated: String?) 

In fact, the code is shorter, because 2 annotations (expose and serialized) in one line.

The same class in Java. Gently sea the code!
 @SerializedName("id") @Expose private String id; @SerializedName("name") @Expose private String name; @SerializedName("symbol") @Expose private String symbol; @SerializedName("rank") @Expose private String rank; @SerializedName("price_usd") @Expose private String priceUsd; @SerializedName("price_btc") @Expose private String priceBtc; @SerializedName("24h_volume_usd") @Expose private String _24hVolumeUsd; @SerializedName("market_cap_usd") @Expose private String marketCapUsd; @SerializedName("available_supply") @Expose private String availableSupply; @SerializedName("total_supply") @Expose private String totalSupply; @SerializedName("max_supply") @Expose private Object maxSupply; @SerializedName("percent_change_1h") @Expose private String percentChange1h; @SerializedName("percent_change_24h") @Expose private String percentChange24h; @SerializedName("percent_change_7d") @Expose private String percentChange7d; @SerializedName("last_updated") @Expose private String lastUpdated; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSymbol() { return symbol; } public void setSymbol(String symbol) { this.symbol = symbol; } public String getRank() { return rank; } public void setRank(String rank) { this.rank = rank; } public String getPriceUsd() { return priceUsd; } public void setPriceUsd(String priceUsd) { this.priceUsd = priceUsd; } public String getPriceBtc() { return priceBtc; } public void setPriceBtc(String priceBtc) { this.priceBtc = priceBtc; } public String get24hVolumeUsd() { return _24hVolumeUsd; } public void set24hVolumeUsd(String _24hVolumeUsd) { this._24hVolumeUsd = _24hVolumeUsd; } public String getMarketCapUsd() { return marketCapUsd; } public void setMarketCapUsd(String marketCapUsd) { this.marketCapUsd = marketCapUsd; } public String getAvailableSupply() { return availableSupply; } public void setAvailableSupply(String availableSupply) { this.availableSupply = availableSupply; } public String getTotalSupply() { return totalSupply; } public void setTotalSupply(String totalSupply) { this.totalSupply = totalSupply; } public Object getMaxSupply() { return maxSupply; } public void setMaxSupply(Object maxSupply) { this.maxSupply = maxSupply; } public String getPercentChange1h() { return percentChange1h; } public void setPercentChange1h(String percentChange1h) { this.percentChange1h = percentChange1h; } public String getPercentChange24h() { return percentChange24h; } public void setPercentChange24h(String percentChange24h) { this.percentChange24h = percentChange24h; } public String getPercentChange7d() { return percentChange7d; } public void setPercentChange7d(String percentChange7d) { this.percentChange7d = percentChange7d; } public String getLastUpdated() { return lastUpdated; } public void setLastUpdated(String lastUpdated) { this.lastUpdated = lastUpdated; } 


Well, you are not tired of flipping?

I think the difference in the face. You say: well, these are models, it happens to them. When I wrote the adapter for RecylerView, it was only 50 lines long. The same adapter on Java would occupy 80 - 90 lines.

5. No getters and setters


This is one of the main reasons why models in Kotlin are so short. In order to change the value of a variable, it is enough just to do:

 someModel.name = "new name" 

In Java, for the same result, you need to declare the setters, and only then contact them. And one setter takes 3 lines of code. And as you saw in the previous example, this difference is significant.

Conclusion


In fact, Kotlin has a lot more advantages, I wrote only about those that are clearly striking, you can read more about Kotlin chips in the documentation of the language. In principle, I liked it and next time I will be using it in a real project. Despite my pre-emptive opinion, he showed his best side. The code is much simpler and shorter than on Java. Now Kotlin is a trend and at the same time he is rapidly gaining momentum. Two years ago, he was considered damp and less than 5 percent of all android workers used it. With such a language in the baggage you will have advantages at the interviews, during the development, and also Cotlin compiles JavaScript code in bytes, which can be useful in one day when changing the profile.

The most important lesson I learned today was: you should not be afraid to try something new, perhaps it is even better than the old one.

Source: https://habr.com/ru/post/344540/


All Articles