Skip to content

Commit

Permalink
Chnaging the introduction.md for fixing styling errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jagdish-15 committed Nov 18, 2024
1 parent ad27bb2 commit 6def244
Showing 1 changed file with 0 additions and 57 deletions.
57 changes: 0 additions & 57 deletions exercises/practice/bob/.approaches/method-based/content.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,5 @@
# Method-Based Approach

<<<<<<< HEAD
=======
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.

The main `hey` method determines Bob’s response by delegating each condition to a helper method (`isSilent`, `isYelling`, and `isAsking`), each encapsulating specific logic.

## Explanation

This approach simplifies the main method `hey` by breaking down each response condition into helper methods:

1. **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.

2. **Delegating to Helper Methods**:
Each condition is evaluated using the following helper methods:

- **`isSilent`**: Checks if the trimmed input has no characters.
- **`isYelling`**: Checks if the input is all uppercase and contains at least one alphabetic character, indicating shouting.
- **`isAsking`**: Verifies if the trimmed input ends with a question mark.

This modular approach keeps each condition encapsulated, enhancing code clarity.

3. **Order of Checks**:
The order of checks within `hey` is important:
- Silence is evaluated first, as it requires an immediate response.
- Shouted questions take precedence over individual checks for yelling and asking.
- Yelling comes next, requiring its response if not combined with a question.
- Asking (a non-shouted question) is checked afterward.

This ordering ensures that Bob’s response matches the expected behavior without redundancy.

## Code structure

>>>>>>> e67fc434c32715ee323ebc5079ef0497932738f6
```java
class Bob {
String hey(String input) {
Expand Down Expand Up @@ -69,7 +35,6 @@ class Bob {
}
```

<<<<<<< HEAD
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
Expand Down Expand Up @@ -115,26 +80,4 @@ if (isSilent(inputTrimmed))
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()
=======
## Advantages of the Method-Based Approach

- **Readability**: Each method is clearly responsible for a specific condition, making the main response logic easy to follow.
- **Maintainability**: Changes to a condition can be confined to its method, minimizing impacts on the rest of the code.
- **Code Reusability**: Helper methods can be reused or adapted easily if new conditions are added in the future.

## Considerations

- **Efficiency**: While this approach introduces multiple method calls, it enhances readability significantly, which is often more valuable in non-performance-critical applications.
- **Error Prevention**: This approach avoids redundant code, reducing the risk of maintenance errors.

## Shortening Condition Checks

If each `if` statement body is only a single line, braces can be omitted, or the test expression and result could be placed on a single line. However, [Java Coding Conventions][coding-conventions] recommend always using curly braces for error prevention and easier future modifications.

### Alternative: Inline Helper Methods

For smaller projects, consider implementing helper methods inline or as lambdas, though this might reduce readability.

[trim]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim()
>>>>>>> e67fc434c32715ee323ebc5079ef0497932738f6
[coding-conventions]: https://www.oracle.com/java/technologies/javase/codeconventions-statements.html#449

0 comments on commit 6def244

Please sign in to comment.