Is there any rule as to when I should use `.not()`...
# getting-started
j
Is there any rule as to when I should use
.not()
and when I should use good old
!
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/not.html
n
I'd never use
not()
j
Someone on my team is using it. A lot. What should I tell him to discourage him?
t
!condition
is used in most programming languages (or at least in most of those I know). Because it is more familiar to non-Kotlin developers, I think i'ts more readable than
condition.not()
, especially in long conditions. AFAIK the Kotlin compiler may not fully optimize the
.not()
function call in some cases, compared to when using
!
n
I'd expect
!
and
.not()
to be bytecode-equivalent since
.not()
is just an operator function, btw.
👆 1
having a postfix negation method just seems unnatural to me. it's more obvious to say "not some condition" instead of "some condition, not"
e
I wonder if they were influenced by Borat english in this case 🤔 This is great... NOT!
😄 6
n
I don't know for sure that you can "convince" someone of this based on merit alone...I'd say have a team style guide and enforce it.
j
OK, but if its not a good idea, then what sort of cases was the operator designed for?
s
Out of curiosity:
Copy code
fun test(): Boolean = true
fun main () { test().not() }

// decompiled
public static final boolean test() { return true;}
public static final void main() { if (!test()) {} }
I guess Kotlin doesn’t optimize that out
@Jacob maybe for Unit testing during expects. I come from Jasmine/Mocha world and I see that a lot
n
I think it's just how the
!
operator is implemented
just because you can call
plus
doesn't mean you're supposed to call it by that name
☝️ 1
that's the kind of operation the JVM would optimize out I think
e
@Son it'll optimize it with a
const val
, otherwise it's not constant and can't be inlined at compile time.
e
@Jacob
Someone on my team is using it. A lot. What should I tell him to discourage him?
"You must unlearn what you have learned." (c) Yoda
😁 1
j
do not do do.not() do !do
😀 2
t
"House Style: `!not()`"