Java to Kotlin conversion

Asked

Viewed 1,398 times

4

One of the qualities cited about the Kotlin language is the creation of clean code, that is, code that is pleasant to read and that can accomplish its goal without curling.

From what I saw, Android Studio contains translation for Kotlin, even manages to translate the whole project. I would like to know how this issue with the plugin translation? It is advisable to project translation or it would be right to rewrite it?

2 answers

7


The programmer creates clean code. In all languages most programmers do atrocities with code because most refuse to learn real programming.

Kotlin helps to have more concise code compared to Java, nothing more. In fact the code can even be considered verbose compared to some other languages. And that’s not necessarily bad.

I have not seen anything that makes a good comparison between the codes before and after the conversion, but my experience with this type of converter is that if the semantics is not exactly the same between the languages the conversion never gets very good.

Convert C# to VB.NET and vice versa is good "80 or 90%", IE, then need to take a fancy in hand. But these languages were created almost by the same people, and it was supposed to be practically a different syntax with the same language. It is not 100% because VB.NET descends from VB that had some bad things and there is total compatibility with C#.

Kotlin has important semantic differences in relation to Java, as well as several similarities. My estimate is that it will be a "60, 70%" good.

Note that you can convert 100% (not everything), only the quality is that it will not stay 100%. I think you will have gains, but not so much when doing in hand. It can be an initial step to go through the whole code. Several things cannot be converted as well as a human would. It can help to learn, but it can help to unlearn because it will not be a good code.

In VB.NET and C# the library is the same. Kotlin brings a good part of the own library that works better and little or none of this will be converted. So depending on the case may have an even lower quality than it might be.

I think if you are going to program in Kotlin you have to embark on everything, you should avoid doing any part of the Java way, which is what will happen with the conversion. But then it’s everyone’s opinion.

Are creating a Swift to Kotlin converter, which should yield worse results.

6

The recommendable of language Kotlin, for many, is that you start to rewrite your project instead of translating it. To be more direct, there are some advantages between rewriting and translating a code into another language.

It is true that the Ides Android Studio and Inteliij IDEA with the language plugin Kotlin, offer code conversion tools as well as on the language website itself you can do this: Try.kotlinlang.org. What we should be aware of in relation to this is:

  • The conversion of code from Java for Kotlin works in almost every case, or I would say in every.
  • Code conversion allows you to save time and you don’t have to worry about rewriting all the code from-Scratch.
  • Code conversion is sometimes a simple conversion, meaning it won’t really show the power of the language.

What I mean, in a nutshell, is that converting code saves you time, but also makes you miss the chance to know a little more of the language Kotlin. It’s a different language, it’s not like Java and therefore you should stay tuned to some details.

Many people recommend that you rewrite your code for the following reason:

  • You will begin to become familiar with the language
  • It’s the best way to learn the language
  • You’ll be able to write clean, optimized code with the advantages that language gives you
  • You will relearn to program with Kotlin

Yes, in my personal experience, I have learned to like programming/programming and have a certain love for language. It’s a new language, but with huge support from the community and developers.

Another important thing, before you want to convert your project to Kotlin, analyze if this is worth it. What I mean is, the conversion will take time, because converting a project to another language like Kotlin is something very important. Your project will probably get cleaner, but that’s up to you. Only using the conversion tools will not make your project "kotlinized". Look at the language documentation, look at projects that use the language and see how they use it. It’s a language multi-paradigm and you can enjoy a lot of it.

You can also modularize your project, that is, go creating small parts of them in Kotlin (new functions) or go rewriting small part of them in Kotlin, thus allowing the maintenance of old code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.