-
-
Notifications
You must be signed in to change notification settings - Fork 695
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add method based if approach for Bob (#2861)
Co-authored-by: Kah Goh <[email protected]>
- Loading branch information
1 parent
3b72eac
commit 3ec59e8
Showing
6 changed files
with
191 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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 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
99 changes: 99 additions & 0 deletions
99
exercises/practice/bob/.approaches/method-based-if-statements/content.md
This file contains 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,99 @@ | ||
# Method-Based `if` statements | ||
|
||
```java | ||
class Bob { | ||
String hey(String input) { | ||
var inputTrimmed = input.trim(); | ||
|
||
if (isSilent(inputTrimmed)) { | ||
return "Fine. Be that way!"; | ||
} | ||
if (isShouting(inputTrimmed) && isQuestioning(inputTrimmed)) { | ||
return "Calm down, I know what I'm doing!"; | ||
} | ||
if (isShouting(inputTrimmed)) { | ||
return "Whoa, chill out!"; | ||
} | ||
if (isQuestioning(inputTrimmed)) { | ||
return "Sure."; | ||
} | ||
|
||
return "Whatever."; | ||
} | ||
|
||
private boolean isShouting(String input) { | ||
return input.chars() | ||
.anyMatch(Character::isLetter) && | ||
input.chars() | ||
.filter(Character::isLetter) | ||
.allMatch(Character::isUpperCase); | ||
} | ||
|
||
private boolean isQuestioning(String input) { | ||
return input.endsWith("?"); | ||
} | ||
|
||
private boolean isSilent(String input) { | ||
return input.length() == 0; | ||
} | ||
} | ||
``` | ||
|
||
In this approach, the different conditions for Bob’s responses are separated into dedicated private methods within the `Bob` class. | ||
This method-based approach improves readability and modularity by organizing each condition check into its own method, making the main response method easier to understand and maintain. | ||
|
||
## Explanation | ||
|
||
This approach simplifies the main method `hey` by breaking down each response condition into helper methods: | ||
|
||
### Trimming the Input | ||
|
||
The `input` is trimmed using the `String` [`trim()`][trim] method to remove any leading or trailing whitespace. | ||
This helps to accurately detect if the input is empty and should prompt a `"Fine. Be that way!"` response. | ||
|
||
~~~~exercism/caution | ||
Note that a `null` `string` would be different from a `String` of all whitespace. | ||
A `null` `String` would throw a `NullPointerException` if `trim()` were applied to it. | ||
~~~~ | ||
|
||
### Delegating to Helper Methods | ||
|
||
Each condition is evaluated using the following helper methods: | ||
|
||
1. **`isSilent`**: Checks if the trimmed input has no characters. | ||
2. **`isShouting`**: Checks if the input is all uppercase and contains at least one alphabetic character, indicating shouting. | ||
3. **`isQuestioning`**: Verifies if the trimmed input ends with a question mark. | ||
|
||
This modular approach keeps each condition encapsulated, enhancing code clarity. | ||
|
||
### Order of Checks | ||
|
||
The order of checks within `hey` is important: | ||
|
||
1. Silence is evaluated first, as it requires an immediate response. | ||
2. Shouted questions take precedence over individual checks for shouting and questioning. | ||
3. Shouting comes next, requiring its response if not combined with a question. | ||
4. Questioning (a non-shouted question) is checked afterward. | ||
|
||
This ordering ensures that Bob’s response matches the expected behavior without redundancy. | ||
|
||
## Shortening | ||
|
||
When the body of an `if` statement is a single line, both the test expression and the body _could_ be put on the same line, like so: | ||
|
||
```java | ||
if (isSilent(inputTrimmed)) return "Fine. Be that way!"; | ||
``` | ||
|
||
or the body _could_ be put on a separate line without curly braces: | ||
|
||
```java | ||
if (isSilent(inputTrimmed)) | ||
return "Fine. Be that way!"; | ||
``` | ||
|
||
However, the [Java Coding Conventions][coding-conventions] advise always using curly braces for `if` statements, which helps to avoid errors. | ||
Your team may choose to overrule them at its own risk. | ||
|
||
[trim]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim() | ||
[coding-conventions]: https://www.oracle.com/java/technologies/javase/codeconventions-statements.html#449 |
8 changes: 8 additions & 0 deletions
8
exercises/practice/bob/.approaches/method-based-if-statements/snippet.txt
This file contains 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,8 @@ | ||
if (isSilent(inputTrimmed)) | ||
return "Fine. Be that way!"; | ||
if (isShouting(inputTrimmed) && isQuestioning(inputTrimmed)) | ||
return "Calm down, I know what I'm doing!"; | ||
if (isShouting(inputTrimmed)) | ||
return "Whoa, chill out!"; | ||
if (isQuestioning(inputTrimmed)) | ||
return "Sure."; |
2 changes: 1 addition & 1 deletion
2
.../bob/.approaches/if-statements/content.md → ...s/variable-based-if-statements/content.md
This file contains 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# `if` statements | ||
# Variable-Based `if` statements | ||
|
||
```java | ||
import java.util.function.Predicate; | ||
|
File renamed without changes.