- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个函数,它有很多嵌套的 if 语句,sonarqube 提示它。可能会发生一系列情况,我会跟踪每种情况发生的时间,返回一个 bool 和一个 int,然后增加一个计数器。
def _fix(ids, id, sch_date, cycles, dp):
try:
gs = cycles.get(id)
except AttributeError:
gs = False
if id in ids:
if cycles:
if gs:
if sch_date in gs:
if dp in gs[sch_date]:
return True, None
else:
self.d_types[4] += 1
return False, 4
else:
self.d_types[1] += 1
return False, 1
else:
self.d_types[3] += 1
return False, 3
else:
self.d_types[2] += 1
return False, 2
else:
return False, None
我在想我可以做这样的事情:
if id in ids and cycles and gs and such_date in gs and dp in gs[sch_date]:
do something...
但是我不知道它在哪里短路,所以我将无法增加计数器或返回必要的 int 和 boolean。
有什么想法可以摆脱所有这些 if 语句,同时仍然保留 return 和 counter 吗?
最佳答案
每个 else
都可以终止函数,因此反转测试条件。如果函数已经返回,则不需要 else
,这大大减少了嵌套代码。
if id not in ids:
return False, None
if not cycles:
self.d_types[2] += 1
return False, 2
if not gs:
self.d_types[3] += 1
return False, 3
if sch_date not in gs:
self.d_types[1] += 1
return False, 1
if dp not in gs[sch_date]:
self.d_types[4] += 1
return False, 4
return True, None
关于短路时的python调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37128841/
考虑 function foo(x) x isa Bar || throw(ArgumentError("x is not a Int64...")) dosomething(x) e
这个问题在这里已经有了答案: AndAlso/OrElse in VBA (8 个回答) 7年前关闭。 VBA 不会短路 VBA 不支持短路 - 显然是因为它只有按位 And/Or/Not 等操作。来
这个问题在这里已经有了答案: Java operator precedence guidelines (6 个答案) 关闭 7 年前。 你好,我有一个关于 java 中的优先级表的问题。它说 &&
a &= foo(); 只是被 C++ 中的 a = a && foo(); 取代还是还有更多内容? 我关心的是 foo() 的计算。如果 a 为假,它会被调用吗? 最佳答案 && 是否有嵌入式短路:
我正在尝试注释以下代码。 当同时定义了 zone 和 zones 时,或者当定义了 file 时(但不是同时定义了两者),该函数将起作用: def get_file(zone: str, zones:
给定远程beam.smp服务的已知TCP端口和名称,以及已知的cookie,是否可以短路Erlang分发协议(protocol)的Erlang端口映射器守护进程握手阶段并直接建立Erlang shel
我正在尝试注释以下代码。 当同时定义了 zone 和 zones 时,或者当定义了 file 时(但不是同时定义了两者),该函数将起作用: def get_file(zone: str, zones:
我需要从第二个表中查询数据,但前提是满足主表中的一组罕见条件: SELECT ..., IF(a AND b AND c AND (SELECT 1 FROM tableb ...)) FROM ta
如标题所述,我想将 onchange 短路(在选择内部)。我有以下代码,但我只想发送一个硬编码值而不显示选择框。 "> [编辑] 对不起,我不清楚。我什至不想提供
我是 C++ 的新手,很好奇编译器如何处理 bool 值的惰性求值。例如, if(A == 1 || B == 2){...} 如果 A 确实等于 1,那么 B==2 部分是否曾经计算过? 最佳答案
我在尝试让这个 linq 语句起作用时遇到了一些麻烦。我正在使用 linq 查询设置搜索。我想要做的是,如果搜索为空或为空,让它忽略过滤的那一部分。所以我设置了很多 where 子句来缩短 where
这个问题在这里已经有了答案: How to short-circuit a reduce() operation on a Stream? (4 个答案) 关闭 4 年前。 在做项目的时候写了这行,
首先,我不是 Bash 专家。几个月前我发现,如果我同时使用 && 和 || 短路运算符和花括号,那么如果第一个语句以真实值退出,如果 true block 中的最后一条语句退出非零,那么 fail
我有一个使用逻辑 OR 运算符的 NSPredicate,如下所示: NSPredicate(format: "activeFrom == NULL OR %@ >= activeFrom", som
我是 C++ 的新手,很好奇编译器如何处理 bool 值的惰性求值。例如, if(A == 1 || B == 2){...} 如果 A 确实等于 1,那么 B==2 部分是否曾经计算过? 最佳答案
我需要从第二个表中查询数据,但前提是满足主表中的一组罕见条件: SELECT ..., IF(a AND b AND c AND (SELECT 1 FROM tableb ...)) FROM ta
我遇到了这段代码: 我想我们可以归纳为: 然后我找到了这个 article on short circuits和另一个 more focused one一旦我知道他们叫什么。但是,我还是不明白。
据我了解和阅读,您可以在 if 语句(&& 或 ||)中使用短路,以使第二个条件不触发。如果您希望两个条件都触发,您可以使用单个操作数(& 或 |)。 假设我有如下内联 if 语句: var test
在玩弄编译时字符串(char 的可变参数列表)操作时,我需要实现一种方法来检查编译时字符串是否包含另一个(较小的)编译时字符串。 这是我的第一次尝试: template struct Contains
如果我有以下... a OrElse b ... 并且 a 是 真那么显然 b 永远不会被评估。但是如果我添加一个 Or ,然后呢? a OrElse b Or c c 是否/应该得到评估?如果我放入
我是一名优秀的程序员,十分优秀!