DEV Community

Discussion on: 5 Anti-Patterns to Avoid When Working With Collections in JavaScript

Collapse
 
softmantk profile image
NIKHIL CM • Edited

Thanks for this nice article!
What could be the possible solution for the problem no. 1, Prematurely passing functions as direct arguments ?

I could n't perceive. Sorry.

Collapse
 
larrybolt profile image
Larry Boltovskoi

Perhaps an inline function there would be less confusing if it's a one-time, otherwise having a function taking the array as a parameter and output an array as sums, and having a clear name what it does.

const numbers = [[1, 2], [2, 2], [18, 1], [4, 5], [8, 9], [0, 0]]
const numberSums = numbers.map(([a,b]) => a+b)
// [3, 4, 19, 9, 17, 0]
Collapse
 
suraneti profile image
Suraneti Rodsuwan

If an input is a heavy huge array, using simple for-loop or while-loop for me is the best choice for optimized performance.

Collapse
 
vonheikemen profile image
Heiker

I see that one as lack of information. It doesn't matter if you pass the reference to the function or create it inline, the function signature is what is causing the problem. The solution is to search documentation about both function and make sure they are compatible.