5 ms·
Why Hate the for Loop?
- ajuc 14y agoI, for one, much prefer python generator/list expressions or map/reduce/filter combo of functional languages over regular for loop. At least most of the time. That's because writing for loop (however trivial most of them are) forces me to switch thinking mode from conceptual to implementation. Writing filter(isValid, items) or sum(price(items)) I stay on the higher level. And context switches are bad for my productivity. Besides with for loops you need to come up with an useless name for a variable (hence i convention), and you can easily make off-by-one error (hence standard form (int i=0; i<n; i++). Not a big problems, but still problems, and you don't need to have them in the first place. And last reason - there is a big difference in perceived effort to do sth between writing in-place function invocation (like join(filter(isNotValid, items))), and backing of, declaring temporary variables, writing small for loop, and returning back to the point where you needed your new for loop result. Sometimes the difference is just big enough, that you don't go the extra mile to show your error better, or log more useful debug information, etc. And vertical space is very important in programs. I like my functions to fit into one screen. For loops use 3 or more times as much vertical space as functional equivalents.