Understanding Nested Iteration: a Key to Success in RB101

Understanding With an Example

Photo by Hafidh Satyanto on Unsplash

Modeling the example in code

Given the following array where each subarray represents a parking meter and each element in the array represents a coin type.

Array Provided
Expected Output

Coding Strategy: Outsource to helper methods

For problems of nested iteration like this there seem to be two main coding strategies:

1) To code it all in one place within a single method

Although there is a certain sense of security that comes from solving a problem within a single method this approach can be problematic with nested iteration since it is difficult to test the different parts of the method as it is being built. Additionally, if an error occurs it is difficult to pinpoint where the error occurred to troubleshoot and remedy it.

2) To outsource functionality to helper methods

Using multiple methods increases the conceptual burden to a certain extent since helper methods must be constructed independently and plugged in. However, for difficult problems with nested iteration the trade off is worth it since it allows for testing on the go as well as simpler troubleshooting. This is the strategy I have elected to demonstrate with this example. This strategy will break down a complex problem into smaller, simpler problems.

Building the main method

A framework for the main method is illustrated in the code snippet below. Within the context of this problem initializing the hash to track the coins and returning it is straightforward (lines 2 and 11 ). Likewise, the outer iteration on line 4 is fairly simple to implement. It’s the nested iteration and subsequent actions within this that are difficult to represent in code. The commented out notes will help bridge the gap between concept and code. It’s apparent that we need a method to tally the coins and that this helper method will interact with both the block parameter parking_meter and the meter_count hash. Simply put those will be the arguments supplied to the helper method.

Main Method Framework

Building the helper method

This is the substantial part of the problem. It’s a good idea to revisit the algorithm and add additional details:

Additional Details for Helper Method
Helper Method Framework
tally_coins! Helper Method Implementation

Testing the Helper Method

A benefit of using a helper method is that this key detail in the solution can be tested independently. This is important since after a successful test one can conclude that the helper method will work as anticipated. This will give a higher level of confidence when working through a complex problem as well as eliminate an option to troubleshoot if things are to go wrong. If something has been verified to work at an acceptable level it cannot be the source of an error. The helper method in this example can be tested independently and apart from any other code. In the code snippet below an empty meter_count is successfully incremented by tally_coins! .

Independent Testing of the Helper Method

Plugging the Helper Method Into the Main Method

It’s as simple as line 5. There is a functional and tested tally_coins! helper method. This helper method performs the nested iteration and operations needed. The method is called for each outer iteration. The code snippet below shows another benefit of using helper methods: simplicity. The code below is much simpler to read and understand than a solution that doesn’t rely on helper methods.

Main Method With Helper Method

Running the Code

At this point one can anticipate the code will produce the correct output because of the benefits of independently testing the helper method. That is the point of being in control of the code: solutions are tested along the way and problems are identified and corrected.

Final Code

Putting It All Together

With nested iteration the difficulty isn’t really with the individual steps in isolation. It’s the interaction of these steps within a nested context that adds a substantial burden of difficulty. Having both a conceptual understanding and functional ability with nested iteration is a key to success in RB101 and beyond. This means having a familiar mental model as well as the capability to apply that model to solve problems in code. I encourage new programmers to experiment with their own mental models and example problems to increase their ability and confidence.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ryan DeJonghe

Ryan DeJonghe

Launch School Student in Ruby Track