I'm asking in terms of a loop, obviously
break is important in
switch statements. Whether or not
switch statements themselves are code smells is a separate issue.
So consider the following use cases for iterating a data structure:
- You want to do something to the entire structure (no break needed)
- You want to do something to part of a data structure.
- You want to find something(s) in the data structure (which may or may not involve iterating the entire structure)
The above list seems more-or-less exhaustive to me, maybe I'm missing something there.
Case 1 can be thrown right out, we can use
forEach. Case 2 sounds like
reduce would work. For case 3, needing to iterate the data structure to find something seems plain wrong, either the data structure itself should provide a relevant method or you are likely using the wrong data structure.
I saw this when researching but it is explicitly in the context of C/C++. I could understand how it would be more-or-less a necessity in C, but my understanding is that C++ has lambdas and many data structures are generally objects (e.g.
std::map), so I'm not sure I understand why the answers are all so universally in favor of
break there, but I don't feel I know C++ well enough to even begin to comment.
I also realize that for some corner-case exceedingly large data structure the cost of iterating the entire structure may be unacceptably high, but I doubt those are very common when working even in node.js. Certainly it's the kind of thing you'd want to profile.
So I just don't see a use-case for