Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backing property requires other value to be part of public API #2345

Closed
paul-dingemans opened this issue Nov 8, 2023 · 4 comments · Fixed by #2346
Closed

Backing property requires other value to be part of public API #2345

paul-dingemans opened this issue Nov 8, 2023 · 4 comments · Fixed by #2346
Milestone

Comments

@paul-dingemans
Copy link
Collaborator

According to Kotlin Coding conventions:

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:

Currently it is not checked that the variable not starting with _ is a part of the public API. As a result the _elementList property is code below is not reported:

            class Foo {
                private val _elementList = mutableListOf<Element>()

                private val elementList: List<Element>
                    get() = _elementList
            }
@paul-dingemans paul-dingemans added this to the 1.1 milestone Nov 8, 2023
paul-dingemans added a commit that referenced this issue Nov 8, 2023
- Provide better message when the backing property does not have a private modifier
- Check that property correlated with the backing property is public

Closes #2345
@Goooler
Copy link
Contributor

Goooler commented Nov 9, 2023

We can ignore this elementList if it's public or internal?

@paul-dingemans
Copy link
Collaborator Author

We can ignore this elementList if it's public or internal?

I am not sure whether I understand your question. As I read the documentation, a backing property requires a private property of which the name starts with _ and a public property with same name but not starting with _.

@Goooler
Copy link
Contributor

Goooler commented Nov 13, 2023

Yes, I meant if an internal property with same name but not starting with _, should we also ignore this case?

Example:

class Foo {
  private val _foo = "foo"

  internal val foo: String get() = _foo
}

And maybe there is another case like:

class Foo {
  internal val _foo = "foo"

  val foo: String get() = _foo
}

Should we consider bypassing this?

@paul-dingemans
Copy link
Collaborator Author

If we go this route, more exceptions will follow. The end result will be that properties can always start with an _. I think we should as close to the coding conventions as is possible. We can consider to split this rule and move cheking of the backing property to a separate rule, which then can be disabled if a project uses backing properties less strict than the coding conventions.

paul-dingemans added a commit that referenced this issue Nov 15, 2023
- Provide better message when the backing property does not have a private modifier
- Check that property correlated with the backing property is public

Closes #2345
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants