Skip to content

Commit e57b2e7

Browse files
authored
Merge pull request #340 from habibasorour/issue_328_listing
Issue 328 fixed: 7.2 listing
2 parents 6f9e628 + ea99928 commit e57b2e7

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

source/ch7_recursion.ptx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,10 @@ public class MTools {
129129

130130

131131
<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. <xref ref="array-sum-python-no-helper" text="type-global"/> shows a Python version.
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>
161-
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+
<xref ref="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 <xref ref="array-sum-java-no-helper" text="type-global"/>.
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>
190-
Here's the improved Python version using a helper method:
194+
<xref ref="array-sum-python-with-helper" text="type-global"/> shows 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):
@@ -229,9 +234,10 @@ main()
229234
</p>
230235

231236
<p>
232-
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 <xref ref="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.
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)