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: source/ch7_recursion.ptx
+15-8Lines changed: 15 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -129,9 +129,10 @@ public class MTools {
129
129
130
130
131
131
<p>
132
-
First, let's see what happens if we try to write a recursive array sum function <em>without</em> using a helper method. In this approach, the user must provide the starting index, which is awkward and exposes implementation details:
132
+
First, let's see what happens if we try to write a recursive array sum function <em>without</em> using a helper method. In this approach, the user must provide the starting index, which is awkward and exposes implementation details. <xrefref="array-sum-python-no-helper"text="type-global"/> shows a Python version.
This approach has a significant problem, namely that users must remember to start with index 0. Hence, the method signature is cluttered with an implementation detail, and it's easy to make a mistake by passing the wrong starting index. The same awkward pattern appears in Java:
163
+
<xrefref="array-sum-python-no-helper"text="type-global"/>'s approach has a significant problem, namely that users must remember to start with index 0. Hence, the method signature is cluttered with an implementation detail, and it's easy to make a mistake by passing the wrong starting index. The same awkward pattern appears in Java as shown in <xrefref="array-sum-java-no-helper"text="type-global"/>.
public static int sumArray(int[] arr, int index) {
@@ -182,14 +185,16 @@ public class ArrayProcessor {
182
185
}
183
186
</code>
184
187
</program>
188
+
</listing>
185
189
186
190
<p>
187
191
Both versions force users to understand and provide implementation details they shouldn't need to know about. Now let's see how helper methods solve this problem by providing a clean, user-friendly interface. Notice how the public method only requires the array itself, and the hidden recursive logic tracks the current index position.
188
192
</p>
189
193
<p>
190
-
Here's the improved Python version using a helper method:
194
+
<xrefref="array-sum-python-with-helper"text="type-global"/> shows the improved Python version using a helper method.
Now let's see the improved Java version using a helper method:
237
+
The same helper method pattern can be applied in Java, as shown in <xrefref="array-sum-java-with-helper"text="type-global"/>. The public method <c>sumArray</c> provides a clean interface, while the private helper method <c>sumHelper</c> manages the recursion and index tracking.
Compare these improved versions with the earlier problematic ones. Notice how much cleaner the method calls become: <c>processor.sum_array(numbers)</c> in Python and <c>sumArray(numbers)</c> in Java. Users no longer need to worry about providing a starting index or understanding the internal mechanics of the recursion. The helper method pattern creates a clear separation between what users need to know (just pass an array) and the implementation details (tracking the index through recursion).
0 commit comments