In General, Avoid Keywords
Can you be cute and have a property named public
? Can you be even more cute and have a private
property named public
?
Yes.
Is this a good idea?
Probably not.
Remember that the point behind identifiers is to tell all developers — both your current teammates and “future you” — what the variable, property, function, etc. means. Reusing keywords for this purpose can cause confusion, even if the syntax will be completely legitimate and safe.
Frequently, the keyword makes for a poor identifier anyway. set
is a soft keyword, but you can still use it as a variable name:
fun main() {
val set = setOf(1, 3, 3, 7)
println(set)
}
You might think “well, it is a Set
, so why not call it set
?” However, what it does not convey is what it is a Set
of. Any Set
could be named set
— there is no context for which of your many Set
objects this set
happens to refer to.
Just because the compiler allows certain syntax does not mean that the choice of syntax is good. Write code that is easy to read.
Prev Table of Contents
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.