|
1 | 1 | # Introduction
|
2 | 2 |
|
3 |
| -There are various idiomatic approaches to solve Bob. |
4 |
| -A basic approach can use a series of `if` statements to test the conditions. |
5 |
| -An array can contain answers from which the right response is selected by an index calculated from scores given to the conditions. |
| 3 | +In this exercise, we’re working on a program to determine Bob’s responses based on the tone and style of given messages. |
| 4 | +Bob responds differently depending on whether a message is a question, a shout, both, or silence. |
| 5 | +Various approaches can be used to implement this logic efficiently and cleanly, ensuring the code remains readable and easy to maintain. |
6 | 6 |
|
7 | 7 | ## General guidance
|
8 | 8 |
|
9 |
| -Regardless of the approach used, some things you could look out for include |
| 9 | +When implementing your solution, consider the following tips to keep your code optimized and idiomatic: |
10 | 10 |
|
11 |
| -- If the input is trimmed, [`trim()`][trim] only once. |
| 11 | +- **Trim the Input Once**: Use [`trim()`][trim] only once at the start to remove any unnecessary whitespace. |
| 12 | +- **Use Built-in Methods**: For checking if a message is a question, prefer [`endsWith("?")`][endswith] instead of manually checking the last character. |
| 13 | +- **Single Determinations**: Use variables for `questioning` and `shouting` rather than calling these checks multiple times to improve efficiency. |
| 14 | +- **DRY Code**: Avoid duplicating code by combining the logic for determining a shout and a question when handling shouted questions. Following the [DRY][dry] principle helps maintain clear and maintainable code. |
| 15 | +- **Return Statements**: An early return in an `if` statement eliminates the need for additional `else` blocks, making the code more readable. |
| 16 | +- **Curly Braces**: While optional for single-line statements, some teams may require them for readability and consistency. |
12 | 17 |
|
13 |
| -- Use the [`endsWith()`][endswith] `String` method instead of checking the last character by index for `?`. |
| 18 | +## Approach: method-based `if` statements |
14 | 19 |
|
15 |
| -- Don't copy/paste the logic for determining a shout and for determining a question into determining a shouted question. |
16 |
| - Combine the two determinations instead of copying them. |
17 |
| - Not duplicating the code will keep the code [DRY][dry]. |
| 20 | +```java |
| 21 | +class Bob { |
| 22 | + String hey(String input) { |
| 23 | + var inputTrimmed = input.trim(); |
| 24 | + |
| 25 | + if (isSilent(inputTrimmed)) { |
| 26 | + return "Fine. Be that way!"; |
| 27 | + } |
| 28 | + if (isShouting(inputTrimmed) && isQuestioning(inputTrimmed)) { |
| 29 | + return "Calm down, I know what I'm doing!"; |
| 30 | + } |
| 31 | + if (isShouting(inputTrimmed)) { |
| 32 | + return "Whoa, chill out!"; |
| 33 | + } |
| 34 | + if (isQuestioning(inputTrimmed)) { |
| 35 | + return "Sure."; |
| 36 | + } |
| 37 | + |
| 38 | + return "Whatever."; |
| 39 | + } |
18 | 40 |
|
19 |
| -- Perhaps consider making `questioning` and `shouting` values set once instead of functions that are possibly called twice. |
| 41 | + private boolean isShouting(String input) { |
| 42 | + return input.chars() |
| 43 | + .anyMatch(Character::isLetter) && |
| 44 | + input.chars() |
| 45 | + .filter(Character::isLetter) |
| 46 | + .allMatch(Character::isUpperCase); |
| 47 | + } |
20 | 48 |
|
21 |
| -- If an `if` statement can return, then an `else if` or `else` is not needed. |
22 |
| - Execution will either return or will continue to the next statement anyway. |
| 49 | + private boolean isQuestioning(String input) { |
| 50 | + return input.endsWith("?"); |
| 51 | + } |
23 | 52 |
|
24 |
| -- If the body of an `if` statement is only one line, curly braces aren't needed. |
25 |
| - Some teams may still require them in their style guidelines, though. |
| 53 | + private boolean isSilent(String input) { |
| 54 | + return input.length() == 0; |
| 55 | + } |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +This approach defines helper methods for each type of message—silent, shouting, and questioning—to keep each condition clean and easily testable. |
| 60 | +For more details, refer to the [method-based `if` Statements Approach][approach-method-if]. |
26 | 61 |
|
27 |
| -## Approach: `if` statements |
| 62 | +## Approach: variable-based `if` statements |
28 | 63 |
|
29 | 64 | ```java
|
30 | 65 | import java.util.function.Predicate;
|
@@ -56,7 +91,8 @@ class Bob {
|
56 | 91 | }
|
57 | 92 | ```
|
58 | 93 |
|
59 |
| -For more information, check the [`if` statements approach][approach-if]. |
| 94 | +This approach uses variables to avoid rechecking whether Bob is silent, shouting or questioning. |
| 95 | +For more details, refer to the [variable-based `if` Statements Approach][approach-variable-if]. |
60 | 96 |
|
61 | 97 | ## Approach: answer array
|
62 | 98 |
|
@@ -86,16 +122,22 @@ class Bob {
|
86 | 122 | }
|
87 | 123 | ```
|
88 | 124 |
|
89 |
| -For more information, check the [Answer array approach][approach-answer-array]. |
| 125 | +This approach uses an array of answers and calculates the appropriate index based on flags for shouting and questioning. |
| 126 | +For more details, refer to the [Answer Array Approach][approach-answer-array]. |
| 127 | + |
| 128 | +## Which Approach to Use? |
| 129 | + |
| 130 | +The choice between the **Method-Based `if` Statements Approach**, **Variable-Based `if` Statements Approach**, and the **Answer Array Approach** depends on readability, maintainability, and efficiency: |
90 | 131 |
|
91 |
| -## Which approach to use? |
| 132 | +- **Method-Based `if` Statements Approach**: This is clear and easy to follow but checks conditions multiple times, potentially affecting performance. Storing results in variables like `questioning` and `shouting` can improve efficiency but may reduce clarity slightly. |
| 133 | +- **Variable-Based `if` Statements Approach**: This approach can be more efficient by avoiding redundant checks, but its nested structure can reduce readability and maintainability. |
| 134 | +- **Answer Array Approach**: Efficient and compact, this method uses an array of responses based on flags for questioning and shouting. However, it may be less intuitive and harder to modify if more responses are needed. |
92 | 135 |
|
93 |
| -Since benchmarking with the [Java Microbenchmark Harness][jmh] is currently outside the scope of this document, |
94 |
| -the choice between `if` statements and answers array can be made by perceived readability. |
| 136 | +Each approach offers a balance between readability and performance, with trade-offs in flexibility and clarity. |
95 | 137 |
|
96 | 138 | [trim]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim()
|
97 | 139 | [endswith]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#endsWith(java.lang.String)
|
98 | 140 | [dry]: https://en.wikipedia.org/wiki/Don%27t_repeat_yourself
|
99 |
| -[approach-if]: https://exercism.org/tracks/java/exercises/bob/approaches/if-statements |
| 141 | +[approach-method-if]: https://exercism.org/tracks/java/exercises/bob/approaches/method-based-if-statements |
| 142 | +[approach-variable-if]: https://exercism.org/tracks/java/exercises/bob/approaches/variable-based-if-statements |
100 | 143 | [approach-answer-array]: https://exercism.org/tracks/java/exercises/bob/approaches/answer-array
|
101 |
| -[jmh]: https://github.com/openjdk/jmh |
|
0 commit comments