Categories
tech

代码设计之 Guard clause

大家有没有看过这样的代码?就是一个方法里面,整个就是一个if语句。然后这个if语句非常长长长长。。。一屏都放不下,你都不知道这个if语句会在哪里结束。 fun runRule() if (rulesEnabled) { var maxConsecutiveCount = 0 var currentConsecutiveCount = 0 var previousReceiver = “” expression.children.forEach { if (it is KtBinaryExpression) { val currentReceiver = getReceiver(it) if (currentReceiver == previousReceiver) { currentConsecutiveCount ++ } else { currentConsecutiveCount = 1 previousReceiver = currentReceiver } maxConsecutiveCount = max(maxConsecutiveCount, currentConsecutiveCount) } else { currentConsecutiveCount = […]