PBP: 074 List Transformation

The last Best Practice suggested using map instead of a for loop to process lists.  There is an exception suggested by the Best Practices; if you’re transforming a list in place, use a for loop.

The reason given for this is twofold; it’s a little more obvious from code that you’re modifying an element of an array instead of assigning a new list to an array, and that you don’t have to create a new list and assign it.  On large operations, the performance can matter, as you don’t have to create and allocate new lists, just update the contents of each one.

I wish I could say, “Well, duh!” to this, but I probably never would have thought of it.  I would miss more cases if I were better at remembering to use map instead of a for loop.

Leave a Reply