https://kotlinlang.org logo
#android
Title
# android
h

henrikhorbovyi

05/20/2020, 9:52 PM
Hey kotliners, 🙂 Does anybody have some opinion about KTX:
isVisible
,
isInvisible
,
isGone
extensions? I mean, first time I implemented KTX in my project I expected something like:
gone()
,
visible()
,
invisible()
h

Haim

05/21/2020, 6:31 PM
I always use
isVisible
to set visibility according to a boolean expression. For example:
titleView.isVisible = title.isNotEmpty()
. So something like
visible()
would not be so helpful for me.
h

henrikhorbovyi

05/21/2020, 7:12 PM
Good point, but in case you have no boolean expression, you just want to set some view visible/invisible after some liveData changes? You don't think
visible()
or
invisible()
is a better approach?
Idk, it seems more elegant for me 😄
h

Haim

05/21/2020, 7:25 PM
Actually, I even wasn't sure if your
visible()
function is setting it to visible or checking if it's visible...
Generally functions that do something should start with a verb. So
setVisible()
or
makeVisible()
sound better to me.
visible()
on the other hand, is an adverb, so the word doesn't seem to indicate that the function changes the visibility of the view.
c

Cody Engel

05/21/2020, 7:46 PM
Yeah mutators in general are shortened in Kotlin (remove the
get
and the
set
) but when it comes to boolean expressions you'd still have
is
or
has
for example. I wasn't aware KTX had those already, they sound pretty nifty though.
h

henrikhorbovyi

05/21/2020, 8:32 PM
I got it! Thank you for the opinion guys
s

sindrenm

05/22/2020, 9:46 AM
I'm a little bit on the fence about having boolean setters/getters for setting visibility, since it's really a tri-state thingy. Who's to say that
isVisible = false
makes it
GONE
and not
INVISIBLE
, for instance?
☝️ 1
✔️ 1
I mean “who's to say” is obviously the API and its docs, but my point is that the name itself is somewhat ambiguous.
☝️ 1
h

henrikhorbovyi

05/22/2020, 1:44 PM
@sindrenm. I faced this code and it kinda bugged my mind a bit 😄 A banch of
true
and
false
Then, we made some extensions
c

Cody Engel

05/22/2020, 4:03 PM
How often do folks make things invisible instead of gone though? Realistically if I don't want to show something, I don't want it taking up any space on the screen. There are of course cases where you would want invisible but I don't think I've had a use for it on the last 4 apps I've worked on
h

henrikhorbovyi

05/22/2020, 4:38 PM
Yeah I agree, I rarely set some view as invisible... but in this case it was needed