Slashdot Mirror


Google-backed Kotlin Gains Adoption in Open Source Android Apps; Scientists Say It Has Improved Code Quality (theregister.co.uk)

Kotlin, which Google blessed last year as an alternative to Java for programming Android apps, has already made its way into almost 12 per cent of open source Android apps, and in so doing has elevated their code quality. From a report: So we're told by computer scientists Bruno Gois Mateus and Matias Martinez, affiliated with University of Valenciennes in France, who observed that Google at the end of 2017 said Kotlin had infiltrated more than 17 per cent of Android apps developed with its IDE, Android Studio 3.0. Kotlin is an open source statically typed programing language that targets the JVM, Android, JavaScript (transpiling to ES5.1) and native platforms (via LLVM). JetBrains, the company that created it, contends Kotlin is more concise and more type-safe than Java. It estimates that apps written in Kotlin require about 40 per cent less code than they would with Java. With fewer lines of code, in theory, one can expect fewer bugs. In a paper distributed through pre-print service ArXiv, "An Empirical Study on Quality of Android Applications written in Kotlin language," Mateus and Martinez describe how they gathered 925 apps from the open source F-Droid repository, measured the amount of Kotlin code in each, and analyzed the code for "smells" as an indicator of code quality.

4 of 86 comments (clear)

  1. It depends on how you get fewer by MikeRT · · Score: 4, Informative

    In Java, it's common to check if a list is null before iterating over it avoid a NPE. In Groovy we can do this:

    someList?.each { item -> something(item) }

    The null-safe operator ? tells the Groovy compiler to write that null check for me and quietly disregard the call to "each" if it fails.

    I see colleagues use Map.get and then either forget to check for null values or add an extra 1-2 lines of code. In Groovy we can do:

    def x = myMap[someString] ?: DEFAULT_VAL

    Another simple example, but the "elvis operator," ?: means "if the conditional statement to my left is truthy, return its value and assign to x otherwise use the value to my right."

    These are simplistic examples, but in Java they happen all the time.

    1. Re:It depends on how you get fewer by Anonymous Coward · · Score: 4, Informative

      As an interesting aside, replace def with val and both of your examples are valid Kotlin syntax.

    2. Re:It depends on how you get fewer by AuMatar · · Score: 4, Insightful

      Yeah, nobody writes Java write that. Its unreadable and unmaintainable. You write a freaking if statement and a loop. That way people can understand what you're trying to do, and can easily edit it if what the loop needs to do changes. You example would be immediately rejected in any code review.

      --
      I still have more fans than freaks. WTF is wrong with you people?
  2. Checked Exceptions. by NextApp · · Score: 5, Interesting

    Android app dev here. Learning Kotlin, I started off being thoroughly excited about it, especially the nullity checking and other general code safety features. Then I got to the bit about it not having checked exceptions and now have a very mixed opinion.

    The arguments against checked exceptions seem to be that 1.) most languages don't have them, 2.) bad programmers will just throw and catch the base "Exception" class everywhere, and 3.) bad programmers will screw up interfaces and the like by putting implementation-specific exceptions in the contract rather than writing appropriate exception classes.

    Bad programmers write bad code, and will continue to write bad code. I guess if we "fix" the problem by having good programmers write bad code too, it will technically make the bad programmers average.

    Going to try using it on a new project (that won't have checked exceptions as a key component of its design, like my existing stuff) and hope for the best. I still can't get over this seeming like two steps forward, three steps back. I hope I'm wrong, as it otherwise appears to be a great improvement compared to Java.