If a class has two properties which are conceptually the same but one is part of a public API and another is an implementation detail, use an underscore as the prefix for the name of the private property:
When a backing property is needed, its name should exactly match that of the real property except prefixed with an underscore. Android Kotlin style guide is less explicit:
When a backing property is needed, its name should exactly match that of the real property except prefixed with an underscore.
Several users have requested to loosen the restrictions regarding the modifier of the base property:
property-naming rule should ignore properties for which the name starts with _ as this will be handled by backing-property rule
backing-property rule will be identical to logic which is currently implemented in property-naming in case of intellij_idea or ktlint_official code styles. In case of android_studio code style, the modifier is ignored.
The
property-naming
also contains logic to check backing properties.Kotlin Coding Conventions clear states that:
When a backing property is needed, its name should exactly match that of the real property except prefixed with an underscore. Android Kotlin style guide is less explicit:
Several users have requested to loosen the restrictions regarding the modifier of the base property:
As Originally posted by @paul-dingemans in https://github.com/pinterest/ktlint/issues/2345#issuecomment-1808943854 the solution could be to split the
property-naming
rule and extract part of the logic to a new rulebacking-property
. Requirements:property-naming
rule should ignore properties for which the name starts with_
as this will be handled bybacking-property
rulebacking-property
rule will be identical to logic which is currently implemented inproperty-naming
in case ofintellij_idea
orktlint_official
code styles. In case ofandroid_studio
code style, the modifier is ignored.