Kotlin代码重构
我有一个功能,检查是否有搜索条件Kotlin代码重构,kotlin,Kotlin,我有一个功能,检查是否有搜索条件 var currentStatus = MutableLiveData<List<Int>>() private var sellerIds: List<Int>? = null private var subSellerIds: List<Int>? = null private var partnerIds: List<Int>? = null private var productTypeIds
var currentStatus = MutableLiveData<List<Int>>()
private var sellerIds: List<Int>? = null
private var subSellerIds: List<Int>? = null
private var partnerIds: List<Int>? = null
private var productTypeIds: List<Int>? = null
private var brunchIds: List<Int>? = null
private var contractDateFrom: String? = null
private var contractDateTo: String? = null
private var completeDateFrom: String? = null
private var completeDateTo: String? = null
private var totalStatusCnt: Int = 10
val isSearching = ObservableField<Boolean>(false)
private fun checkIfSearchConditions() {
if (currentStatus.value != null && currentStatus.value!!.isNotEmpty() &&
currentStatus.value?.size != totalStatusCnt) {
isSearching.set(true)
} else if (keyword != null && keyword!!.isNotEmpty()) {
isSearching.set(true)
} else if (sellerIds != null && sellerIds!!.isNotEmpty()) {
isSearching.set(true)
} else if (subSellerIds != null && subSellerIds!!.isNotEmpty()) {
isSearching.set(true)
} else if (partnerIds != null && partnerIds!!.isNotEmpty()) {
isSearching.set(true)
} else if (productTypeIds != null && productTypeIds!!.isNotEmpty()) {
isSearching.set(true)
} else if (brunchIds != null && brunchIds!!.isNotEmpty()) {
isSearching.set(true)
} else if (contractDateFrom != null && contractDateFrom!!.isNotEmpty()) {
isSearching.set(true)
} else if (contractDateTo != null && contractDateTo!!.isNotEmpty()) {
isSearching.set(true)
} else if (completeDateFrom != null && completeDateFrom!!.isNotEmpty()) {
isSearching.set(true)
} else if (completeDateTo != null && completeDateTo!!.isNotEmpty()) {
isSearching.set(true)
} else {
isSearching.set(false)
}
}
但是文件类型可以是列表?还是字符串?那么
“isNotEmpty不是未解决的引用”错误
如何重构该方法以明确说明问题?为
列表?
和字符串?
创建两个单独的重载方法:
private fun checkIfNotEmpty(field: String?): Boolean {
return field != null && field.isNotEmpty()
}
private fun checkIfNotEmpty(field: List<*>?): Boolean {
return field != null && field.isNotEmpty()
}
此外,具有相同主体的所有else if
分支可以使用|
组合:
if ((checkIfNotEmpty(currentStatus) &&
currentStatus?.value?.size != totalStatusCnt) ||
checkIfNotEmpty(keyword) ||
checkIfNotEmpty(sellerIds) || ...
) {
isSearching.set(true)
} else {
isSearching.set(false)
}
这一点还可以进一步改进
val isSearchingValue = (checkIfNotEmpty(currentStatus) &&
currentStatus?.value?.size != totalStatusCnt) ||
checkIfNotEmpty(keyword) ||
checkIfNotEmpty(sellerIds) || ...
isSearching.set(isSearchingValue)
为
列表?
和字符串?
创建两个单独的重载方法:
private fun checkIfNotEmpty(field: String?): Boolean {
return field != null && field.isNotEmpty()
}
private fun checkIfNotEmpty(field: List<*>?): Boolean {
return field != null && field.isNotEmpty()
}
此外,具有相同主体的所有else if
分支可以使用|
组合:
if ((checkIfNotEmpty(currentStatus) &&
currentStatus?.value?.size != totalStatusCnt) ||
checkIfNotEmpty(keyword) ||
checkIfNotEmpty(sellerIds) || ...
) {
isSearching.set(true)
} else {
isSearching.set(false)
}
这一点还可以进一步改进
val isSearchingValue = (checkIfNotEmpty(currentStatus) &&
currentStatus?.value?.size != totalStatusCnt) ||
checkIfNotEmpty(keyword) ||
checkIfNotEmpty(sellerIds) || ...
isSearching.set(isSearchingValue)
Kotlin stdlib包含函数
public inline fun CharSequence?.isNullOrEmpty(): Boolean = this == null || this.length == 0
public inline fun CharSequence?.isNullOrBlank(): Boolean = this == null || this.isBlank()
您可以为Kotlin stdlib包含的函数定义类似的乐趣
public inline fun CharSequence?.isNullOrEmpty(): Boolean = this == null || this.length == 0
public inline fun CharSequence?.isNullOrBlank(): Boolean = this == null || this.isBlank()
您可以为
Collection?
定义类似的乐趣,为什么不使用stdlib中的String?.isNullorEmpty()
?我要创建扩展函数而不是常规函数。为什么不使用stdlib中的String?.isNullorEmpty()
?我会创建扩展函数而不是常规函数。