Thiago
12/08/2021, 2:58 PMa > 0 && a < 10
and replace with range as a in 1..9
?
Why "primitive" comparisons are not recommended in favor of Ranges instances to call a contains operator?
Is there any article/resources explaining the recomendation?dmitriy.novozhilov
12/08/2021, 3:06 PMa in 1..9
is much more readable than a > 0 && a < 10
And in
operator in this case actually optimized by compiler
fun test_1(x: Int) {
if (x in 10..20) {
println()
}
}
/*
L0
LINENUMBER 2 L0
BIPUSH 20
BIPUSH 10
ILOAD 0
ISTORE 1
ILOAD 1
IF_ICMPLE L1
POP
GOTO L2
L1
ILOAD 1
IF_ICMPLT L2
L3
LINENUMBER 3 L3
L4
GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
INVOKEVIRTUAL java/io/PrintStream.println ()V
L5
L2
LINENUMBER 5 L2
RETURN
*/
// ------------------------------------------------------------------------------
fun test_2(x: Int) {
if (x >= 10 && x < 20) {
println()
}
}
/*
L0
LINENUMBER 33 L0
ILOAD 0
BIPUSH 10
IF_ICMPLT L1
ILOAD 0
BIPUSH 20
IF_ICMPGE L1
L2
LINENUMBER 34 L2
L3
GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
INVOKEVIRTUAL java/io/PrintStream.println ()V
L4
L1
LINENUMBER 36 L1
RETURN
*/
Fleshgrinder
12/08/2021, 3:24 PM0 < x && x < 10
cannot be further optimized, as it's not providing the compiler the knowledge regarding what's happening on each side of the branch.jimn
12/10/2021, 1:51 PMin ..
?dmitriy.novozhilov
12/10/2021, 1:53 PMjimn
12/10/2021, 1:54 PMjimn
12/10/2021, 1:55 PMif
to when
, and so on.dmitriy.novozhilov
12/10/2021, 1:55 PMjimn
12/10/2021, 2:05 PMnatario1
12/13/2021, 10:14 AMmaxDuration > 0L && currDuration >= maxDuration
is suggested to be transformed into maxDuration in 1..currDuration
, which is terrible (maxDuration is a constant, 0 means don't check).