用好 Require,check,assert,写好 Kotlin 代码

在编码的时候,我们需要做很多的检测判断,比如某个变量是否为null,某个成员属性是否为true,执行某个操作结果是否成功。比如像下面的这段代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

var isDiskMounted = true

fun createNewFile(file: File?): Boolean {
return if (isDiskMounted) {
if (file != null) {
file.createNewFile()
if (file.exists()) {
true
} else {
println(“Create file($file) failed”)
false
}
} else {
println(“File($file) is null”)
false
}
} else {
println(“Disk is not mounted”)
false
}
}

上面的代码实现了

  • 检测磁盘是否挂载
  • 检测file参数是否为null
  • 检测执行操作结果是否成功(file.exists())

但是上面的代码也有一些问题

  • 太多的if else 检测,层级产生,不够平
  • 多个方法出口
  • 更不容易发现异常和错误(有点类似fail safe模式)

使用今天的知识点改造

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

fun createNewFileV2(file: File?): Boolean {
check(isDiskMounted) {
“Disk is not mounted”
}

requireNotNull(file) {
“file is null”
}

file.createNewFile()

assert(file.exists()) {
“createNewFileV2 file($file) does not exist”
}
return true
}

  • 方法体没有多余层级,比较平
  • 单个方法出口
  • 更快更早发现问题(有点类似fail fast)
  • file.createNewFile()执行时可以不需要再使用file?.createNewFile() 这一点是因为使用了Contract

require

  • require(boolean) 用来检测方法的参数,当参数boolean为false时,抛出IllegalArgumentException

示例代码

1
2
3
4
5
6
7
8
9
10
11
12

fun readFileContent(file: File?): String {
//判断file不能为null
requireNotNull(file)

//判断文件必须可读,并提供错误的信息
require(file.canRead()) {
“readFileContent file($file) is not readable”
}

//read file content
return “Your file content”
}

变种方法

  • fun require(value: Boolean)
  • fun require(value: Boolean, lazyMessage: () -> Any)
  • fun requireNotNull(value: T?)
  • fun requireNotNull(value: T?, lazyMessage: () -> Any)

check

  • check(boolean)用来检测对象的状态(属性),如果boolean为false,抛出异常IllegalStateException

示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13

class Engine {
var isStarted = false

fun speedUp() {
check(isStarted) {
“Engine is not started, cannot be speed up now”
}

//speed up the engine
}

}

变种方法

  • fun check(value: Boolean, lazyMessage: () -> Any)
  • fun checkNotNull(value: T?)
  • fun checkNotNull(value: T?, lazyMessage: () -> Any)

assert

  • assert(boolean) 用来检测执行结果,当boolean为false时,抛出AssertionError。但是需要在开启对应的JVM选项时才生效。

示例代码

1
2
3
4
5
6
7
8

fun makeFile(path: String) {
val file = File(path)
file.createNewFile()

assert(file.exists()) {
“make File($file) failed”
}
}

使用顺序

  • 先使用check检测对象的状态
  • 再使用require检测方法的参数合法性
  • 执行操作后,使用assert校验结果

关于lazyMessage

崩溃更多了,怎么办

  • 是的,上面无论是require,check,assert都会在发现错误的时候抛出异常
  • 这是为了让问题更早发现,这就是它们的哲学
  • 如果想要考虑稳定的话,可以在业务侧 debug下崩溃,非debug下捕获吞掉异常

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

fun main() {
createNewFile(null)
safeRun {
createNewFileV2(null)
}
}

private val isDebug = true

fun safeRun(block: () -> Unit) {
try {
block()
} catch (t: Throwable) {
t.printStackTrace()
if (isDebug) {
throw t
}
}
}

下载说明:
1、本站所有资源均从互联网上收集整理而来,仅供学习交流之用,因此不包含技术服务请大家谅解!
2、本站不提供任何实质性的付费和支付资源,所有需要积分下载的资源均为网站运营赞助费用或者线下劳务费用!
3、本站所有资源仅用于学习及研究使用,您必须在下载后的24小时内删除所下载资源,切勿用于商业用途,否则由此引发的法律纠纷及连带责任本站和发布者概不承担!
4、本站站内提供的所有可下载资源,本站保证未做任何负面改动(不包含修复bug和完善功能等正面优化或二次开发),但本站不保证资源的准确性、安全性和完整性,用户下载后自行斟酌,我们以交流学习为目的,并不是所有的源码都100%无错或无bug!如有链接无法下载、失效或广告,请联系客服处理!
5、本站资源除标明原创外均来自网络整理,版权归原作者或本站特约原创作者所有,如侵犯到您的合法权益,请立即告知本站,本站将及时予与删除并致以最深的歉意!
6、如果您也有好的资源或教程,您可以投稿发布,成功分享后有站币奖励和额外收入!
7、如果您喜欢该资源,请支持官方正版资源,以得到更好的正版服务!
8、请您认真阅读上述内容,注册本站用户或下载本站资源即您同意上述内容!
原文链接:https://www.dandroid.cn/20783,转载请注明出处。
0

评论0

显示验证码
没有账号?注册  忘记密码?