-
Notifications
You must be signed in to change notification settings - Fork 92
Ternary operator should not be nested #93
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
Ternary operator should not be nested #93
Conversation
openrewrite#51 add recipe and test
src/main/java/org/openrewrite/staticanalysis/TernaryOperatorsShouldNotBeNested.java
Outdated
Show resolved
Hide resolved
|
Thanks for starting yet another one! Fun to see how you're progressing. :) It's getting a bit late for an in depth review, so I'll keep it at some general comments for now that might help you find similar recipes to copy patterns from. I noticed you're having some excess braces / blocks inserted; while ideally these are not inserted, sometimes you can simplify your logic by inserting them unconditionally, and then cleaning up after yourself by explicitly invoking Good seeing the tests you have already. I think it's fine to start small for instance with return statements only, and expand from there. Any tests we don't cover yet you can then annotate with |
|
The thought to use something to clean the braces had crossed my mind but I thought that would be considered cheating 🙈 Tests are green now but polishing is still required. Will leave this as a draft until and revisit it later this week (or possibly later today). |
src/main/java/org/openrewrite/staticanalysis/TernaryOperatorsShouldNotBeNested.java
Outdated
Show resolved
Hide resolved
|
Maybe we could add a few more tests just to be sure where the choice options are not literal strings. So for instance:
All such cases could require slightly different handling, which could negatively affect either the functioning of code, or preservation of whitespace/comments for readability. |
|
Thanks @pstreef for your contribution! I would like to add to the @timtebeek list, a new test for lambda expressions. It might happen that we should add a new block entry before a new if statement. should be translated to |
Interesting case as well! :) Might be good to add a test that verifies no changes are made in such cases yet; so not |
|
Thanks for these notes/additions. I will take them into account. @rpau I'm doubting if that is really a rewrite everyone would be happy with. Some people prefer to keep their streams brace-less (which is also why you might see ternaries like these pop up) and in stead of adding this in line would expect a method call. So this would be an opinionated approach, which I guess openrewrite is full of, but I'm still doubting which way would be "best" |
|
@pstreef the problem extracting a method is that we can't infer a meaningful method name, and I agree that the strategy might be opinionated. IMO, Skipping these cases for lambda expressions might create the impression that the recipe is not fully working. As a developer, I would like at least to identify those cases and then adapt the code.. extracting the method for instance, but if no changes are made, it is hard to identify if the code is satisfying the rule or not. We can add a boolean parameter to support those cases or not, but as a developer, I believe that we want to identify it to create trust in our recipes. |
src/main/java/org/openrewrite/staticanalysis/TernaryOperatorsShouldNotBeNested.java
Show resolved
Hide resolved
|
@pstreef Sorry about these late review comments. I didn't look carefully enough the first time around. |
src/main/java/org/openrewrite/staticanalysis/TernaryOperatorsShouldNotBeNested.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/staticanalysis/TernaryOperatorsShouldNotBeNested.java
Show resolved
Hide resolved
|
@knutwannheden thx for the detailed review! I'm still fairly new to all this so we can expect there can be some mistakes for anything untested. I'd love your input on this failing test: doReplaceMultiLevelTernariesWithComments |
|
I can't think of any other cases to test. Once we release and deploy it to public.moderne.io I wouldn't be surprised if we discover a few corner cases more, we didn't think of, but that is normal 😄 |
I can take a look at this. But I think we could also integrate this PR as is and fix this problem later. |
…y-operator-should-not-be-nested
…should-not-be-nested
src/main/java/org/openrewrite/staticanalysis/TernaryOperatorsShouldNotBeNested.java
Outdated
Show resolved
Hide resolved
src/test/java/org/openrewrite/staticanalysis/TernaryOperatorsShouldNotBeNestedTest.java
Outdated
Show resolved
Hide resolved
src/test/java/org/openrewrite/staticanalysis/TernaryOperatorsShouldNotBeNestedTest.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Impressive work! I've added a few last minor comments, but trust your judgement in resolving those before a merge.
As a last comment: Perhaps it'd be nice to clearly divide the tests over ~3 nested classes: ifs / simple switch / switch expressions.
move comment
… breaks null safety
|
Regarding the nesting. I made it a bit better for the already nested |
|
Maybe good to also annotated one or two representative tests with |
draft for: #51
Recipe to replace nested ternary statements.