DEV Community

Discussion on: Why can't you break out of the forEach loop?

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ • Edited

You can throw an exception. Not the prettiest solution, but it works...

const BreakException = {}
try {
  [1, 2, 3].forEach(function(el) {
    console.log(el)
    if (el === 2) throw BreakException
  });
} catch (e) {
  if (e !== BreakException) throw e
}
Enter fullscreen mode Exit fullscreen mode

Another way is to just use some and return a truthy from your function if you want to break out:

[1, 2, 3].every(function(el) {
   console.log(el)
   if (el===2) return true
})
Enter fullscreen mode Exit fullscreen mode
Collapse
 
lionelrowe profile image
lionel-rowe

Why use an unreadable, hacky solution when for... of exists?

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

Many ways to skin a cat, couldn't be bothered to list them all

Thread Thread
 
lionelrowe profile image
lionel-rowe

What I'm saying is, if you care about code quality, why not use the best tool for the job?

Thread Thread
 
jonrandy profile image
Jon Randy πŸŽ–οΈ • Edited

I care about code. I care about, and am interested in the many permutations that can achieve the same result - including all the weird and wonderful ways. To me, that's the beauty of code, and is why I love it. Correctness, readability, and quality are purely subjective. I personally cannot think of anything worse than blindly following the dogmatic methodologies promoted as the best, or right way to do things - without playing and enjoying exploring alternatives.

Writing code, from my perspective, is more like art crossed with science than engineering. It's a very personal thing - like writing a novel, or poetry, or painting a picture. There can very definitely be beauty in it.

This approach does inevitably clash with others who follow a more traditional software engineering route, but it has served me well over the 38 years I've been writing code.

Thread Thread
 
lionelrowe profile image
lionel-rowe

I sympathize with that approach for sure, but I see it as similar to writing prose, which is certainly a creative endeavor. Sure, you could write an essay caPitALiZIng RAndOM leTtErs or spelΔ±ng uerds uΔ±th ior oun Δ±nventΔ±d orthogr'fy, but who would want to read it, much less be your co-writer on it? There are conventions for a reason, and you should have an even better reason if you want to break them. That doesn't mean for a second that you can never break them β€” it just means that, for example, you should typically avoid adding complexity where you don't gain something (performance, flexibility, etc) of at least equal value in return.

Thread Thread
 
jonrandy profile image
Jon Randy πŸŽ–οΈ • Edited

Have you read Cormac McCarthy's "The Road"? That won a Pullitzer prize, and would probably get you a fail from most school English teachers if you were to submit it, or something similar for a writing assignment.

Most of the best art is made by breaking or testing the rules to their limits. I much prefer to read code that is 'hard to reason about' than code that reads like a class reader for five year olds - it gives me pause to think and exercise my brain, and maybe lend new perspectives on ways to use code.

I think we'll have to agree to disagree

Thread Thread
 
lionelrowe profile image
lionel-rowe

I think we'll have to agree to disagree

That's fair.

Thread Thread
 
mayankav profile image
mayankav

Much appreciation to you both for the exchange of thoughts. I think playing around with the code and conventions is as important as keeping a shared repository clean and conventional. I like how Jon added his perspective to the post and I also agree with Lionel about readability when it comes to code in production. Cheers πŸ₯‚

Collapse
 
mayankav profile image
mayankav

@jonrandy Indeed Jon! This post doesn't cover the possible solutions for a reason. I found it easier to throw in an MDN link which covers some alternatives.