Functions, lambdas, closures. So high order, nondeclarative, and hot.
Problems
Maintainability
Testability
Code Reuse
Implementation Hiding
Debugging
Solutions
Wrap functions/closures
Reify algorithms in method object / Strategy
Sample Code
Wrong
Right
Detection
- Closures and anonymous functions are very useful to model code blocks, promises etc. So It'd difficult to tear them apart.
Tags
Primitive
Abuser
Conclusion
Humans read code. Software works ok with anonymous functions but maintainability is compromised when multiple closures are invoked.
Relations

Code Smell 06 - Too Clever Programmer
Maxi Contieri ・ Oct 25 '20 ・ 1 min read
Credits
Photo by Roman Mager on Unsplash
Object-oriented programming increases the value of these metrics by managing this complexity. The most effective tool available for dealing with complexity is abstraction. Many types of abstraction can be used, but encapsulation is the main form of abstraction by which complexity is managed in object-oriented programming.
Rebecca Wirfs-Brock
Discussion (2)
Given that "Humans read code", as you said, I find the first example wayyy easier to read than the second. The second example has over 2x as many lines and over 3x as many characters... I don't know if introducing classes was the right solution here, as it adds indirection and complexity. To me, it thus seems that Right is Wrong and Wrong is Right.
To modify the first example so you solve your 5 stated problems, you could simply:
Rename
sortFunction()
→bubbleSort()
.Inside it (the sort function), rename
fn
→compare
, for clarity, so the if sentence reveals the intent:if (compare(arr[j], arr[j+1]))
Name the anonymous function before passing it into the (newly renamed) sort function:
I do agree that anonymous functions can and often are abused, though. But mostly because they are often injected everywhere (typically obfuscating param lists), without the developer having taken the the time to name and declare it (which would simplify the params list, and also reveal the function's intent better).
sortFunction