Extension Properties & Methods | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/8458472-programming-in-kotlin-functions-custom-types/lessons/14

Code looks better with when usage

fun SimpleDates.monthsUntilHallowenDecoration(): Int{

    val currentMonth = SimpleDates.months.indexOf(month)
    val hallowenStart = SimpleDates.months.indexOf("September")
    val hallowenEnd = SimpleDates.months.indexOf("October")

    return when(currentMonth){
        in 0..hallowenStart -> hallowenStart - currentMonth
        in hallowenStart..hallowenEnd -> 0
        else -> hallowenStart + (12 - currentMonth)
    }
}