Skip to content

Commit 2e6b670

Browse files
committed
Changing approach names
1 parent fc74587 commit 2e6b670

File tree

9 files changed

+173
-173
lines changed

9 files changed

+173
-173
lines changed

exercises/practice/bob/.approaches/answer-array/content.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Answer array
1+
# answer array
22

33
```java
44
import java.util.function.Predicate;

exercises/practice/bob/.approaches/config.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
"approaches": [
1212
{
1313
"uuid": "6ca5c7c0-f8f1-49b2-b137-951fa39f89eb",
14-
"slug": "if-statements",
15-
"title": "if statements",
16-
"blurb": "Use if statements to return the answer.",
14+
"slug": "method-based-if-statements",
15+
"title": "method-based if statements",
16+
"blurb": "Use if statements to return the answer with the help of methods.",
1717
"authors": [
1818
"jagdish-15"
1919
],
@@ -24,9 +24,9 @@
2424
},
2525
{
2626
"uuid": "323eb230-7f27-4301-88ea-19c39d3eb5b6",
27-
"slug": "nested-if-statements",
28-
"title": "nested if statements",
29-
"blurb": "Use nested if statements to return the answer.",
27+
"slug": "if-statements",
28+
"title": "if statements",
29+
"blurb": "Use if statements to return the answer.",
3030
"authors": [
3131
"bobahop"
3232
]
Lines changed: 56 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,88 @@
11
# `if` statements
22

33
```java
4+
import java.util.function.Predicate;
5+
import java.util.regex.Pattern;
6+
47
class Bob {
5-
String hey(String input) {
6-
var inputTrimmed = input.trim();
7-
8-
if (isSilent(inputTrimmed))
9-
return "Fine. Be that way!";
10-
if (isShouting(inputTrimmed) && isQuestioning(inputTrimmed))
11-
return "Calm down, I know what I'm doing!";
12-
if (isShouting(inputTrimmed))
13-
return "Whoa, chill out!";
14-
if (isQuestioning(inputTrimmed))
8+
9+
final private static Pattern isAlpha = Pattern.compile("[a-zA-Z]");
10+
final private static Predicate < String > isShout = msg -> isAlpha.matcher(msg).find() && msg == msg.toUpperCase();
11+
12+
public String hey(String message) {
13+
var speech = message.trim();
14+
if (speech.isEmpty()) {
15+
return "Fine. Be that way!";
16+
}
17+
var questioning = speech.endsWith("?");
18+
var shouting = isShout.test(speech);
19+
if (questioning) {
20+
if (shouting) {
21+
return "Calm down, I know what I'm doing!";
22+
}
1523
return "Sure.";
16-
24+
}
25+
if (shouting) {
26+
return "Whoa, chill out!";
27+
}
1728
return "Whatever.";
1829
}
19-
20-
private boolean isShouting(String input) {
21-
return input.chars()
22-
.anyMatch(Character::isLetter) &&
23-
input.chars()
24-
.filter(Character::isLetter)
25-
.allMatch(Character::isUpperCase);
26-
}
27-
28-
private boolean isQuestioning(String input) {
29-
return input.endsWith("?");
30-
}
31-
32-
private boolean isSilent(String input) {
33-
return input.length() == 0;
34-
}
3530
}
3631
```
3732

38-
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.
39-
40-
## Explanation
41-
42-
This approach simplifies the main method `hey` by breaking down each response condition into helper methods:
33+
In this approach you have a series of `if` statements using the private methods to evaluate the conditions.
34+
As soon as the right condition is found, the correct response is returned.
4335

44-
### Trimming the Input
36+
Note that there are no `else if` or `else` statements.
37+
If an `if` statement can return, then an `else if` or `else` is not needed.
38+
Execution will either return or will continue to the next statement anyway.
4539

46-
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.
40+
The `String` [`trim()`][trim] method is applied to the input to eliminate any whitespace at either end of the input.
41+
If the string has no characters left, it returns the response for saying nothing.
4742

48-
### Delegating to Helper Methods
43+
~~~~exercism/caution
44+
Note that a `null` `string` would be different from a `String` of all whitespace.
45+
A `null` `String` would throw a `NullPointerException` if `trim()` were applied to it.
46+
~~~~
4947

50-
Each condition is evaluated using the following helper methods:
48+
A [Pattern][pattern] is defined to look for at least one English alphabetic character.
5149

52-
1. **`isSilent`**: Checks if the trimmed input has no characters.
53-
2. **`isShouting`**: Checks if the input is all uppercase and contains at least one alphabetic character, indicating shouting.
54-
3. **`isQuestioning`**: Verifies if the trimmed input ends with a question mark.
50+
The first half of the `isShout` [Predicate][predicate]
5551

56-
This modular approach keeps each condition encapsulated, enhancing code clarity.
57-
58-
### Order of Checks
59-
60-
The order of checks within `hey` is important:
52+
```java
53+
isAlpha.matcher(msg).find() && msg == msg.toUpperCase();
54+
```
6155

62-
1. Silence is evaluated first, as it requires an immediate response.
63-
2. Shouted questions take precedence over individual checks for shouting and questioning.
64-
3. Shouting comes next, requiring its response if not combined with a question.
65-
4. Questioning (a non-shouted question) is checked afterward.
56+
is constructed from the `Pattern` [`matcher()`][matcher-method] method and the [`Matcher`][matcher] [`find()`][find] method
57+
to ensure there is at least one letter character in the `String`.
58+
This is because the second half of the condition tests that the uppercased input is the same as the input.
59+
If the input were only `"123"` it would equal itself uppercased, but without letters it would not be a shout.
6660

67-
This ordering ensures that Bob’s response matches the expected behavior without redundancy.
61+
A question is determined by use of the [`endsWith()`][endswith] method to see if the input ends with a question mark.
6862

6963
## Shortening
7064

71-
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:
65+
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
7266

7367
```java
74-
if (isSilent(inputTrimmed)) return "Fine. Be that way!";
68+
if (speech.isEmpty()) return "Fine. Be that way!";
7569
```
7670

77-
or the body _could_ be put on a separate line without curly braces:
71+
or the body _could_ be put on a separate line without curly braces
7872

7973
```java
80-
if (isSilent(inputTrimmed))
74+
if (speech.isEmpty())
8175
return "Fine. Be that way!";
8276
```
8377

84-
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.
78+
However, the [Java Coding Conventions][coding-conventions] advise to always use curly braces for `if` statements, which helps to avoid errors.
79+
Your team may choose to overrule them at its own risk.
8580

86-
[trim]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim()
81+
[trim]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim()
82+
[pattern]: https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html
83+
[predicate]: https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html
84+
[matcher]: https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html
85+
[matcher-method]: https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#matcher-java.lang.CharSequence-
86+
[find]: https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html#find--
87+
[endswith]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#endsWith(java.lang.String)
8788
[coding-conventions]: https://www.oracle.com/java/technologies/javase/codeconventions-statements.html#449
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
if (isSilent(inputTrimmed))
2-
return "Fine. Be that way!";
3-
if (isShouting(inputTrimmed) && isQuestioning(inputTrimmed))
4-
return "Calm down, I know what I'm doing!";
5-
if (isShouting(inputTrimmed))
1+
if (questioning) {
2+
if (shouting)
3+
return "Calm down, I know what I'm doing!";
4+
return "Sure.";
5+
}
6+
if (shouting)
67
return "Whoa, chill out!";
7-
if (isQuestioning(inputTrimmed))
8-
return "Sure.";
8+
return "Whatever.";

exercises/practice/bob/.approaches/introduction.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ When implementing your solution, consider the following tips to keep your code o
1313
- **Return Statements**: An early return in an `if` statement eliminates the need for additional `else` blocks, making the code more readable.
1414
- **Curly Braces**: While optional for single-line statements, some teams may require them for readability and consistency.
1515

16-
## Approach: `if` statements
16+
## Approach: method-based `if` statements
1717

1818
```java
1919
class Bob {
@@ -50,9 +50,9 @@ class Bob {
5050
}
5151
```
5252

53-
This approach defines helper methods for each type of message—silent, shouting, and questioning—to keep each condition clean and easily testable. For more details, refer to the [`if` Statements Approach][approach-if].
53+
This approach defines helper methods for each type of message—silent, shouting, and questioning—to keep each condition clean and easily testable. For more details, refer to the [method-based `if` Statements Approach][approach-method-if].
5454

55-
## Approach: nested `if` statements
55+
## Approach: `if` statements
5656

5757
```java
5858
import java.util.function.Predicate;
@@ -84,7 +84,7 @@ class Bob {
8484
}
8585
```
8686

87-
This approach utilizes nested `if` statements and a predicate for determining if a message is a shout. For more details, refer to the [nested `if` Statements Approach][approach-nested-if].
87+
This approach utilizes nested `if` statements and a predicate for determining if a message is a shout. For more details, refer to the [`if` Statements Approach][approach-if].
8888

8989
## Approach: answer array
9090

@@ -118,17 +118,17 @@ This approach uses an array of answers and calculates the appropriate index base
118118

119119
## Which Approach to Use?
120120

121-
The choice between the **`if` Statements Approach**, **Nested `if` Statements Approach**, and the **Answer Array Approach** depends on readability, maintainability, and efficiency:
121+
The choice between the **Method-Based `if` Statements Approach**, **Nested `if` Statements Approach**, and the **Answer Array Approach** depends on readability, maintainability, and efficiency:
122122

123-
- **`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.
124-
- **Nested `if` Statements Approach**: This approach can be more efficient by avoiding redundant checks, but its nested structure can reduce readability and maintainability.
123+
- **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.
124+
- **`if` Statements Approach**: This approach can be more efficient by avoiding redundant checks, but its nested structure can reduce readability and maintainability.
125125
- **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.
126126

127127
Each approach offers a balance between readability and performance, with trade-offs in flexibility and clarity.
128128

129129
[trim]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim()
130130
[endswith]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#endsWith(java.lang.String)
131131
[dry]: https://en.wikipedia.org/wiki/Don%27t_repeat_yourself
132-
[approach-nested-if]: https://exercism.org/tracks/java/exercises/bob/approaches/nested-if-statements
132+
[approach-method-if]: https://exercism.org/tracks/java/exercises/bob/approaches/method-based-if-statements
133133
[approach-if]: https://exercism.org/tracks/java/exercises/bob/approaches/if-statements
134134
[approach-answer-array]: https://exercism.org/tracks/java/exercises/bob/approaches/answer-array
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# method-based `if` statements
2+
3+
```java
4+
class Bob {
5+
String hey(String input) {
6+
var inputTrimmed = input.trim();
7+
8+
if (isSilent(inputTrimmed))
9+
return "Fine. Be that way!";
10+
if (isShouting(inputTrimmed) && isQuestioning(inputTrimmed))
11+
return "Calm down, I know what I'm doing!";
12+
if (isShouting(inputTrimmed))
13+
return "Whoa, chill out!";
14+
if (isQuestioning(inputTrimmed))
15+
return "Sure.";
16+
17+
return "Whatever.";
18+
}
19+
20+
private boolean isShouting(String input) {
21+
return input.chars()
22+
.anyMatch(Character::isLetter) &&
23+
input.chars()
24+
.filter(Character::isLetter)
25+
.allMatch(Character::isUpperCase);
26+
}
27+
28+
private boolean isQuestioning(String input) {
29+
return input.endsWith("?");
30+
}
31+
32+
private boolean isSilent(String input) {
33+
return input.length() == 0;
34+
}
35+
}
36+
```
37+
38+
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.
39+
40+
## Explanation
41+
42+
This approach simplifies the main method `hey` by breaking down each response condition into helper methods:
43+
44+
### Trimming the Input
45+
46+
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.
47+
48+
### Delegating to Helper Methods
49+
50+
Each condition is evaluated using the following helper methods:
51+
52+
1. **`isSilent`**: Checks if the trimmed input has no characters.
53+
2. **`isShouting`**: Checks if the input is all uppercase and contains at least one alphabetic character, indicating shouting.
54+
3. **`isQuestioning`**: Verifies if the trimmed input ends with a question mark.
55+
56+
This modular approach keeps each condition encapsulated, enhancing code clarity.
57+
58+
### Order of Checks
59+
60+
The order of checks within `hey` is important:
61+
62+
1. Silence is evaluated first, as it requires an immediate response.
63+
2. Shouted questions take precedence over individual checks for shouting and questioning.
64+
3. Shouting comes next, requiring its response if not combined with a question.
65+
4. Questioning (a non-shouted question) is checked afterward.
66+
67+
This ordering ensures that Bob’s response matches the expected behavior without redundancy.
68+
69+
## Shortening
70+
71+
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:
72+
73+
```java
74+
if (isSilent(inputTrimmed)) return "Fine. Be that way!";
75+
```
76+
77+
or the body _could_ be put on a separate line without curly braces:
78+
79+
```java
80+
if (isSilent(inputTrimmed))
81+
return "Fine. Be that way!";
82+
```
83+
84+
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.
85+
86+
[trim]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim()
87+
[coding-conventions]: https://www.oracle.com/java/technologies/javase/codeconventions-statements.html#449
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
if (isSilent(inputTrimmed))
2+
return "Fine. Be that way!";
3+
if (isShouting(inputTrimmed) && isQuestioning(inputTrimmed))
4+
return "Calm down, I know what I'm doing!";
5+
if (isShouting(inputTrimmed))
6+
return "Whoa, chill out!";
7+
if (isQuestioning(inputTrimmed))
8+
return "Sure.";

0 commit comments

Comments
 (0)