Table of Contents

代码操作 DR014(可读性):使用 IN 简化

描述

使用 IN 运算符改写复合谓词(对同一表达式进行的等值比较,并通过 OR 或 || 连接)。

示例

将以下内容:

IF(Document[Type] = "Invoice" || Document[Type] = "Credit Note", 1, 0)

改为:

IF(Document[Type] IN {"Invoice", "Credit Note"}, 1, 0)

为什么 Tabular Editor 会这样建议?

IN 运算符比多个 || 运算符或多次调用 OR 函数更简洁,也更易读。同时,也更方便在要比较的值列表中添加或移除值。