Skip to content

Commit 1862bd7

Browse files
committed
added listing tags
1 parent 6f9e628 commit 1862bd7

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

source/ch7_recursion.ptx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ public class MTools {
131131
<p>
132132
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:
133133
</p>
134-
<program xml:id="array-sum-python-no-helper" interactive="activecode" language="python">
134+
<listing xml:id="array-sum-python-no-helper">
135+
<program interactive="activecode" language="python">
135136
<code>
136137
class ArrayProcessor:
137138
def sum_array(self, arr, index):
@@ -156,11 +157,13 @@ def main():
156157
main()
157158
</code>
158159
</program>
160+
</listing>
159161

160162
<p>
161163
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:
162164
</p>
163-
<program xml:id="array-sum-java-no-helper" interactive="activecode" language="java">
165+
<listing xml:id="array-sum-java-no-helper">
166+
<program interactive="activecode" language="java">
164167
<code>
165168
public class ArrayProcessor {
166169
public static int sumArray(int[] arr, int index) {
@@ -182,14 +185,16 @@ public class ArrayProcessor {
182185
}
183186
</code>
184187
</program>
188+
</listing>
185189

186190
<p>
187191
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.
188192
</p>
189193
<p>
190194
Here's the improved Python version using a helper method:
191195
</p>
192-
<program xml:id="array-sum-python-with-helper" interactive="activecode" language="python">
196+
<listing xml:id="array-sum-python-with-helper">
197+
<program interactive="activecode" language="python">
193198
<code>
194199
class ArrayProcessor:
195200
def sum_array(self, arr):
@@ -231,7 +236,8 @@ main()
231236
<p>
232237
Now let's see the improved Java version using a helper method:
233238
</p>
234-
<program xml:id="array-sum-java-with-helper" interactive="activecode" language="java">
239+
<listing xml:id="array-sum-java-with-helper">
240+
<program interactive="activecode" language="java">
235241
<code>
236242
import java.util.Arrays;
237243

@@ -263,6 +269,7 @@ public class ArrayProcessor {
263269
}
264270
</code>
265271
</program>
272+
</listing>
266273

267274
<p>
268275
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

Comments
 (0)