Kotlin Language Features Related to Null Handling

Any software engineer with a Java background would find the null handling features in the Kotlin language interesting. Let's summarize this topic with some examples. Nullable types: In Kotlin, types are non-nullable by default. If you want a variable to be able to hold a null value, you need to explicitly declare its type as nullable using the Type? syntax. For example, String? denotes a nullable string, while String represents a non-nullable string. Safe calls (?.): Kotlin introduces the safe call operator (?.) for handling nullable types. It allows you to safely invoke a method or access a property on a nullable object. If the object is null, the expression returns null instead of throwing a NullPointerException. Example: data class Person(val name: String, val age: Int, val address: String?) fun main() {     // Create a person with a nullable address     val person1 = Person("John Doe", 25, "123 Main Street")     val person2 = Person("Jane Doe", 30,...

Fluent Interface Example in an Enterprise Integration Project

While working on an enterprise integration project about virtual bank payments, I utilized a simple fluent interface approach. The purpose of the code is creating strongly typed objects from a generic SMO (ServiceMessageObject) instance.

Note: In the context of IBM integration technologies, ServiceMessageObject represents the “message”. In our example, it represents a Web Service request.

Old Code:

if (paymentInfo.getDataObject("totalAmount") != null) {
totalAmount = paymentInfo.getDataObject("totalAmount");

DataObject totalAmount2 = totalAmount.getDataObject("totalAmount");
if (totalAmount2.getBigDecimal("amount") != null) {
parameters.setAmount(totalAmount2.getBigDecimal("amount"));
}

String currencyCode = totalAmount2.getString("currencyCode");
parameters.setCurrencyCode(currencyCode);

}
Byte numberOfInstallments = paymentInfo.getByte("numberOfInstallments");
if (numberOfInstallments != null) {
parameters.setNumberOfInstallments(numberOfInstallments);
}

if (transactionInfo.size() != 0) {
if (((DataObject) transactionInfo.get(0)).getString("orderID") != null) {
parameters.setOrderId(((DataObject) transactionInfo.get(0))
.getString("orderID"));
if (((DataObject) transactionInfo.get(0))
.getString("transactionID") != null) {
parameters.setTransactionID(((DataObject) transactionInfo
.get(0)).getString("transactionID"));
}
parameters.setBankRefId(((DataObject) transactionInfo.get(0)).getString("bankReferenceID"));
//...more


It is really painful to read and modify this code. But actually, what it does is really simple. It should look simple.

New Code:

parameters.setCreditCardNumber(body
.getDataObject("paymentInfo")
.getDataObject("creditCardInfo")
.getString("creditCardNumber"));

parameters.setCreditCardExpiryMonth(body
.getDataObject("paymentInfo")
.getDataObject("creditCardInfo")
.getByte("creditCardExpiryMonth"));

parameters.setAmount(body
.getDataObject("paymentInfo")
.getDataObject("totalAmount")
.getDataObject("totalAmount")
.getBigDecimal("amount"));

parameters.setTransactionID(body
.getListItem("transactionInfo")
.getString("transactionID"));

This is a simple example of an internal DSL. The point I want to stress is this: “Use internal DSLs whenever you need better readability, among other things.”
This simple refactoring helped us a lot to read/modify the code easier than before.

To implement the DSL, what I did is hiding some details in a class that I named DataObjectWrapper. This class contains IBM's DataObject:

public class DataObjectWrapper {

private DataObject dataObject;
public DataObjectWrapper getListItem(String string) {

List list = dataObject.getList(string);

if (list.size() != 0)
return new DataObjectWrapper((DataObject)list.get(0));

return new NullDataObject();
}

public DataObjectWrapper getDataObject(String field) {

if (dataObject.getDataObject(field) != null)
return new DataObjectWrapper(dataObject.getDataObject(field));

return new NullDataObject();
}

public String getString(String field) {
return dataObject.getString(field);
}

public Byte getByte(String field) {
return dataObject.getByte(field);
}
}


To support fluent statements, NullDataObject is an important abstraction:

public class NullDataObject extends DataObjectWrapper {
@Override
public DataObjectWrapper getDataObject(String string) {
return new NullDataObject();
}

@Override
public String getString(String field) {
return null;
}
// ...
}

Comments

Popular posts from this blog

Trie Data Structure and Finding Patterns in a Collection of Words

Virtual Memory

NOTES ON COMPUTER ARCHITECTURE: Some important concepts in computer architecture