https://kotlinlang.org logo
#detekt
Title
# detekt
c

Czar

12/09/2019, 7:12 AM
In a simple entity update situation, where there's an entity
Copy code
class MyEntity {
  val id: Long? = null
  var prop1: String? = null
  var prop2: String? = null
// ...
  var propN: String? = null
}
and an update command:
Copy code
class UpdateEntityCommand {
  val myEntityId: Long
  var prop1: String? = null
  var prop2: String? = null
// ...
  var propN: String? = null
}
the handler should only change property of the entity if corresponding property of the command is not
null
:
Copy code
class MyHandler(private val repo: MyEntityRepository) {
  fun handle(command: UpdateEntityCommand) {
    with(repository.getOne(command.myEntityId)) {
      // (1)
      if (command.prop1 != null) prop1 = command.prop1
      // (2)
      command.prop2?.let { prop2 = it }
      // (3)
      ifNotNull(command.propN) { propN = it }
    }
  }
}
If all the properties are set using method (1) or (3), detekt is happy, if method (2) is used, detekt is complaining about
"ComplexMethod"
Should I report this to the issue tracker, or is it as it should be? If the latter, I wonder why?
b

Brais Gabin

12/09/2019, 12:24 PM
This is an issue, so report it. The complexity of 1 and 2 are the same. And is
ifNotNull
an standard function? If so, it should be the same too.
c

Czar

12/10/2019, 7:18 AM
No, not standard:
inline fun <T : Any, R> ifNotNull(input: T?, block: (T) -> R): R? = input?.let(block)
3 Views