Florian
08/06/2021, 10:34 AM!!
operator?
Example: In an edit and delete situation I never expect the todoId
to be null, only when a new todo was added. Does it make sense to just use !!
then instead of the null-safe operator?
fun onAddEditResult(result: Int, todoId: Long? = null) {
viewModelScope.launch {
when (result) {
RESULT_TODO_ADDED -> eventChannel.send(Event.ShowTodoSavedConfirmationMessage("Todo added"))
RESULT_TODO_EDITED -> {
eventChannel.send(Event.ShowTodoSavedConfirmationMessage("Todo updated"))
setExpandedState(todoId!!, false)
}
RESULT_TODO_DELETED -> {
val todo = todoDao.getTodoById(todoId!!)
todo?.let {
todoDao.delete(todo)
setExpandedState(todo.id, false)
eventChannel.send(Event.ShowUndoDeleteTodoMessage(todo))
}
}
}
}
}
ephemient
08/06/2021, 10:41 AMsealed class Todo {
object Added : Todo()
data class Edited(val todoId: Long) : Todo()
data class Deleted(val todoId: Long) : Todo()
}
then you can't have such inconsistency between input argumentsFlorian
08/06/2021, 10:52 AMYoussef Shoaib [MOD]
08/06/2021, 3:07 PMFlorian
08/06/2021, 3:15 PMYoussef Shoaib [MOD]
08/06/2021, 3:18 PMFlorian
08/06/2021, 3:24 PMFlorian
08/06/2021, 3:24 PMFlorian
08/06/2021, 3:30 PMYoussef Shoaib [MOD]
08/06/2021, 3:31 PM!!
here is absolutely acceptable. What the people here and I are getting at though is that isn't expandable easily and it requires the developer working on the code to know that this long can only be those 3 values and that if it is those 2 values then the other long cannot be null etc. Simply, it just seems like it isn't documented based on the types.
If this is jsut a personal hobby project, then sure this works. But if it was for something more professional or if you want to do it "The Right Way" then using a sealed class is bestFlorian
08/06/2021, 3:36 PMFlorian
08/06/2021, 3:40 PMFlorian
08/06/2021, 3:41 PMYoussef Shoaib [MOD]
08/06/2021, 3:44 PMFlorian
08/06/2021, 3:46 PMFlorian
08/06/2021, 3:47 PMYoussef Shoaib [MOD]
08/06/2021, 3:50 PMFlorian
08/06/2021, 3:59 PMFlorian
08/06/2021, 3:59 PMeygraber
08/06/2021, 5:01 PM!!
is appropriate wherever you decide it is.
For example, on my team I've decided that it's almost never appropriate because it's usually indicative that something was designed wrong.
It's allowed if it's deep in a 3rd party integration, and even then only if there's no feasible way to refactor.Florian
08/06/2021, 5:13 PMColton Idle
08/06/2021, 9:47 PMColton Idle
08/06/2021, 9:49 PMFlorian
08/06/2021, 9:54 PMColton Idle
08/06/2021, 10:05 PMColton Idle
08/06/2021, 10:05 PMeygraber
08/06/2021, 10:11 PMrequireNotNull
helps a lot with debugging, although it can't be used the same way as !!
ephemient
08/07/2021, 12:24 AMcheckNotNull()
and requireNotNull()
both return the non-null value, so they can be used like !!
, but allow you to attach a message as wellephemient
08/07/2021, 12:25 AM!!
does, so I'm not sure what you mean by "it can't be use in the same way as `!!`"?eygraber
08/08/2021, 1:50 AM!!
ephemient
08/08/2021, 3:04 AM.let { checkNotNull(it) }
could work, but it is pretty long and awkward. can't compare to the succinctness of !!
, that's for sure