https://kotlinlang.org logo
Title
i

igor.wojda

07/30/2019, 1:31 PM
is
prefix for Boolean returning methods is quite straight forward. I wonder what are you thoughts on adding
is
prefix to Boolean properties?
// method
view.isVisible()

// property
view.visible
or
view.isVisible
k

karelpeeters

07/30/2019, 1:39 PM
In this case I lean towards
visible
but it's an internal struggle every time 😒imple_smile:
d

dalexander

07/30/2019, 1:42 PM
I think at this point Kotlin has been in production too long to make that change, even if I would like to see it (I'm not a huge fan of property syntax in general, so I'm probably biased).
s

Stefan Beyer

07/30/2019, 1:45 PM
I personally prefer
view.isVisible
, but it depends on whether you need the accessors to be used (by java code or some lib like jackson)
// kotlin
class Foo(val cool:Boolean,val isHot:Boolean)

// java
public class Java {
    public static void main(String[] args) {
        Foo foo = new Foo(true, true);
        foo.getCool(); // not very cool :(
        foo.isHot();
    }
}
you could add
@getter:JvmName("isCool")
to the property, but this is uglier than in the first place ^^
k

karelpeeters

07/30/2019, 1:47 PM
@dalexander what change?
d

dalexander

07/30/2019, 1:49 PM
I was just thinking about how properties can be exposed which may not follow boolean naming conventions?
But it's slightly off topic from the original question I suppose. I do lean towards
isFoo
pretty heavily.
a

arekolek

07/30/2019, 2:14 PM
I write
isVisible: Boolean
rather than
visible: Boolean
Because of java interop, but also like I would write
hasChildren: Boolean
and not
children: Boolean
Also, there’s #codingconventions 😉
👍 1
i

igor.wojda

07/30/2019, 2:22 PM
Let’s assume Java is not there - pure Kotlin project
a

arekolek

07/30/2019, 3:13 PM
For example
view.takeIf { it.isVisible }
reads better than
view.takeIf { it.visible }