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
new check: ConstructorsDeclarationGrouping to check the order of overloaded constructors #14415
Comments
The issue #14273 will be continuation of this issue, updating the google_checks.xml based on the new functionality.
I have already made a PR for adding the new functionality, I just need the maintainers review on that. I have referenced that PR here. I would like to have @romani @nrmancuso opinion on this. |
This feels like it should be its own check, |
@Zopsss @nrmancuso Please make sure the URLs you are using are https://checkstyle.org/ and not sourceforge. The only thing sourceforge supports that our main doesn't still is version history which I think should be fixed.
I am fine with this, but...
Can we explain what these nuances are? I am not seeing anything. While I am generally fine with OverloadedConstructorOrder, I am not sure I see how it will be nothing more than a copy/paste of the existing module. |
@rnveach |
Why we should create a new check:
Regardless of if we extend public class Test {
record R1(int x, int y) {
R1() {
this(0, 0);
}
R1(int x) {
this(x, 0);
}
R1(int x, int y) {
this.x = x;
this.y = y;
}
R1(int x, int y, int z) {
this(x, y + z);
}
}
record R2(int x, int y) {
R2() {
this(0, 0);
}
R2(int x) {
this(x, 0);
}
R2 {
// Compact constructor is equivalent to R2(int x, int y).
// Should this be here? Could it be argued that compact
// constructors should be declared first?
}
R2(int x, int y, int z) {
this(x, y + z);
}
}
} Additional reading: https://docs.oracle.com/javase/specs/jls/se21/html/jls-8.html#jls-8.8 |
We will not be able to approve this issue until we have determined how we should check compact constructors. @Zopsss please share your vision on this. |
@nrmancuso I agree with all the 5 points you mentioned but... R2 {
// Compact constructor is equivalent to R2(int x, int y).
// Should this be here? Could it be argued that compact
// constructors should be declared first?
} for the compact constructor example you mentioned, I researched about it but I did not find any rules saying that we should keep the compact constructor at first before any other constructors. But I guess we should keep that rule in our check, that rule seems pretty obvious to keep as it helps us to initialize the instance variables of record class based on our requirements or conditions so it should be the first constructors so other contributor's can see how the record variables are initialized. I agree to keep that rule, now we only need @romani's approval on this issue. |
@Zopsss if you are in agreement about creating a new check, please update the issue description and title to match. |
@Zopsss we need to extend issue description to show good and bad code, with classes and records to show users clearly what the behavior should be and make sure we have agreement during implementation |
done |
@romani can you approve this issue? |
@Zopsss No one likes the constant pinging and I would consider it rude. Not all of us live in GH to respond to every message every day. This is not the only issue admins are looking at. I always come on to a backlog of 20-30 notifications a day, and most aren't for me. It is more proper etiquette to wait some time for a reply, before thinking this is being missed and ask for a follow up. If you believe you are waiting on only approval at this point, then you can ask (if you can't do it) the issue be assigned to the person you are waiting on to ensure they see the assignment. Otherwise, I recommend you look at other issues or PRs to spend your free time on if you wish. |
so sorry for that, I thought he missed my ping. Can you assign this issue to him? I don't have permission do it |
hey @romani can you review this issue pls? |
What order of ctor we expect? From less parameters to more parameters? I do not think we do this for methods. Did you mean we are going to enforce groupping of constructors and any declaration in between ctor declaration will be violation ? Please make Inputs with violation message exact location and message, you will reuse this code after approval. |
In public class TestMethods {
public void foo(int i) {}
public void foo(String s) {}
public void foo(int i, String s) {}
public enum Hello{ // ok
TEST, TEST2
}
public TestMethods(String a) {} // ok
int a = 10; // ok
interface Testinggg { // ok
}
static class Test2 { // ok
}
public void notFoo() {} // violation
public void foo(String s, int i) {}
} config: <?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
<module name="TreeWalker">
<module name="OverloadMethodsDeclarationOrder"/>
</module>
</module> result:
|
Additionally, in |
I would expect the general behavior of this check to follow whatever Looks like we should also extend the documentation for |
@Zopsss ,
please update example to show where violation is expected by comments, same as we do in our Inputs for test and Example for xdoc. |
Updated the example showing what's allowed and what's not |
Yeah I agree with you |
@Zopsss, please move all to issue description to have single place with all details and examples. I will remove all comments as we finalize discussion, to avoid confusion from next readers of this issue.
It as only single ctor. Not clear why we have violations. |
Done, I have updated the description as we discussed, added more examples and additional context.
The example was showing how the OverloadedMethodsDeclarationOrder works, showing what is allowed and what is not. Our new check will work the same way like this check but for constructors. @romani |
Please update examples to put violations on constructors that are happening after nonctor. Check should violate only constructors. |
Done |
@romani ping is this issue is ready to be approved? or it still needs some changes? |
Why violation? Name of module become a confusion. As we do not care about order and just care about grouping. Let's rename Check |
Me & @nrmancuso thinks that in
You mean |
I didn't think this, I just shared a possibility :)
This does seem reasonable initially, but if we aren't actually checking the order, we should consider the name change. Although, adding ordering probably wouldn't be that difficult if that is something that we wanted to do. Not sure about this one. |
Yes
Yes Ordering might be tricky, as user like logical ordering not a technical ordering. Logical: by reusage, by amount or arguments, as vs description, ... Let's update all examples to do grouping validation only. |
ohkayyy
I have updated the examples, In |
Should be violation full. As all actors should be grouped together.
Please rename to ConstructorsDeclarationGroupping
Are you sure? |
Done! removed that example and the check will follow the rule you suggested.
Done
This was just an idea by Mr. Nick, if you think that it should be in different check then I'm also okay with it. @romani |
Can I start working on this issue now? |
No, until issue is labeled as approved, I do not recommend to start, as you will waste bunch time in reimplementation.
We do not order ctors, we just group them. |
sorry for the late reply... I've removed that example and updated the issue description as well @romani Do we need anything else to change also? |
Please close this issue, and open new issue. It should not have "order" word at all!! I fixed few cases of that word, but we should eliminate it completely. So we will start review of design from scratch, without any history. |
I have downloaded the latest cli from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words
How it works Now:
Checkstyle have a check called OverloadMethodsDeclarationOrder which checks the order of overloaded methods, meaning that it checks if the overloaded methods are grouped together or not, if there is another method between overloaded methods then it'll give you an error. Currently, in checkstyle there is no module to check the order of overloaded constructors in a class, enum, abstract class, records. This new module will check that the overloaded constructors are grouped together or not. If there is another method or anything between the constructors, it will give an error.
How I want check to work
Test.java
Expected output
The module should check the order of overloaded constructors and give error if the constructors are not in order. It should give an error something like this:
Some other examples showing how the check should work
Example 1: This is fine
Example 2: This is fine as we do not required to have overloaded constructors in a specific order like in ascending order based on the number of parameters or something similar to that. We only care about that if the overloaded constructors are grouped together or not, we don't check their order based on number of parameters or anything.
Example 3: This will give error, constructors are not in order
Example 4: This is fine
Example 6: This will give error, constructors are not in an order
Solution
The check should be able to verify that all overloaded constructors are grouped together in a class, enum, records, etc. The overloaded constructors should be strictly grouped together, if there is a method, variables, etc between them then it will give an error.
The text was updated successfully, but these errors were encountered: