Hacker News new | ask | show | jobs
by thefj 1814 days ago
Another, less serious issue in the memoization example for JS:

  (cache[key] = cache[key] || fn(...args));
Will not memoize results that equal 0 or an empty string.

I guess it goes to show that copying answers from stack overflow might not be the best idea, whether it's done manually or using ML.

2 comments

To be fair, they did market it as paired programming with an AI. When pairing, your partner can make mistakes like this as well.
The entire selling point of pair programming is that your copilot would point out errors like this, not introduce them.

Pairing works when you either pair two strong programmers or pair a strong programmer with a weak one. In the latter case, the main advantage is that it's also a mentoring opportunity.

Copilot has invented a pair programming situation in which your partner constantly introduces dubious code you must painstakingly verify, but is itself incapable of being mentored.

Of course it can be “mentored”, it’s machine learning… I expect this will eventually learn from its users.
Haha, I have to remind myself constantly of the now existing ?? operator.
I think really you'd want to use `key in cache`, because ?? wouldn't let you memoize null or undefined.

Although `in` possibly opens you up to prototype issues. JavaScript is a bundle of fun.

Yeah, Map (or WeakMap if keys are objects and you don't need iteration) is much better at this kind of thing.
Good point.

Collection types are prevalent in languages like Java, but JS devs had to use plain objects for years.

Which is why you needed to build an actual data structure to do this kind of work based off checking for against the prototype chain instead of assuming you can use tiny bits of direct JavaScript operators.