https://kotlinlang.org logo
#getting-started
Title
# getting-started
j

Jacob

10/19/2020, 9:00 PM
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

nanodeath

10/19/2020, 9:07 PM
I'd never use
not()
j

Jacob

10/19/2020, 9:08 PM
Someone on my team is using it. A lot. What should I tell him to discourage him?
t

tseisel

10/19/2020, 9:18 PM
!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

nanodeath

10/19/2020, 9:22 PM
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

Even André Fiskvik

10/19/2020, 9:22 PM
I wonder if they were influenced by Borat english in this case 🤔 This is great... NOT!
😄 6
n

nanodeath

10/19/2020, 9:24 PM
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

Jacob

10/19/2020, 9:36 PM
OK, but if its not a good idea, then what sort of cases was the operator designed for?
s

Son

10/19/2020, 9:36 PM
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

nanodeath

10/19/2020, 10:08 PM
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

ephemient

10/20/2020, 12:58 AM
@Son it'll optimize it with a
const val
, otherwise it's not constant and can't be inlined at compile time.
e

elizarov

10/20/2020, 8:00 AM
@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

Joris PZ

10/20/2020, 8:55 AM
do not do do.not() do !do
😀 2
t

todd.ginsberg

10/21/2020, 1:33 PM
"House Style: `!not()`"
3 Views