I largely agree with this nodding along to many of the pitfalls presented. Except numbers 2s good refactor. I hope I won’t sound too harsh/picky for an example that perhaps skipped renaming for clarity on the other parts, but I wanted to mention it.
While I don’t use javascript and may be missing some of the norms and context of the lanugage, creating lamda functions (i don’t know the js term) and then hardcoding them into a function is barely an improvement. It’s fine because they work well with
map
andfilter
, but it didn’t address the vague naming. Renaming is refactoring too!isAdult
is a simple function with a clear name, butformatUser
andprocessUsers
are surprisingly vague.formatUser
gives only adultFormattedUser
s, and that should probably be highlighted in the name offormatUser
now that it is a resuable function. To me, it seems ripe for mistaken use given that it is the filter that at a glance handles removing non-adult users before the formatting, whileformatUser
doesn’t appear to exepct only adult users from it’s naming or even use! Ideally,formatUser
should have checked the age on it’s own and set isAdult true/false accordingly, instead of assuming it will be used only on adultUser
s.Likewise, the main function is called
processUsers
but could easily have been something more descriptive likeGetAdultFormattedUsers
or something similar depending on naming standards in js and the context it is used in. It may make more sense in the actual context, but in the example aFormattedUser
doesn’t have to be an adult, so a function processing users should clarify that it only actually creates adult formatted users since there is a case where aFormattedUser
is not an adult.Totally agree. The hardcoded
isAdult: true
repeated in all #2 examples seems like a bug waiting to happen; that should be a property dynamically computed from the age during access time, not a static thing.
I appreciate that this article highlights the value of using of named functions in functional-style code. Too often, programmers assume that “functional programming” means using lambdas everywhere, when in my experience, lambdas are actually a (very mild) code smell.
SEO breaks the web, writer was a hero :)