I have a field and I need to compare if the given field is equal to one of my constants. I have three constants in total:
companion object {
private const val NEGATIVE_POSITION = -1
private const val ZERO_POSITION = 0
private const val POSITIVE_POSITION = 1
}
For comparison, I decided to use range:
if(position in NEGATIVE_POSITION..POSITIVE_POSITION)
But in this case, my code becomes less obvious, because it is not clear what the values that are in the range mean (because ZERO_POSITION
not used).
Of course, I could use this method, but this method seems very cumbersome to me:
if(position == NEGATIVE_POSITION || position == ZERO_POSITION || position == POSITIVE_POSITION)
Tell me please, is there a more elegant way to perform such a comparison, while not losing the meaning of each constant?
Please, help me.
You can keep a list of all the constants and do your
in
check with the List instead of a Range.