我建议采用前一种做法的唯一原因是,如果这套方法具有某种可图性,或者如果它拥有如此庞大和完全的复杂数据,它实际上会有助益。
If that was the case, I would still give it a name and leverage the fact that contains
is also aliased as apply
for Set
s to make it more readable (and avoid creating a lot of very short-lived data):
val isFooOrBar: String => Boolean = Set("foo", "bar")
if (isFooOrBar(x)) { ... }
关于业绩:
- there is a highly likelihood that this is not critical
- if it is critical, the only source of truth is measuring
在这方面,我确信,“很有可能取得更好的效果(没有数据可生成、低层抽象化、对时间很熟悉和JIT等),但即便如此,我还是将侧重于可读性。 如果<条码>类别条码>相当庞大,你可能不想将整件内容放在<条码>中,但不妨碍你做以下工作:
def isFooOrBar(x: String): Boolean = x == "foo" || x == "bar"
if (isFooOrBar(x)) { ... }
which makes it indistinguishable at call-site what approach you took and is arguably readable anyway. Maybe the only improvement is to give the predicate a name that described more accurately the why, like in the following made-up snippet:
// anything but "foo" and "bar" are incompatible with the retroencabulator
def isCompatibleWithRetroencabulator(s: String) = ...
如果你在上注意履约情况,就将其改为 私人
,并可能将其附在@inline
(及衡量标准);