Mastering Kotlin Conventions: Plus | Minus | CompareTo | Inc | Dec | Times | Div : Part I

Gopinath Langote
InsideN26
Published in
4 min readDec 5, 2018

--

Kotlin Conventions

Kotlin is famous for its features like high order functions or the power to use functions as first class citizens, extension functions and its conventions to make code easier to understand.

Along with other general programming language conventions like naming classes, functions, Kotlin has its different flavor called Kotlin Conventions.

What is Kotlin Conventions?

Kotlin allows us to provide implementations for a predefined set of operators on our types. These operators have fixed symbolic representation (like + or *) and fixed precedence. To implement an operator, we provide a member function or an extension function with a fixed name, for the corresponding type, i.e. left-hand side type for binary operations and argument type for unary ones. Functions that overload operators need to be marked with the operator modifier.

An imperative programming language we write a statement to add a number to other number is result = Number(1).plus(Number(2)) wherein the function plus is adding two numbers resulting sum. Whereas Kotlin conventions give us the possibility to write it in a more concise way likeresult = Number(1) + Number(2). Like conventions of using the symbol + for add and minus as -

We are going to focus some of the arithmetic conventions in this blog, rest of the conventions we will discuss in next blog post. We are going to use Money class for a demonstration of all the conventions here.

data class Money(val value: Int)

Plus Convention

As the Plus word suggests, this convention is used to add something to other. Kotlin as some predefined operator, which we can use to make programmers life more relaxed. These conventions can be achieved by just using operator keyword for the extension function. This keyword is used at compile to and has restrictions for each time of operator you are using. Example, if plus is extension function name and has operator keyword to it then it can accept only one parameter of the same type and should return the same type back.

operator fun Money.plus(other: Money): Money {
return Money(this.value + other.value)
}
val ten = Money(10)
val seven = Money(7)

Here, we can alternatively use tten + seven instead of ten.plus(seven) for adding two amounts.

Minus Convention

operator fun Money.minus(other: Money): Money {
return Money(this.value - other.value)
}
val ten = Money(10)
val seven = Money(7)

Here, subtracting one amount from other, traditionally we write either ten.minus(seven) or ten.substract(seven) , Instead with Kotlin minus operator (convention), we write ten — seven.

CompareTo Convention

operator fun Money.compareTo(other: Money): Int {
return this.value.compareTo(other.value)
}
val ten = Money(10)
val seven = Money(7)

Here, comparing of two money I can perform several actions like,

val greaterThan: Boolean = ten > seven
val lessThan: Boolean = ten < seven
val equalToThan: Boolean = ten == seven
val notEqualToThan: Boolean = ten != seven
val greaterThanEqualToThan: Boolean = ten >= seven
val lessThanEqualToThan: Boolean = ten <= seven

Instead of implementing extension functions greaterThan lessThan equalToThan notEqualToThan greaterThanEqualToThan lessThanEqualToThan.

Inc | Dec Convention

operator fun Money.inc(): Money {
return Money(this.value + 1)
}
operator fun Money.dec(): Money {
return Money(this.value + 1)
}
val ten = Money(10)

Here, the incrementing amount by one can be achieved by just saying ten++ instead of writing result = ten + one .

Decrementing one can be obtained by just saying ten-- instead of writing result = ten -one

Times | Div Convention

operator fun Money.times(other: Money): Money {
return Money(this.value * other.value)
}
operator fun Money.div(other: Money): Money {
return Money(this.value / other.value)
}
val ten = Money(10)
val seven = Money(7)

Use, ten * seven or ten / seven to multiply and divide money respectively.

Till this point, you might have understood the pattern of using mathematical operators instead of using normal extension functions to do arithmetic operations on data.

Same as above conventions Kotlin also has the other arithmetic conventions like

  • -= for minusAssign ex: result -= ten
  • += for plusAssign
  • *= for timesAssign
  • *= for divAssign
  • %= for remAssign
  • + for unaryPlus

Kotlin has few more conventions for using them as operator, these we will discuss in next blog. Stay Tuned!

References:

  1. https://kotlinlang.org/docs/reference/operator-overloading.html#operator-overloading
  2. https://kotlinlang.org/docs/reference/keyword-reference.html
  3. https://www.programiz.com/kotlin-programming/operators

Interested in joining one of our teams as we grow?

If you’d like to join us on the journey of building the mobile bank the world loves to use, have a look at some of the roles we’re looking for here.

Follow us on LinkedIn and Twitter to keep updated and in touch with us!

--

--

Gopinath Langote
InsideN26

Software Engineer | poet | writer | loveliterature | coder