1

A dataConfig object has a nullable Boolean field urlIsabled, and would like to return based on whether the dataConfig.urlIsabled == null or the negative of the the dataConfig.urlIsabled

val result = if (dataConfig.urlIsabled != null) (dataConfig.urlIsabled != true) else true)

could it be simplified?

lannyf
  • 9,865
  • 12
  • 70
  • 152

3 Answers3

4

Plot a simple truth table:

| dataConfig.urlIsabled | result |
|-----------------------|--------|
|        null           |  true  |
|        true           |  false |
|        false          |  true  |
|-----------------------|--------|

So the result is true in all cases except when urlIsabled equals true. Thus it can be expressed as:

val result = dataConfig.urlIsabled != true
Ilya
  • 21,871
  • 8
  • 73
  • 92
1

You try to return true if dataConfig.urlIsDisabled is null or false.

Just invert the logic and return false if value is equal to true:

val result = !(dataConfig.urlIsabled == true)

Pawel
  • 15,548
  • 3
  • 36
  • 36
0

Can be simplified as:

val result = dataConfig.urlIsabled in listOf(false, null)
Boken
  • 4,825
  • 10
  • 32
  • 42