-
-
Notifications
You must be signed in to change notification settings - Fork 25
Leap Analyzer Issue #260
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
Merged
Merged
Leap Analyzer Issue #260
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a842af8
first push for leap issue
jagdish-15 3734858
Adding new scenario and adding trailing newline
jagdish-15 5d8b7a2
ammending AnalyzerIntegrationTest.java and other test files
jagdish-15 6e9ccf1
Changing approved txt
jagdish-15 2287c87
Removing temporary files
jagdish-15 49717be
Adding trailing newlines
jagdish-15 3da13cd
Logic ammendements for ternary scenario
jagdish-15 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/main/java/analyzer/exercises/leap/AvoidIfStatements.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package analyzer.exercises.leap; | ||
|
|
||
| import analyzer.Comment; | ||
|
|
||
| /** | ||
| * @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/leap/avoid_if_statements.md">Markdown Template</a> | ||
| */ | ||
| class AvoidIfStatements extends Comment { | ||
| @Override | ||
| public String getKey() { | ||
| return "java.leap.avoid_if_statements"; | ||
| } | ||
|
|
||
| @Override | ||
| public Type getType() { | ||
| return Type.ACTIONABLE; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should this be checking if both sides are boolean literals? If only one side is a boolean, I'd assume the other side is some boolean expression (e.g.
(year % 400 == 0) ? true : (year % 100 == 0) ? false : (year % 4 == 0)).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.
Yep, this is checking whether either the
thenorelseexpression is a boolean literal. If either one is, it triggers the action. In the example you shared, theelseexpression is a ternary while thethenis a boolean literal — so the condition still passes because we're checking both sides. But the opposite case (wherethenis a ternary andelseis a boolean literal) is also possible, which is exactly why I made sure to check both!Let me know if I’m thinking in the right direction here.
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.
Sorry, I was trying to figure out whether line 79 should be (notice the
&&instead of||):I noticed the test for this case uses the expression:
But the proposed comment is:
Are we trying to say don't use a ternary expression to just return
true/false(as in the(year % 4 == 0) ? true : falsepart)? I don't think it would make sense if it caught(year % 100 == 0) ? false : (year % 4 == 0)because theelsepart is another expression.Another possibility is that we tell the students that it can be solved with just one ternary expression (instead of saying don't use boolean literals). This could be done by checking if there is more than one ternary expression (in a similar way to how we already check if there are more than 3 checks).
Uh oh!
There was an error while loading. Please reload this page.
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.
Makes sense. We can change this message:
to something like:
But then the title would need to be changed too, right? If it does, let me know what I should rename it to!
I would suggest the title to be something like "avoid multiple ternary".
Additionally, we’d need to update the logic to check that the solution uses only one ternary. The current implementation should still work, but this approach of checking the number of ternary operators makes more sense for the message and improves the overall understandability of the system.
The name of the scenario would also be changed to
UsingMultipleTernaryinstead ofUsingRedundantTernary, which was discussed before.I’ll check this out over the weekend, if you give the go-ahead for this approach!
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.
Also, let me know if I should make these changes in this PR itself or create a follow-up PR for the rest, as @SleeplessByte mentioned!
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.
Yes, that makes sense. Sometimes we do use ternary operators to return boolean literals directly — for example:
(year % 100 == 0) ? false : (year % 4 == 0)Even though this specific case wouldn’t be valid here, it shows that returning boolean literals via ternary operators isn’t always wrong.
So instead of telling students to avoid using ternaries to return boolean literals in general, it’s better to guide them with something more exercise-specific, like saying “this can be solved using just one ternary.” That’s more accurate and helpful in context.
Let me know if I’m on the right track and if this is what you meant!
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.
Yes, to all this! I think the message could be tweaked but we can worry about that in the website-copy PR.
Yes, that's right. I think you can do something like
node.findAll(ConditionalExpr.class)to work out how many ternary usages there are.Yes, I agree we should update the name to match.
The change to "avoid multiple ternary" should be in this PR because this is the PR introduces the boolean literal check that would need to be changed.
Yes, to both!
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.
Sure, I'll make the amendments over the weekend!