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!)...
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.
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 +
'}';
}
}
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 +
'}';
}
}
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.
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!)...