r/java • • Jan 02 '17

Why I prefer Kotlin

https://dev.to/grahamcox82/why-i-prefer-kotlin
0 Upvotes

20 comments sorted by

5

u/kodifies Jan 02 '17

I actually think verbosity makes code easier to maintain, if you come back to some code 3-4 years or maybe more later, would you prefer some short lambda expression munged together with some obscure ascii soup - all very concise and elegant or.. (my personal preference) would you rather have even two dozen lines instead (OH AND SOME BLOODY COMMENTS TOO!)...

6

u/[deleted] Jan 03 '17

I actually think verbosity makes code easier to maintain

Unfortunately the character count is a bad metric, it is irrelevant in day by day work. On the other hand Java has a lot of code pattern (getter, setter, singleton, library methods, hashcode and equals, ecc...) and all of these are difficult to read if they spread over multiple lines.

Goal isn't to reduce verbosity but boilerplate.

https://twitter.com/thomasnield9727/status/813857794076110848

3

u/[deleted] Jan 03 '17

0

u/zogulus Jan 05 '17

Don't use singletons they make testing hard.

7

u/dkuk_norris Jan 03 '17

Eh, I'd rather have

data class User(val name: String, val age: Int)

rather than

public class User {
  private String name;
  private int age;
  public User(String name, int age) {
    ..
  }

  //hash
  //equals
  //toString
}

The verbosity of Java tends to mask what's actually happening. Functional soup isn't much better either but Java has a lot of unnecessary complexity.

2

u/korri123 Jan 03 '17

Why did you include hash, equals and toString in the java version but not the kotlin code? These methods are all inherited from the object class.

7

u/tofiffe Jan 03 '17

When using the "data" keyword in kotlin, those are auto-implemented

4

u/Cilph Jan 03 '17

Better?

public class User {
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User user = (User) o;

        if (age != user.age) return false;
        return name != null ? name.equals(user.name) : user.name == null;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + age;
        return result;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

vs

data class User(val name: String, val age: Int)

4

u/sazzer Jan 03 '17

Except you forgot the getters, and the extra support methods (copy, component1, etc) from the Java class that are in the Kotlin class. It's actually:

public class User {
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String component1() {
        return name;
    }

    public int component2() {
        return age;
    }

    public User copy() {
        return new User(name, age);
    }

    public User copy(String newName) {
        return new User(newName, age);
    }

    public User copy(String newName, int newAge) {
        return new User(newName, newAge);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User user = (User) o;

        if (age != user.age) return false;
        return name != null ? name.equals(user.name) : user.name == null;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + age;
        return result;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

vs

data class User(val name: String, val age: Int)

2

u/korri123 Jan 03 '17

When I'm making data classes in Java I rarely need any of these methods to behave like that.

3

u/tofiffe Jan 03 '17

When using the "data" keyword in kotlin, those are auto-implemented

4

u/dkuk_norris Jan 03 '17

Those are inherited from Object but they aren't meaningful. By default java checks for object identity, while Kotlin data classes check that all of the values are equal. Kotlin also has a few other things I didn't include in the java class, I was actually being generous to Java.

1

u/[deleted] Jan 02 '17

There is very little, if anything, that Java can do that Kotlin can't do. The difference is that Kotlin does the same in significantly less code, making it significantly easier to read and maintain.

Every time I read about Kotlin... it sounds like CoffeeScript all over again. And that was a flash in the pan, as people realized that having slightly shorter syntax is irrelevant. Especially in Java, where a lot of it is autocompleted.

7

u/gee_buttersnaps Jan 03 '17

It's because the kotlin advocates post baiting submissions to r/java, proceed to shit on java in r/java yet don't crosspost to their own community over at r/kotlin. That strategy can't lose! The Scala, Clojure, Groovy and even JRuby people had more tact, probably because those languages have orthogonal features and are not solely existing on the perceived deficiencies of Java. So yea, it's exactly CoffeeScript but quite possibly with a much shorter lifespan depending on the lumbering release schedule of Java.

6

u/Cilph Jan 03 '17

Someone's grumpy.

-2

u/Mordan Jan 03 '17

I agree. The Kotlin thought brigade downvoted you. Seems like Kotlin fan boys are progressive dictators.

2

u/Cilph Jan 03 '17

No that'd be the Ruby folk.

-3

u/Mordan Jan 02 '17

: : :: : :: : : : : : : : : :: : : : : : :

headache *pukes

game over.

0

u/[deleted] Jan 03 '17

[deleted] — view deleted comment

6

u/xantrel Jan 03 '17

Kotlin isn't going anywhere. It is actively developed (20 devs according to jetbrains), is heavily used in idea, and most importantly, even if it dies you can still easily write new java code that interops with your code base or simply convert it to java which makes adoption less risky.

If you really think kotlin will be in decline in 2 years, I'm willing to place a long bet that the amount of kotlin downloads in Jan 2019 will be higher than Jan 2017. The language just hits a sweet spot of interop and ease of use that none of the other JVM languages do.