Do people typically add `is` at the start of a boo...
# random
c
Do people typically add
is
at the start of a boolean name in kotlin? Basically need to create a bool called
isDebug
but arguing with myself if it should be
debug
or
isDebug
.
👌 13
🚫 1
k
To follow the example set by the designers of Kotlin: if a Java boolean method is called
isDebug()
then Kotlin creates a property called
isDebug
. So, yes, add the
is
prefix.
💯 4
r
Copy code
val isGoodPractice = true
😄 5
s
no, not as a rule. Sometimes
is
as a prefix makes sense (in order to make code read more expressively) but using it indiscriminately is basically just Hungarian notation with extra steps
👍 1
💯 2
say you want to add a
debug
flag to some kind of config, then you go to use it, which results in the callsite looking like this
Copy code
if (config.isDebug) {
  ...
}
which reads like “if the config is debug, then…“, which doesn’t really make a whole lot of sense. You might try to make it a bit more descriptive like this
Copy code
if (config.isDebugMode)
but really, I think that just makes it obvious that you should omit
is
in this case, and just write something like this
Copy code
if (config.debugEnabled)
I’d try to reserve usage of
is
in cases where the boolean describes the state of its container or otherwise actually makes a conditional read more sensibly
6
also s/o #codingconventions
t
what about java interop? calling the property
debug
generates a method
getDebug()
, calling it
isDebug
generates
isDebug()
. depending on what library you use (or is you want your library to be used in java) I think the second convention is more solid (naming convention for boolean getters is - or at least was, honestly not sure anymore -
isMyBool
)
t
Another common prefix is
has
as in
Node.hasChildren
. So I'd say it depends a bit on your preference and whether you want the special Java interop or not.
👍 2