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

Class members ordering rule #309

Closed
romtsn opened this issue Nov 4, 2018 · 8 comments
Closed

Class members ordering rule #309

romtsn opened this issue Nov 4, 2018 · 8 comments

Comments

@romtsn
Copy link
Collaborator

romtsn commented Nov 4, 2018

As per kotlin styleguide they recommend to sort the contents of a class with the given order, I would also like to add it to the standard ruleset + formatter. What's your thoughts on this one?

@shashachu
Copy link
Contributor

Let's hold off on this until #208 is implemented. I imagine many developers have their own opinions about class ordering that they wouldn't want touched by ktlint.

@duncte123
Copy link

Are there any updates regarding this issue?

@romtsn
Copy link
Collaborator Author

romtsn commented May 25, 2020

@duncte123 It's on my todo list to implement in the near future, but contributions are welcome of course

@duncte123
Copy link

@duncte123 It's on my todo list to implement in the near future, but contributions are welcome of course

I've looked at adding the rule but I cannot figure out how to make it work unfortunately

@romtsn
Copy link
Collaborator Author

romtsn commented Jun 24, 2020

@duncte123 what exactly you can't figure? You could also drop a message on kotlinlang slack either in the #ktlint channel or directly to me

@duncte123
Copy link

It's most likely that creating this specific rule goes above my skillset when it comes to kotlin

@paul-dingemans
Copy link
Collaborator

This topic is probably way too large / complex to be solved.

Especially autocorrecting problems is nearly impossible. For example regarding the ordering of methods, the coding guidelines specify following:

Do not sort the method declarations alphabetically or by visibility, and do not separate regular methods from extension methods. Instead, put related stuff together, so that someone reading the class from top to bottom can follow the logic of what's happening. Choose an order (either higher-level stuff first, or vice versa) and stick to it.

Perhaps the problem should be sliced into smaller problems that are more likely to be solved. A few examples of such smaller problems would be:

  • Add a (autocorrect) rule to move companion object to the end of the file but. Problem might be to deal with nested classes.
  • Add a (autocorrect) rule to move init blocks before the first (normal) method. Problem might be to deal with init blocks and variable declarations that are related.
  • Add a (report only) rule to find variables declared after any secondary constructor, (normal) method or companion object.

All in all I do understand the request for this change. But I think an auto correct rule can never be achieved as there are too many situations in which the autocorrect would make things worse. So my advice would be to stick to adding a (report only) rule to identify classes which might violate the ordering advised in the Kotlin style guide. It should be easy to disable the rule per class/file in case after manual inspection it is decided that the rule is not applicable for the class/file.

@paul-dingemans
Copy link
Collaborator

This issue should be considered stale. It has been created nearly 4 years ago. More than 1.5 years have been gone, since I lastly wrote about the complexity of this functionality. As no new ideas have popped up, it is time to close the issue and move on.

@paul-dingemans paul-dingemans closed this as not planned Won't fix, can't repro, duplicate, stale Jun 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants