Feeds

To iterate is human

Iterator versus the Enumeration Method

Maximizing your infrastructure through virtualization

Enumeration Method is a general design pattern, not just a language-specific idiom. In the right situation, it offers a number of significant benefits. For example, if you have a relatively stable set of actions you want to perform during an iteration, the fact that a language does not support closures becomes less of an issue. Passing blocks as objects is particularly effective when iteration is ad hoc but, when the kinds of loop action you have are well characterised and bounded, common actions can be wrapped up and provided as predefined Command classes.

Alternatively, when traversing aggregate data structures holding different kinds of elements, such as objects that represent the syntax structure of source code or a document, Enumeration Method with a Visitor interface offers a far simpler programming model than working multiple nested loops, multiple Iterator types and explicit runtime type checking. It is also much easier to write unit tests against such structures by passing Mock Objects to the Enumeration Method.

But what of the original observation that kicked off this article? If you recall:

There are many ways to offer traversal, but if the caller needs to be able to know the position of elements in some way there are essentially only three general designs that keep the collection's internal representation hidden from the caller.

Whenever you mention magic numbers — three in this case — you implicitly invite others to check your working (in a discipline where we don't do nearly as much reviewing and checking as we should, this is no bad thing). The claim is that I was off by one and that, including Enumeration Method (or Internal Iterator), there are four. Given that I seem to be quite a fan of Enumeration Method, have written about it going back many years, and even mention it in the very same article, is this correction correct? Not quite, and it is worth understanding why.

Had the problem to be solved just been how to provide some mechanism for traversal that kept the collection's internal representation hidden from the caller, three would have been right out. In fact, so would four. Here's a summary of the three approaches from the previous article:

First, it is possible to use an index into the collection. [...] The second approach is to internalise a cursor within the collection. [...] And the third option is to introduce an Iterator object.

And then add Enumeration Method. And then add Batch Method. And then you can choose whether or not to count separately variations of these or combinations that draw on more than one approach, such as Batch Iterator (also known as Chunky Iterator).

However, the phrasing of the design issue was more constrained:

... if the caller needs to be able to know the position of elements in some way...

This constraint is important because not only does it subset the available solutions, it also highlights a significant distinction between Iterator and Enumeration Method. While many choices are driven by idiom and context, keep in mind that the detail of the problem being solved casts an important vote (or veto) in choosing an appropriate solution.

In this particular case, one of Enumeration Method's strengths — that of fully encapsulating any concept of position and iteration mechanism from the caller — is a mismatch for the problem we want to solve. What is needed are solutions that have the property of both traversal and persistent position, hence the inclusion of Iterator and the exclusion of Enumeration Method.

So, to summarise, Iterator supports traversal by encapsulating position and offering traversal control whereas Enumeration Method supports traversal by encapsulating the whole loop and all of its associated mechanisms. Specific requirements and general context help to determine which is the more appropriate solution in a given situation. ®

Reducing security risks from open source software

More from The Register

next story
HIDDEN packet sniffer spy tech in MILLIONS of iPhones, iPads – expert
Don't panic though – Apple's backdoor is not wide open to all, guru tells us
Do YOU work at Microsoft? Um. Are you SURE about that?
Nokia and marketing types first to get the bullet, says report
Microsoft takes on Chromebook with low-cost Windows laptops
Redmond's chief salesman: We're taking 'hard' decisions
Cheer up, Nokia fans. It can start making mobes again in 18 months
The real winner of the Nokia sale is *drumroll* ... Nokia
EU dons gloves, pokes Google's deals with Android mobe makers
El Reg cops a squint at investigatory letters
Chrome browser has been DRAINING PC batteries for YEARS
Google is only now fixing ancient, energy-sapping bug
prev story

Whitepapers

Seven Steps to Software Security
Seven practical steps you can begin to take today to secure your applications and prevent the damages a successful cyber-attack can cause.
Consolidation: The Foundation for IT Business Transformation
In this whitepaper learn how effective consolidation of IT and business resources can enable multiple, meaningful business benefits.
Designing a Defense for Mobile Applications
Learn about the various considerations for defending mobile applications - from the application architecture itself to the myriad testing technologies.
Build a business case: developing custom apps
Learn how to maximize the value of custom applications by accelerating and simplifying their development.
Consolidation: the foundation for IT and business transformation
In this whitepaper learn how effective consolidation of IT and business resources can enable multiple, meaningful business benefits.