Most beginners write code to make it work. Experienced developers write code to make it readable. Take this simple example in JavaScript: Traditional approach: for (let i = 0; i < arr.length; i++) { total += arr[i]; } Modern approach: const total = arr.reduce((a, b) => a + b, 0); Both produce the same result. But the difference is architectural thinking. The first is imperative programming. You manually control iteration and mutate state. The second is declarative programming. You express intent. The engine handles the mechanics. Why this matters: • Lower cognitive complexity • Better readability • Cleaner abstraction • Reduced mutation • More maintainable codebase Good developers solve problems. Great developers reduce mental load. Code is read far more often than it is written. What do you prefer: traditional loops or functional methods? #JavaScript #WebDevelopment #Programming #CleanCode #SoftwareEngineering #Developers
I see a new definition for clean in coding every day. In what way the second one is more clean? What problem is it solving that the first one is incapable of? Is the number of key strokes a factor for cleanness? Is it more readable? Or is it just a trend to say hay my code is clean so others are dirty don't use them, just like saying red color is more pure because it is the color of blood and life (it is just someone's opinion not a fact).
How about // Sum up all numbers in arr const total = arr.reduce( (a, b) => a + b, 0); Terse AND human readable. Of course we don't need a comment for every single expression, but for a multi-step pipeline it's useful to summarize what the end result of that stage is. Thus the reader can easily follow the system's state in their mental map just by reading the comments.
This is one thing I love about Ruby. It doesn’t get clearer than array.sum
For loop is faster than reduce, I think performance should be part of decision
so 3 lines == 50 lines now?
(38 years coding in many languages) I'd typically see the first as more readable. If we made it more complex, eg. const t = a.reduce( (b,c) => a + e.map( (x) => x**2 + myFunc(x) ), 0) You then have two issues: - step-by-step debugging becomes much harder, because you can't easily put in breakpoints - suddenly, you can no longer easily see the flow. One good point about the .reduce method is you're less likely to get edge cases - because the loop condition is implied by the array length, it's not a separate thing. So in all except simple cases, I'd see the first form as easier to support and read.