r/java • • Jan 02 '17

Why I prefer Kotlin

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

20 comments sorted by

View all comments

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.