Welcome to the SRP Forum! Please refer to the SRP Forum FAQ post if you have any questions regarding how the forum works.

For Each suggestion

Referring to this example of code:
For Each Field in Array // Processing for each field Next Field
If Array happens to be null, the "For Each" loop will still process the code within the block one time with Field = "". Would it make more sense to skip the block of code if Array = "" in these cases? Just a thought...

Comments

  • I get your point here. I'm guess we could build in a check for an empty list before handling the iteration. However, an empty field within the list of values is a legitimate condition. Thus an empty field should be iterated along with populated fields. Thus, this may come down to a difference of preference on how one wants an empty list to be processed.
  • I agree with you about empty fields. To illustrate my point, I should clarify my code and compare to how I would use a For, Next statement versus a For Each statement.

    // Field is @vm delimited Cnt = Dcount(Field, @vm) For I = 1 to Cnt Value = Field<1, I> // Process through each value of the Field Next I // Using For Each syntax... For Each Value in Field using @vm setting I // Process through each Value of the Field Next Field

    In the For, Next statement, if Field were to equal null, then processing would be ignored because Cnt would be set to the value of zero.

    In the For Each statement, processing occurs even if Field were equal to null. I would expect it to skip the processing where the Field = "".

    If Field were to equal a list of null values (i.e. Field = "":@vm:"":@vm:"") then both would cycle through each null value as expected.
  • I would expect it to skip the processing where the Field = "".

    If Field were to equal a list of null values (i.e. Field = "":@vm:"":@vm:"") then both would cycle through each null value as expected.
    Yep, I get it. But it still boils down to one's expectations (translation: preference) versus some clear rule. While I am inclined to agree with your preference, one could rightly ask why one "" is less valid than two "".

    If explicit rules were intended rather than implicit rules (which I always prefer to eliminate ambiguity in my code), then I would suggest that this is better logic:

    If Field NE '' then // Using For Each syntax... For Each Value in Field using @vm setting I // Process through each Value of the Field Next Field end
  • Let's look at it from the angle of how much work is involved to work around the default behavior.

    The way it is currently, "" is treated as an array with one element that is empty. So, in order to treat "" as empty, we need to do the following:

    If Field NE '' then For Each Value in Field using @vm setting I // Do stuff with Value Next Field end

    Let's say we update the For...Each loop to treat "" as an empty list. Now, when I need to treat "" as a list with one element (because eventually someone will need it), I have to do this:

    If Field EQ '' then // Do stuff with Value end else For Each Value in Field using @vm setting I // Do stuff with Value Next Field end

    Which work around is preferred? The real issue is that "" has ambiguous meaning in BASIC+. There is no truly empty list unless you count unassigned variables.
  • Or perhaps another key word in the precompiler? Something like
    For Each NonNull Value in Field setting I // Do stuff with Value Next Value
  • So, this would skip all empty values in the list? I'll have to think about that for a bit. I'm all for efficiency and productivity, but there's a balance between power and simplicity that I want to maintain. Your example is just one keyword, but when does just one more keyword become too much? It's a philosophical question to be sure. Especially considering that SRP_Clean_Array could handle this scenario with just one more line of code.
Sign In or Register to comment.