You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: exercises/practice/bob/.approaches/introduction.md
+9-6Lines changed: 9 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,8 @@
1
1
# Introduction
2
2
3
-
In this exercise, we’re working on a program to determine Bob’s responses based on the tone and style of given messages. Bob responds differently depending on whether a message is a question, a shout, both, or silence. Various approaches can be used to implement this logic efficiently and cleanly, ensuring the code remains readable and easy to maintain.
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.
4
6
5
7
## General guidance
6
8
@@ -50,9 +52,10 @@ class Bob {
50
52
}
51
53
```
52
54
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].
55
+
This approach defines helper methods for each type of message—silent, shouting, and questioning—to keep each condition clean and easily testable.
56
+
For more details, refer to the [method-based `if` Statements Approach][approach-method-if].
54
57
55
-
## Approach: `if` statements
58
+
## Approach: variable-based `if` statements
56
59
57
60
```java
58
61
importjava.util.function.Predicate;
@@ -85,7 +88,7 @@ class Bob {
85
88
```
86
89
87
90
This approach uses variables to avoid rechecking whether Bob is silent, shouting or questioning.
88
-
For more details, refer to the [method-based `if` Statements Approach][approach-method-if].
91
+
For more details, refer to the [variable-based `if` Statements Approach][approach-variable-if].
89
92
90
93
## Approach: answer array
91
94
@@ -122,7 +125,7 @@ This approach uses an array of answers and calculates the appropriate index base
122
125
The choice between the **Method-Based `if` Statements Approach**, **Nested `if` Statements Approach**, and the **Answer Array Approach** depends on readability, maintainability, and efficiency:
123
126
124
127
-**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.
125
-
-**`if` Statements Approach**: This approach can be more efficient by avoiding redundant checks, but its nested structure can reduce readability and maintainability.
128
+
-**Variable-Based `if` Statements Approach**: This approach can be more efficient by avoiding redundant checks, but its nested structure can reduce readability and maintainability.
126
129
-**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.
127
130
128
131
Each approach offers a balance between readability and performance, with trade-offs in flexibility and clarity.
@@ -131,5 +134,5 @@ Each approach offers a balance between readability and performance, with trade-o
0 commit comments