Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/main/java/analyzer/exercises/leap/AvoidIfStatements.java
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import analyzer.Comment;

/**
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/leap/avoid_conditional_logic.md">Markdown Template</a>
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/leap/avoid_redundant_ternary.md">Markdown Template</a>
*/
class AvoidConditionalLogic extends Comment {
class AvoidRedundantTernary extends Comment {
@Override
public String getKey() {
return "java.leap.avoid_conditional_logic";
return "java.leap.avoid_redundant_ternary";
}

@Override
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/analyzer/exercises/leap/LeapAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,15 @@ public void visit(IntegerLiteralExpr node, OutputCollector output) {

@Override
public void visit(IfStmt node, OutputCollector output) {
output.addComment(new AvoidConditionalLogic());
output.addComment(new AvoidIfStatements());
super.visit(node, output);
}

@Override
public void visit(ConditionalExpr node, OutputCollector output) {
output.addComment(new AvoidConditionalLogic());
if (node.getThenExpr().isBooleanLiteralExpr() || node.getElseExpr().isBooleanLiteralExpr()) {
output.addComment(new AvoidRedundantTernary());
}
Copy link
Member

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)).

Copy link
Member Author

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 or else expression is a boolean literal. If either one is, it triggers the action. In the example you shared, the else expression is a ternary while the then is a boolean literal — so the condition still passes because we're checking both sides. But the opposite case (where then is a ternary and else 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.

Copy link
Member

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 ||):

if (node.getThenExpr().isBooleanLiteralExpr() && node.getElseExpr().isBooleanLiteralExpr()) {

I noticed the test for this case uses the expression:

(year % 400 == 0) ? true : (year % 100 == 0) ? false : (year % 4 == 0) ? true : false

But the proposed comment is:

Try returning the conditions directly instead of returning boolean literals (true and false).

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 the else 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).

Copy link
Member Author

@jagdish-15 jagdish-15 Apr 23, 2025

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:

# avoid redundant ternary

Try returning the conditions directly instead of returning boolean literals (`true` and `false`).

to something like:

# avoid redundant ternary

Consider using a single ternary operator for the most efficient solution.

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 of UsingRedundantTernary, which was discussed before.

I’ll check this out over the weekend, if you give the go-ahead for this approach!

Copy link
Member Author

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!

Copy link
Member Author

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!

Copy link
Member

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:

# avoid redundant ternary

Try returning the conditions directly instead of returning boolean literals (`true` and `false`).

to something like:

# avoid redundant ternary

Consider using a single ternary operator for the most efficient solution.

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".

Yes, to all this! I think the message could be tweaked but we can worry about that in the website-copy PR.

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.

Yes, that's right. I think you can do something like node.findAll(ConditionalExpr.class) to work out how many ternary usages there are.

The name of the scenario would also be changed to UsingMultipleTernary instead of UsingRedundantTernary, which was discussed before.

Yes, I agree we should update the name to match.

Also, let me know if I should make these changes in this PR itself or create a follow-up PR for the rest,

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.

Let me know if I’m on the right track and if this is what you meant!

Yes, to both!

Copy link
Member Author

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!

super.visit(node, output);
}

Expand Down
Loading