0

I learned to use enum class to constantize the constants involved. I also learned that using when when using enum class is very effective. I think I heard that you may not use else.

Case1 may not use else in isMovable. However, we still have to use the else in fromConditionOf() and the when is redundant twice.

Am I misunderstanding something? Should I use else when using enum class?

Case 0 is the code when no enum class is used

To briefly explain the code, when the car gets a random value (0-9), it stops if it's 0-3, and moves forward if it's 4-9.

Case 0:




data class Car(
    val name: CarName,
) {
    init {
        require(name.isNotBlank()) { "[ERROR] Name is empty or blank" }
        require(name.length <= 5) { "[ERROR] Car names longer than five letters" }
    }

    fun move(randomValue: Int) =
        when (randomValue) {
            in 4..9 -> true
            in 0..3 -> false
            else -> throw IllegalArgumentException("[ERROR] When a random value is invalid")
        }
}



Case 1:




enum class Movement(val range: IntRange) {
    STOP(0..3),
    GO(4..9);

    companion object {
        fun fromConditionOf(randomValue: Int): Movement =
            when (randomValue) {
                in STOP.range -> STOP
                in GO.range -> GO
                else -> throw IllegalStateException("[ERROR] When a random value is invalid")
            }
    }
}

data class Car(
    val name: String,
) {
    init {
        require(name.isNotBlank()) { "[ERROR] Name is empty or blank" }
        require(name.length <= 5) { "[ERROR] Car names longer than five letters" }
    }

    fun isMovable(randomValue: Int): Boolean =
        when (Movement.fromConditionOf(randomValue)) {
            Movement.STOP -> false
            Movement.GO -> true
        }
}




Case2:




enum class Movement(val range: IntRange) {
    STOP(0..3),
    GO(4..9);

    companion object {
        fun fromConditionOf(randomValue: Int): Boolean =
            when (randomValue) {
                in STOP.range -> false
                in GO.range -> true
                else -> throw IllegalStateException("[ERROR] When a random value is invalid")
            }
    }
}

data class Car(
    val name: String,
) {
    init {
        require(name.isNotBlank()) { "[ERROR] Name is empty or blank" }
        require(name.length <= 5) { "[ERROR] Car names longer than five letters" }
    }

    fun isMovable(randomValue: Int): Boolean =
        Movement.fromConditionOf(randomValue)
}




10
  • the redundant else are not actually redundant. You're running a when clause through an Int value. so there are other int values than those that you have given a specific handling for. Case 1 Car.isMovable is actually running the when through the enum and you can see that no else is needed because there are finite values. Commented Dec 6, 2023 at 9:19
  • Thank you for your response. So can we say that case1 used enum class and when well? Commented Dec 6, 2023 at 9:23
  • I don't see a problem with it. Commented Dec 6, 2023 at 9:24
  • It was awkward to use when twice because it felt like it was redundant. I think I misunderstood. thank you! Commented Dec 6, 2023 at 9:26
  • you'd be better not having the enum in the example and just running the integer through the when clause directly. that'll be more efficient. but for the process of learning and using enums with when it fine. Commented Dec 6, 2023 at 9:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.