-
-
Notifications
You must be signed in to change notification settings - Fork 19
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
base: main
Are you sure you want to change the base?
Leap Analyzer Issue #260
Changes from all commits
a842af8
3734858
5d8b7a2
6e9ccf1
2287c87
49717be
3da13cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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_multiple_ternary.md">Markdown Template</a> | ||
*/ | ||
class AvoidMultipleTernary extends Comment { | ||
@Override | ||
public String getKey() { | ||
return "java.leap.avoid_multiple_ternary"; | ||
} | ||
|
||
@Override | ||
public Type getType() { | ||
return Type.ACTIONABLE; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"comments": [] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Leap { | ||
boolean isLeapYear(int year) { | ||
return (year % 100 == 0) ? (year % 400 == 0) : (year % 4 == 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks great. However, it just occurred to me we might also get an if (year % 100 == 0) {
return year % 400 == 0;
} else {
return year % 4 == 0;
} I think the analyzer will currently raise a comment about using the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On one hand, I understand that it’s just a different way to express the same logic we already added using a ternary operator. But at the same time, it feels like a bit of overkill—especially when the same logic can be written in a single line using a ternary or logical operators. I do have one suggestion—I'm not sure if this is even possible, but just putting it out there: does Exercism support different types of analyzer comments? Like maybe a way to leave a non-critical or informational-only comment for cases like this, where the solution is perfectly valid but there might be a more concise alternative? I’m not sure what the official terminology is (just guessing here), but something like this could be a nice middle ground. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the optional solutions be in a dig deeper article vs the analyzer? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, let's leave it as it is currently.
To answer this one - Yes, there are different types of comments (essential, actionable, informative or celeboratory). The if statement comment is currently "actionable".
There is already an approach for this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Sounds good! But if we do want to address it before the "Dig Deeper" section, one idea is to add an informative comment for optimal We can check if an Let me know what you think and how you'd like me to proceed with this! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jagdish-15 @kahgoh my two cents: leave as is, follow-up PR if necessary, because you probably want a bunch of test cases for that. I do like the "informative" road. We do it a few times in te JS track, giving informative feedback about alternative paths. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I agree with leaving this for now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @SleeplessByte, I agree, we can keep this PR focused on the ternary issue that was raised, and avoid mixing in the |
||
} | ||
} |
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
then
orelse
expression is a boolean literal. If either one is, it triggers the action. In the example you shared, theelse
expression is a ternary while thethen
is a boolean literal — so the condition still passes because we're checking both sides. But the opposite case (wherethen
is a ternary andelse
is 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 : false
part)? I don't think it would make sense if it caught(year % 100 == 0) ? false : (year % 4 == 0)
because theelse
part 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).
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
UsingMultipleTernary
instead 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!