“for loop”— and The Pain of Existence
Say you’ve got a bunch-a items in a list,
[4, 1, 3, 2]
for example. If you look at all the reasons you might be using for
loops, you can probably break it down into one of the following.- 1. Filter a specific sub-list from the list. For example we want to pull out all the items less than 3, so that
[4, 1, 3, 2] -> [1, 2]
- 2. Reduce the values into a single one. For example, we want to sum all the items on the list, so that
[4, 1, 3, 2] -> 10
- 3. Map an operation onto each item on the list. For example, we want to double each item, so that
[4, 1, 3, 2] -> [8, 2, 6, 4]
. - 4. Sort the list in some form. For example, in descending form, so that
[4, 1, 3, 2] -> [4, 3, 2, 1]
Oh there are many, many more things that you could be doing in your
Or, well, to be a bit more precise, into one or more of the above buckets!!
for
loops, but the vast majority tends to fall into one of the above filter / reduce / map / sort buckets.Or, well, to be a bit more precise, into one or more of the above buckets!!
And that, my friends, is where The Pain Of Existence comes in. You see, when you run into an issue, what almost immediately happens is that you have to start teasing apart the conflated filter / reduce / map / sort operations (“thisitem got filtered out, but shouldn’t be sorted, but is mapped” etc.).
And of course, that is before you ran into an extremely clever #CowboyDeveloper (hint: not the case), who proceeds to “optimize” the for loop using their knowledge of the data semantics (“the first 30 elements of the list will always be sequential”) without documenting it, and you have no idea why the robot is spinning around in circles…
And of course, that is before you ran into an extremely clever #CowboyDeveloper (hint: not the case), who proceeds to “optimize” the for loop using their knowledge of the data semantics (“the first 30 elements of the list will always be sequential”) without documenting it, and you have no idea why the robot is spinning around in circles…
Mind you, if you’re writing in Erlang or Haskell or some other FP language, the above is almost certainly not an issue that you run into (unless, of course, you are Clever). The thing is though, in this day and age, you can use write anonymous functions, use closures, and do all the other fun stuff that Functional Programmers just take for granted.
So, why aren’t you?Seriously?
Separation of Concerns is a Good Thing, not just at the architectural level, but also at the code level. Tease apart your for loops, so that your operations are simplified, especially from a legibility perspective. You may not care about it right now, but trust me, the person debugging it six months from now will! (And yes, especially if that someone is you!)
Separation of Concerns is a Good Thing, not just at the architectural level, but also at the code level. Tease apart your for loops, so that your operations are simplified, especially from a legibility perspective. You may not care about it right now, but trust me, the person debugging it six months from now will! (And yes, especially if that someone is you!)
Comments