-
-
Notifications
You must be signed in to change notification settings - Fork 682
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
Adding approach for Bob #2861
base: main
Are you sure you want to change the base?
Adding approach for Bob #2861
Conversation
3ddea06
to
e509381
Compare
if (isAsking(inputTrimmed)) | ||
return "Sure."; | ||
|
||
return "Whatever."; |
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.
To be honest, I think this approach is the same as the existing if-statement
approach. The only difference is that the conditions are in methods instead of variables. Is there something else that differentiates this approach from the other ones?
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.
Thanks @kahgoh for your feedback! I understand your point, and while both the method-based approach and the if
statement approach use condition checks, there are a few key differences that I think set the method-based approach apart:
-
Separation of Concerns:
- In the method-based approach, I’ve separated the logic for determining if the message is a question, shouting, or silent into individual methods (
isYelling()
,isAsking()
,isSilent()
). This makes the mainhey()
method cleaner and focused on handling the flow of responses. - In the
if
approach, the logic is directly embedded in the main method, which could become harder to read and maintain as more checks are added.
- In the method-based approach, I’ve separated the logic for determining if the message is a question, shouting, or silent into individual methods (
-
Improved Readability:
- With the helper methods, it’s clear what each condition is doing without having to dive into complex inline
if
statements. For example,isYelling()
immediately tells you that it’s checking for shouting behaviour, andisAsking()
clarifies the question check. - This helps future readers (and even future maintainers) quickly grasp the purpose of each check.
- With the helper methods, it’s clear what each condition is doing without having to dive into complex inline
-
Maintainability:
- While maintainability may not be a major concern for these one-time exercises, the method-based approach provides a structure that makes it easier to modify individual checks without cluttering the main method. This is generally a good practice for ensuring clean, readable, and maintainable code, even in simple exercises.
-
Scalability:
- While this specific exercise may not need to scale in the future, separating the logic into individual methods makes it much easier to extend or modify if more conditions are added down the line. Even though we don’t expect more checks here, structuring code this way is generally a good practice for scalability in larger, more complex applications.
In summary, while both approaches work, the method-based approach tends to be cleaner, more modular, and easier to extend. It may seem similar on the surface, but the structure I’ve chosen is designed with maintainability and scalability in mind, which are good practices even for small exercises like this one.
If you have any suggestions or further insights on this approach, I'd be happy to hear them!
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, it has taken me quite a while to get back to this one. I think I can see where you are coming from. Looking around, I noticed Python's approach has two similar if
approaches for Bob (if statements and if statements nested). If we were to proceed this approach, perhaps we could do something similar by naming the approach in a similar way to the if statements (for example, if statements with checks in methods).
Btw, looking at the points mentioned under General guidance, there is a point about determining the yelling and questioning once: Use variables for questioning
and shouting
rather than calling these checks multiple times to improve efficiency. In your example solution, you calling the method again, so it is going to be recalculated. But, my worry about putting them in variables is that it will look even closer to the current if statements approach.
I think the way you've currently is fine as it is, but may be worth pointing out this short coming in the introduction's Which approach to use? .
I've updated this approach to incorporate all the suggestions from the PR for the Queen Attack exercise here. This update ensures consistency in style with the other exercises' approaches. Additionally, the newly added approach's content.md file is now aligned with the format and structure of the two existing approaches. Please review and let me know your thoughts on this version. |
if (isAsking(inputTrimmed)) | ||
return "Sure."; | ||
|
||
return "Whatever."; |
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, it has taken me quite a while to get back to this one. I think I can see where you are coming from. Looking around, I noticed Python's approach has two similar if
approaches for Bob (if statements and if statements nested). If we were to proceed this approach, perhaps we could do something similar by naming the approach in a similar way to the if statements (for example, if statements with checks in methods).
Btw, looking at the points mentioned under General guidance, there is a point about determining the yelling and questioning once: Use variables for questioning
and shouting
rather than calling these checks multiple times to improve efficiency. In your example solution, you calling the method again, so it is going to be recalculated. But, my worry about putting them in variables is that it will look even closer to the current if statements approach.
I think the way you've currently is fine as it is, but may be worth pointing out this short coming in the introduction's Which approach to use? .
return "Whatever."; | ||
} | ||
|
||
private boolean isYelling(String input) { |
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.
In the intro and other approaches for this exercise, we have used the term "shouting" instead of "yelling". I'd suggest using the same terminology to keep it consistent within the approaches.
.allMatch(Character::isUpperCase); | ||
} | ||
|
||
private boolean isAsking(String input) { |
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.
Similar to above, we've used the term "questioning" in the intro and other approaches.
|
||
## General guidance | ||
## General Guidance |
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.
I don't think the casing in the headings need changing - there are consistent with most other other approaches. Could you undo the changes to the headings?
|
||
This approach simplifies the main method `hey` by breaking down each response condition into helper methods: | ||
|
||
1. **Trimming the Input**: |
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.
Similar to the previous approaches, we probably should make these subheadings.
1. **Trimming the Input**: | |
### Trimming the Input |
|
||
## Which approach to use? | ||
- **Method-Based**: Clear and modular, great for readability. | ||
- **`if` Statements**: Compact and straightforward, suited for smaller projects. |
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.
I was thinking the recommendation for smaller projects might be unnecessary - if its suitable for smaller projects, why wouldn't it be suitable for mid-size or larger projects?
- **`if` Statements**: Compact and straightforward, suited for smaller projects. | |
- **`if` Statements**: Compact and straightforward. |
pull request
This pull request adds an approach to the Bob exercise.
I was unsure how to generate a
uuid
for the approach, so I've left it empty. I would greatly appreciate guidance on how to generate one.Reviewer Resources:
Track Policies