Hi, what is the better between ```data class Zone(...
# getting-started
Hi, what is the better between
Copy code
data class Zone(x: IntRange, y: IntRange, z: IntRange)
Copy code
data class Zone(x1: Int, y1: Int, z1: Int, x2: Int, y2: Int, z2: Int)
? And would it impact performances to use Ranges ? I only need like 50 zones maximum for my project
1️⃣ 4
first code for clarity, so definitely the former code, then measure the effective performance and eventually deal with it
A good rule of thumb is: don't worry about performance until a) you have a genuine problem and b) you have automated a way to performance test it that will prove that one way is faster than another. Until you've got both of those, write the thing that is easiest to understand. In this case that's clearly 1.
The first code can be improved with better parameter names, since they aren’t just points but ranges; so maybe
, etc. for example
Nice explanation thanks !
If performance turns out to be crucial, there's still no need for #2. What you can do is this: (playground)
Copy code
value class MyIntRange private constructor(private val underlying: Long) {
    constructor(start: Int, end: Int) : this(packStartWithEnd(start, end))

    val start: Int
        get() = unpackStart(underlying)

    val end: Int
        get() = unpackEnd(underlying)
    fun copy(
        start: Int = this.start,
        end: Int = this.end
    ): MyIntRange =
        MyIntRange(start, end)


private fun packStartWithEnd(start: Int, end: Int): Long =
    (start.toLong() shl Int.SIZE_BITS) or (end.toLong() and 0xffffffffL)

private fun unpackStart(packed: Long): Int =
    (packed shr Int.SIZE_BITS).toInt()

private fun unpackEnd(packed: Long): Int =

fun main(){
    // Poor man's unit tests
    val range = MyIntRange(20, 45)
    val newRange = range.copy(end = 42)
    println(range.start == 20)
    println(range.end == 45)
    println(newRange.start == 20)
    println(newRange.end == 42)
This doesn't sacrifice the conciseness of #1 nor does it sacrifice the performance of #2