Skip to content

Conversation

@xinri
Copy link

@xinri xinri commented Nov 9, 2025

pull request

Add the design.md file for the calculator-conundrum for the analyzer.

Fixes: #2673


Reviewer Resources:

Track Policies

Copy link
Member

@kahgoh kahgoh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for wanting to contribute @xinri! My thoughts on this one:

  • essential: Verify that the solution is not wrapping all the code in a try catch statement

I agree that it is worth recommending to the student not to wrap all the code in a try catch statement. However, I think this should be actionable because the objective is only to be able to use try catch (which they would've demonstrated). To me, this comment is an improvement. I think a more essential comment would be for something that doesn't meet the exercise's objective (for example, somehow solving this exercise without a try-catch.

  • actionable: If the solution uses an if statement, instruct the student to use the switch case statement instead.

My understanding of this one is that we would also tell to use a switch statement if they use both an if and switch, like this one:

    public String calculate(int operand1, int operand2, String operation) {
        if (operation == null) {
            throw new IllegalArgumentException("Operation cannot be null");
        }

        if (operation.isEmpty()) {
            throw new IllegalArgumentException("Operation cannot be empty");
        }

        int result;
        switch (operation) {
            case "+" -> result = operand1 + operand2;
            case "*" -> result = operand1 * operand2;
            case "/" -> {
                try {
                    result = operand1 / operand2;
                } catch (ArithmeticException e) {
                    throw new IllegalOperationException("Division by zero is not allowed", e);
                }
            }
            default -> throw new IllegalOperationException("Operation '" + operation + "' does not exist");
        }

Did you mean something more like "If the solution does not use a switch statement, instruct the student to use a switch statement"?

  • informative: If the solution does not use HashMap, instruct the student to use the operator as a key and a BiFunction implementing the calculation
    Explain that it is more efficient for performance, and it removes the cyclomatic complexity

I don't understand how or why a HashMap is needed? I thought the students would use switch as in earlier example. I'd expect a switch would be more efficient because Java can turn it into a table, but HashMap requires hashing operations and need to account for keys that hash to the same value. Using a HashMap is also not an objective of this exercise either.

  • informative: If the solution does not throw the exception for Operation cannot be null and Operation cannot be empty at the beginning, instruct the student to do so
    Explain that it is better to fail fast, and it will make the code less error-prone and more readable

I generally agree with this one. The only part I'm not sure about is the readability as that can be a bit subjective.

@xinri
Copy link
Author

xinri commented Nov 10, 2025

Hello @kahgoh,

Thanks for wanting to contribute @xinri

It is a pleasure ;) Thanks for the comments.

Good for me, I will move the try catch comment to the actionable.

I think a more essential comment would be for something that doesn't meet the exercise's objective (for example, somehow solving this exercise without a try-catch.

For the essential, it is quite weird to mention that (no usage of try catch) because you cannot have all the unit tests passed if you don't use the try catch. So the mentee will do the code and will see that the unit test for the try catch part failed and will either search for a mentor or find on the web (most probably chatGPT).

So I will mention these blocking points in the essential but please confirm if what I said is correct.

My understanding of this one is that we would also tell to use a switch statement if they use both an if and switch, like this one:

Sorry indeed, I will put more details about the switch case. You can indeed use the if statement at the beginning. I just want to emphasize the usage of the switch case for the operation part

Did you mean something more like "If the solution does not use a switch statement, instruct the student to use a switch statement"?

correct, only for the part related to the operation.

I don't understand how or why a HashMap is needed? I thought the students would use switch as in earlier example. I'd expect a switch would be more efficient because Java can turn it into a table, but HashMap requires hashing operations and needs to account for keys that hash to the same value. Using a HashMap is also not an objective of this exercise either.

mmmh I was thinking to use this as a potential way to use lambda but it can be quite complex with the try catch, so I will forget about this.

I generally agree with this one. The only part I'm not sure about is the readability as that can be a bit subjective.

Yes, it is subjective but it is also one of the clean code notion from Martin Fowler: https://martinfowler.com/ieeeSoftware/failFast.pdf at least, it is better to inform the mentee that this clean code notion exists. After, it is up to him if he finds it interesting to apply in his code

I find it better to now execute a lot of code to find at the end that if it is null, we have to throw an exception.

@xinri xinri force-pushed the add-analyzer-design-for-calculator-conundrum branch 2 times, most recently from 607f722 to 2cf8ab7 Compare November 10, 2025 20:47
@xinri xinri requested a review from kahgoh November 10, 2025 20:48
Copy link
Member

@kahgoh kahgoh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the essential, it is quite weird to mention that (no usage of try catch) because you cannot have all the unit tests passed if you don't use the try catch. So the mentee will do the code and will see that the unit test for the try catch part failed and will either search for a mentor or find on the web (most probably chatGPT).

So I will mention these blocking points in the essential but please confirm if what I said is correct.

You're right about not needing the comment if there is a test to check for this. However, it is possible to pass the tests without try catch! It can be also be solved using if and making own ArithmeticException. For example:

class CalculatorConundrum {
    public String calculate(int operand1, int operand2, String operation) {
        if (operation == null) {
            throw new IllegalArgumentException("Operation cannot be null");
        }
        if (operation.isEmpty()) {
            throw new IllegalArgumentException("Operation cannot be empty");
        }

        int result = switch (operation) {
            case "+" -> operand1 + operand2;
            case "*" -> operand1 * operand2;
            case "/" -> {
                if (operand2 == 0) {
                    throw new IllegalOperationException("Division by zero is not allowed", new ArithmeticException());
                }
                yield operand1 / operand2;
            }
            default -> throw new IllegalOperationException("Operation '" + operation + "' does not exist");
        };

        return operand1 + " " + operation + " " + operand2 + " = " + result;
    }
}

This certainly exists in the community solutions.

I think the other ones about checking if an exception is thrown if the parameter is null, empty or unknown are covered by the tests though, so I don't think those other comments are needed.

- `essential`: Verify that the solution is throwing an exception if the parameter "operation" is empty
- `essential`: Verify that the solution is throwing an exception if the parameter "operation" is not a mathematical operation.
- `actionable`: If the solution is wrapping all the code in a try catch statement, instruct the student to only use the try catch for the division statement
- `actionable`: If the solution uses only `if` statement, instruct the student that he/she can use the `switch case` statement also.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be worth mentioning where the switch case could be used. For example "... can use the switch case statement to perform the operation".

@xinri
Copy link
Author

xinri commented Nov 11, 2025

It can be also be solved using if and making own ArithmeticException

Indeed It is possible to do something like this ;)

For the null and empty I will remove them.

I think it might be worth mentioning where the switch case could be used. For example "... can use the switch case statement to perform the operation".

You're right. I will update the design.md with some examples.

@xinri xinri force-pushed the add-analyzer-design-for-calculator-conundrum branch from 2cf8ab7 to 41b5e97 Compare November 11, 2025 21:04
@xinri xinri requested a review from kahgoh November 11, 2025 21:04
@xinri xinri force-pushed the add-analyzer-design-for-calculator-conundrum branch from 41b5e97 to f4084d5 Compare November 11, 2025 21:18
Copy link
Member

@kahgoh kahgoh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thanks!

Copy link
Member

@kahgoh kahgoh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, oops, there is lint failure CI:

Error: exercises/concept/calculator-conundrum/.meta/design.md:30:110 MD034/no-bare-urls Bare URL used [Context: "https://martinfowler.com/ieeeS..."] https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md034.md
Error: Failed with exit code: 1

(see also: https://github.com/exercism/java/actions/runs/19278839635/job/55183291934?pr=3046#step:3:11)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

calculator-conundrum: describe Analyzer feedback in .meta/design.md

2 participants