2014-10-27

Using Kotlin for DTOs instead of Java


Creating DTOs in Java, you often end up with very verbose code, similar to the following Address data class:








public class Address
{
private String contactName;
private String companyName;
private String street;
private String streetNumber;
private String streetAppendix;
private String zipCode;
private String city;
private String country;
private String state;
private String contactPhone; 
public String getContactName()
{
return contactName;
}
public void setContactName(final String contactName)
{
this.contactName = contactName;
        // 90 lines of getter / setter code has been omitted here! 
}
Studies have shown that there is a correlation between code size and erroneous code. Hence reducing code size results in concise code which improves readability and understanding of the underlying solution idioms. The following Kotlin data class with merely 11 lines of code represents exactly the same semantics as the 112 lines of Java code. Additionally it defines default values for all properties which is optional.
data class Address (
        var contactName: String = "",
        var companyName: String = "",
        var street: String = "",
        var streetNumber: String = "",
        var streetAppendix: String = "",
        var zipCode: String = "",
        var city: String = "",
        var country: String = "",
        var state: String = "",
        var contactPhone: String = ""
)
From within Java code this Kotlin class can be instantiated and modified using its setter methods in exactly the same way as it would have been written as the Java class outlined above. In fact Kotlin code is 100% (and not only to some degree as some other JVM languages) interoperable with Java code. Unlike some other JVM languages, Kotlin is just like Java also type-safe but uses inference to a far greater extent than Java so you do not have to explicitly specify a type which can be inferred by the compiler.

Additionally the keyword "data" overrides the "toString" method in a way that the result is a human-friendly representation of the DTO with all fields being shown instead of an object's hash code. So whenever you use something like
LOG.info(myDto.toString()) 
you get something like
Vote(referencePoll=election, answerOptionIndex=2)
written inside your logs, instead of something like
com.corp.restkit.model.ballot.Vote@541756ff

Overall Kotlin can be used as a concise DSL for declaring DTOs that not only peacefully coexist with existing (JVM language) code but are also fully interoperable with Java.

As web apps (stateless,  JavaScript based browser clients) are very popular these days but JavaScript as a language for writing huge enterprise-grade applications can become a great challenge, Kotlin can be translated to JavaScript code (comparable to Google's "GWT" toolkit approach). So DTOs that have been written for RESTful micro services can be reused in client code without any adjustment whatsoever.


References: