In the comments of the conversation Top 10 ABAP crimes a few of us were discussing the use of an iterators and iterator classes. Below I will add what has already been presented:
I have read about it but I had never used. What I usually do is just encapsulating an internal table inside a class and not modelling table rows as objects (I hope this was clear). In this case I'm not sure if the iterator class can be used and useful.
I have used iterators for particular complex data structures. Underneath was an internal table of references, and I used indexes to keep track. I could of course have looped through the internal table at the higher level instead of using an iterator, but due to the data complexity, I really didn't want to expose the internal table at that level.
I do in complex application (in my latest one I don't use loops except to deal with BAPIRET), because it's the way you code in C# and Java, and that way I can easily translate the code to C#. Basically for large modules I program as if I was coding in C#.
If you use Collections and HashMaps it's the only way to "loop", although I do miss the typed List<T> and HashMap<T,X>.
PS: Actually it's not the only way, there's the get_values_table or something 😊.
I use CL_OBJECT_MAP and CL_OBJECT_COLLECTION which are the equivalent to HashMap and List the problem is that these are not typed lists and so I have to rely on casts that the compiler doesn't check for correctness.
I usually write the type in the description of the return parameter of the method.
- I used Iterator classes heavily in java and C#, not in abap yet. I want to use them again. I relied heavily on array list, hash maps, lists, etc when programming in other languages.
- Restrictions...hmm, in java i can't critique, they are awesome. In abap, the primary hurdle would probably be generics. Abap does generics differently than java but I assume adding a constructor to initially set the type of object of the list would solve that. Adding a check to make sure objects of that type can only be added would be next. Iterator classes can be implemented from anywhere an object can be implement. If I run into any other hurdles i'll let you know.
- Too many, If I ever needed a place to hold multiple objects of the same type I'd use an iterator class. They provide a wide range of functionality for accessing a list of objects, and follows the object oriented design model. From what I was taught at University and experienced in the field, data structures such as link lists are essential.
What is everyone's opinion on iterator classes and the comments above?
Here are some useful links to similar postings: