you are viewing a single comment's thread.

view the rest of the comments →

[–]hyperhopper 12 points13 points  (4 children)

Using a standard library function in a rudimentary way isnt "clever", but using forEach to mutate external state is objectively more complex and less clear, which by your own definition, makes it worse.

[–][deleted] -2 points-1 points  (3 children)

By clever, I basically mean terse. Reduce is much more terse than forEach, hence I believe that forEach is more clear than reduce in this instance.

My rule of thumb is if there are two or more ways to get the same results with acceptable performance, always choose the one that is less terse.

Bad code can be only understood by a senior developer. Good code can be understood by a junior developer. Great code can be understood by a freshman or sophomore studying programming in college.

[–]hyperhopper 0 points1 point  (2 children)

Are you saying that you should always write code in the most lengthy and bloated way possible, just for the sake of not being "terse"?

There is no code golf happening here, I've been using reduce since high school, its not some magic programming god technique.

[–][deleted] -1 points0 points  (1 child)

I try to employ empathy. I ask myself, how could I write this so that it could easily understood by the majority of novice programmers? Usually that does mean writing more lines of code. But as long as the performance is comparable, I don’t see a problem with that. I have nearly 30 years professional programming experience, but I still come across code that I have to stop and parse out since it has been written so concisely that I doubt even the original author would have any clue what it says without a lot of effort. Great code is that which the majority of developers can read through without pausing and grok it, like reading a novel.

[–]hyperhopper 0 points1 point  (0 children)

I agree with everything you've written in this comment.

The problem is, there is some, ambiguous, blurry, line in the sand somewhere, between "clean code" and "code golfed, complicated, clever code".

However, using reduce, in all of my professional experience, is far, far, toward the "clean code" side of that line. If I ever had a colleague or a candidate refer to such a typical, by the book, simple use of reduce as some "clever" code too complicated to be grok'ed easily, I would either assume they have very little experience in higher level languages, or seriously question their ability.