I have a variable called `dec31_2015` in Java. Ho...
# announcements
g
I have a variable called
dec31_2015
in Java. How should I name that in Kotlin? I get this warning about the underscore.
r
Typically you would used
camelCase
for identifiers in Java/Kotlin which is why you get the warning. Could go with
dec312015
, but that is a bit harder to read. For the sake of readability you could ignore the warning, or consider renaming the variable entirely.
newYearsEve2015
?
decThirtyFirst
? Just
date
?
r
maybe
lastDec2015
? If it makes sense in the context
c
It’s just a warning, but this seems like an area where the variable name is valid, and would be unreadable to remove the underscore. You could add
@Suppress("LocalVariableName")
on the function/class with that variable to document that the name not strictly following Kotlin standards is intentional
✔️ 1
👍 4
As a general rule, I aim to have zero warnings in my build logs. Anytime a warning is logged, I either change it as suggested, or decide that it’s intentional and add an
@Suppress
annotation to hide the warning. Either way, warnings are useful and shouldn’t simply be ignored
👍 2
g
Thanks! I'm going to add a suppression with the comment, "underscore aids readability of this variable name"
e
Copy code
val `dec㉛2015`
/s
s
is this a constant? Maybe it ought to just be
DEC_31_2015
, following the
static final
labeling scheme
r
It’s tough without knowing more of the context, but it might be worth thinking about what that date represents that makes it a logically important constant rather than what date it is. For instance
val COMPANY_FOUNDING_DATE = "2015-12-31".toLocalDate()
captures both that it’s the 31st December 2015 and why that particular date is worth capturing as a constant.
💯 2