16 ms·
Don't use functions as callbacks unless they're designed for it
- deckard1 6y agoFirst-class functions considered harmful. Of course it's JavaScript. The inmates are not only running the asylum, they built it too.
- speedgoose 6y agoI would recommend using eslint and the following rule : https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/no-array-callback-reference.md https://github.com/sindresorhus/eslint-plugin-unicorn/blob/m... eslint-plugin-unicorn has a lot of great rules, some are opinionated but you don't have to use all the rules.
- uallo 6y agoYes, that annoys me every time I see it. One other example that is often used is `someArray.filter(Boolean)`. While that will likely never break, it absolutely could. Thanks for writing this. It's bookmarked and will share it with my team when necessary.
- ragnese 6y agoHow could it break? If `Boolean` (which is basically just a casting function, right?) changed its signature? That does sound very unlikely.
- teddyh 6y ago…in JavaScript.
- leafario2 6y agoI am not sure this really has to do with callbacks - isn't this more due to the dangers and hassle of allowing a function with 3 arguments to be called with just one?
- choeger 6y agoAbsolutely.
- brailsafe 6y agoI gathered that it's the danger of allowing a function with 1 parameter to be called with 3 arguments, but I think you're right too.
- Twisell 6y agoIt's actually quite the opposite, in this case JS is allowing a function with one argument to be called with two extra argument without complaining. In my view it's ways worse. Optionals arguments (ideally named and with default values) are quite easily dealt with and the developer is aware he'll break compatibility if he touch them. No error on extra argument is another flavor of madness.
- dsego 6y agoYou can still have a signature mismatch if you're not sending the correct args. The point is to not pass the function to map directly but use the more explicit arrow function.
- philipyoungg 6y agoI've been bitten by this multiple times.
- craigds 6y ago*in Javascript. In most other languages where unwanted extra arguments raise an error instead of being silently ignored, this mostly isn't a problem
- choeger 6y agoIn a language that actually tries to help you creating error-free code, a type check would prevent this. This is really just a wonderful example how javascript is a mental burden to the programmers instead of a useful tool.
- dsego 6y agoHow would a type check prevent this?
- choeger 6y agoA type check would give map a type, say [a] -> (int -> a -> b) -> [b]. If you try to apply it to any function that does not have the type (int -> a -> b), the check would fail.
- username90 6y agoSo basically any language that does any form of type checking at all.
- MaxBarraclough 6y agoNot including TypeScript.
- xsmasher 6y agoJust to clarify, if you update toReadableNumber to take a second parameter and that parameter is not a number, typescript will complain.* TypeScript won't catch the original landmine because it ignores the extra parameters; maybe some linter would? Is there a rule that enforces "functions used by map must spell out all the parameters?" * Example of typescript giving an error on toReadableNumber_v2: https://www.typescriptlang.org/play?#code/FAYw9gdgzgLgBAQwE5IQTzgXjgbQIwA0ATAQMwC6A3MDQGYCuEIMAlpHDGAEoCmCAJggBGAGx4A5egFshPJAAoI0gFxKZcgJQBvYHD1wkPGPSQQ4agHScAYiwAePfvKIbqAXxrAGTVu068BYTFJdSQAfQA3IkUVNVkkAjgAB0Nae2VYJBYIAHNtXX1DY1Nk1Ps4AGpzaSswWwcnF3cacGgwMQsRMBz5RBR0CykEJN7-PkFRCWl4uA1Z6hbIKHaeTu7e5FQ0QeHR7nGgqdDIoln5miA https://www.typescriptlang.org/play?#code/FAYw9gdgzgLgBAQwE5...
- hoppla 6y agoconst readableNumbers = someNumbers.map((item, _, _) => toReadableNumber(item));
- franciscop 6y agoWhy not just: const readableNumbers = someNumbers.map(item => toReadableNumber(item));
- ulucs 6y agoAnother way to frame this for library developers: adding parameters is a breaking change in javascript, and it should be noted in changelogs as such.
- globular-toast 6y agoYeah. Pretty horrible design flaw. Being able to add parameters in a backwards compatible way is essential for software development. I guess the only way to do it is via passing an object, which is like passing keyword parameters in other languages.
- tetha 6y agoOr you end up with java-like names once you forgot the option object. toReabableNumber, toReadableNumberWithBase, toReadableNumberWithBaseAndPrecision, .... or I guess toReabableNumberWithOptions.
- simongray 6y agoIsn't that more Objective-C style?
- wruza 6y agoAaand in objc, it would traditionally be: [array mapSelector:@selector(toReadableNumber:withOptions:) fromObject:[NSFormattingManager localizedFormattingManager] withArguments:@[@{kCFNumberFormattingBaseKey:@(10)}]]; Thank god they invented @-syntax for core type literals.
- hawk_ 6y agouhm java has method overloading..
- vorticalbox 6y agoSo does javascript.
- mattmanser 6y agoHaha, someone was blaming 'typescript' after they'd made this exact mistake in Ask HN : https://news.ycombinator.com/item?id=26039826 https://news.ycombinator.com/item?id=26039826 Strange coincidence. Edit: I always wondered if jquery's .each deliberately had a signature of function(i, ele) to discourage people from mistakes like this, or if it was a happy accident.
- brailsafe 6y agoYou could derive a decent interview question out of that. Not the specific output of the bad call to parseInt, but simply "What arguments can you pass into parseInt?", and then simply ask about why it would be bad to pass as a callback to map. I'd probably have tripped up on the latter having not read this post, because I'd have never thought to not explicitly pass the radix to parseInt and rarely use anything but anonymous functions in a map call. Though on second thought, maybe not a great question, hard to say. I know I've tripped up on the std sort function having not had used it in a while.
- joantune 6y agoHmm.. haven't coded in it for a while, but wouldn't typescript solve this?! Why are people still using vanilla JS?!
- throw14082020 6y agoPerhaps you should read the article
- joantune 6y agoOops, definitely
- deleted 6y ago[deleted]
- dep_b 6y agoHopefully I can avoid stacks tainted by this kind of unnecessary “flexibility” until the end of my programming career. Typescript was considered the “fix all problems with JavaScript” language but I poked in its type system within the first 10 minutes of using it.
- notapenny 6y agoThe thing is, most of that flexibility or any of the things that could be considered quirks/flaws of the language have been around for a long time and probably will be due to browser compatibility issues. It isn't great, but it is what it is. I enjoy TS and for web applications I wouldn't want to do without it, but it for sure isn't the "end all" since it still needs to work with those issues. Ultimately though in the case of some of the examples, a type system in a language with optional parameters doesn't excuse you from having to use your brain. Especially if you're the kind of developer who thinks testing your code is just unnecessary.
- ragnese 6y agoYeah, TypeScript sounds great, but I quickly became a skeptic. JavaScript is a lost cause. There cannot be a JavaScript++ that will be reasonably safe. I'm not a frontend guy, but I truly don't know what I'd do with a frontend project. ClojureScript? Elm? OCaml? I would even do JavaScript with JSDoc comments before I'd bother doing TypeScript.
- MaxBarraclough 6y agoWhat do you make of Dart?
- ragnese 6y agoGood question. I've only seen cursory information about Dart, so I can't really have an opinion. I'd be willing to try it, though. From a quick web search, it looks like JavaScript interop isn't totally frictionless, which is probably a good thing to- as crazy as that may sound. But it looks like it does rely on making up a type signature for the JS you call into. I assume there must be a way to use TypeScript signature files, too. It's interesting. I'd like to look into it some time.
- franciscop 6y agoAgreed 100%. As a library author I'd also recommend if you create functions to make way for this specific case. It's just very useful to be able to `.map()` so allowing people to do it is a net gain, I've done so in my library `files` (https://documentation.page/github/franciscop/files https://documentation.page/github/franciscop/files): > Ignores the second parameter if it's not an object so you can work with arrays better like .map(read) It's also pretty simple to implement: // Assuming we are taking an options object {} export const myFunc(arg1, arg2 = {}) => { if (typeof arg2 === 'number') arg2 = {}; // ... rest of the code as usual }; So, library authors, do a favour to users and in those functions that could be used as a callback, add this option. Some other tips/niceties I've learned over the years: - For highly async libraries, allow to accept an unresolved promise. It's pretty safe and easy to do on the library-side, and will probably remove a bug or two on your users' side. - Export default and named so the users don't need to worry about whether to `import * as files from` or `import files from` (assuming the library is small and there's no concern about tree-shaking). - I also use a higher-order promise abstraction I created, Swear (https://documentation.page/github/franciscop/swear https://documentation.page/github/franciscop/swear), but that's totally optional and you can treat any of my async libraries as normal Promises.
- joduplessis 6y agoGood article, but like - is this really a problem or even a pattern people use?
- dsego 6y agoI don't think so, maybe before JS had arrow functions. Now you would usually have an anonymous arrow function as a callback to explicitly state the calling contract. Then inside it apply the original function.
- joshxyz 6y agoYeh, first thought when I saw it was "who does this lol"
- spinningarrow 6y agoYes it is (JavaScript is so widely used that probably every pattern is in use somewhere. I myself have done this many times and seen it being done as well!)
- notapenny 6y agoIn my experience yes and I use it myself as well, but with care. Optional parameters are a thing so when you're doing something like the author describe, you need to consider that your function is quietly receiving these parameters but just discarding them. I see people using .filter(Boolean) a lot for example. If the signature of that were to change, for sure it wouldn't be something that quietly gets implemented, but I wouldn't pass formatting functions to these operations carelessly, especially in a codebase where there may not be tests. Some of the safer ways I've seen are use of unary helper functions to wrap the callback, or having the callback actually take the arguments but discarding them like (element, _index, _array). At least that way you communicate some intent.
- ufo 6y agoThe parseInt bug that's mentioned in thd article is very likely to catch you eventually if you use Javascript for long enough. The habit that I developed as a response to that is to always pass a lambda to the callback. That is, write this: foo(x => f(x)) instead of foo(f)
- kleiba 6y agoI don't quite buy it. If a library gets a update that is not backward-compatible, it does not matter whether you previously called that function as a callback or as direct invocation: your code may break one way or the other.
- dsego 6y agoYes, but here the argument is to not blindly apply just any function to a map since the signatures won't necessarily match. It's harder to spot the errors and is just technically wrong, even if it does work in that particular situation.
- XelNika 6y agoThat much is obvious. The blog post has an example of something that appears to be non-breaking (at least it would be in many other languages), but actually does break things. Both the library creator and user messed up in the example given, but if you're not intimately familiar with the language, both mistakes seem reasonable.
- kleiba 6y agoThe keyword here is appear, but in reality it wasn't a non-breaking change.
- hvdijk 6y agoAny update is going to be backwards incompatible to some extent, even a change to the version number can cause and has caused breakage in software that has buggy version checks. Because of that, taking backwards compatibility as an absolute is not useful, and instead we generally limit it to where the old version was being used correctly in supported ways. Whether calling a function with more arguments than it is specified to take is supported will depend on who is maintaining that function, but unless documented I would assume it isn't.
- weego 6y agoI think a more important lesson before you get anywhere near this one in that logic chain is: Don't attempt to code for every possible future.
- tpoacher 6y agoI'm a bit uncomfortable with the overtly broad use of the term "callback" in the article ... most examples aren't callbacks per se, just higher order functions expecting function expressions as inputs (which may or may not act as "callbacks"). Is this a "in the javascript world everything is a callback" thing, or is the author just using the term loosely?
- Noumenon72 6y agoI agree. I started my notes on this under the heading "callbacks" but ended up under "functions". The article could be titled "Pass arrow functions and partials, not objects you don't control."
- kall 6y agoYep that‘s it. Most of the javascript world is very open to beginners and not that worried about being strictly technically correct, when it doesn‘t really matter. A callback is pretty much any function that you pass as an argument. It‘s still not as much a programming language community as it is a website making community.
- spion 6y agoCallback is technically correct. The specific type of callback that parent has in mind is a continuation callback. Maybe you should check your bias about the JS world (as well as your definitions).
- deleted 6y ago[deleted]
- Twisell 6y agoThanks for pointing out this confusion i'm a backend guy and anytime i have to deal with JS i keep wondering wtf people are talking about and if I'm dumb. Apparently they just switch words for free. This is not helping beginners and it make documentation and qa especially hard to search.
- spion 6y agoWhat would be the strict definition of "callback" ?
- wruza 6y agoThe core issue is that now you’re calling a different function. A type system may help, but the fact persists. Type systems only catch changes in (args.length ++ args.map(primitive_typeof)). I think that this issue is not with types or arguments, but with a loose naming and handling of dependencies and backwards compatibility. A theoretical author of toReadableNumber() simply ditched one function and introduced another one, in-place. Apparently they did that because there is no way in their project to fork, retain and maintain both. You may say that types solve 95% of this, but dynamic languages exist for a reason, and when you feel like using one or see an advantage in it, other techniques may be applied. We could resolve that by using semver on functions instead of modules (renaming them at import for convenience), but nobody does that. Functions are fundamental building blocks, they take your args, do their job, return results and may have a separate environment (not in js), but somehow they are not autonomous entities. In contrast, in a real world we use explicit versions of things: gtx 1060 6gb, iphone se, cat 6020b, and the same for their part numbers. Nobody specifies just “gtx” or “cat” in their package.xls.
- leni536 6y agoThe problem in general is a language feature silently increasing the API surface. In this specific case the function was already callable with two parameters, without the library author's intent. Another example to the general rule is the fragile base problem: every method is a customization point by default. These "convenience" features can easily turn into headaches like this for library authors.
- magicalhippo 6y agoFor me the core issue seems to be that the map function has, in essence, three overloads which does very different things. A strongly typed compiler would not catch the error in the article with a similarly overloaded map function, so type checking can't really rescue you from this situation. Sure the chances of it happening silently might be slightly less, as the second and third parameter would have to match in type (integers in this case), but it could still absolutely happen silently. So to me the core take-away is that overloading a function in such a way is a very poor design choice, regardless of language.
- valenterry 6y agoLet's talk straight about Javascript. Repeat with me: Literally any kind of change to a function in javascript is a breaking change. Don't believe it? Give me an example of a function and how you change it and I will show you the code that works with the original function but breaks with the changed one.
- kall 6y agoHow do you feel about adding arguments to a "single object as named arguments that is destructured in function declaration"? I can also always come up with something that breaks, but seems pretty safe. add({a:2, b:4}) function add({a, b}) { return a + b } function _add({a, b, multiplier = 1}) { return a+b*multiplier } challenge: come up with a breaking use that does not involve something having a property named "multiplier"
- valenterry 6y agoFirst of all, I _can_ demonstrate code that will work when using "add" but explodes when using the changed version - even with your restriction of not having a "multiplier" property. However, let's keep the tension for while. The fact that having a working function using "multiplier" is already a bit broken - it shouldn't work from the beginning, but javascript is designed so that it does. Hence you have to give me this restriction, because otherwise it is obvious how your example can be "broken". Before I expose my (very simple) solution to still break your example even without using "multiplier", I would like to ask you to try to come up with another example first, where you don't require restrictions. I think it's a good exercise. :) If no one comes up with one, I'll show it in, say, a day from now.
- benburton 6y agoconst add = ({ a, b }) => a + b; const _add = ({ a, b }) => b + a;
- valenterry 6y agoThat one is rather simple to break. console.log(add({a:"1", b:2})) //12 console.log(_add({a:"1", b:2})) //21 So to make the code break with the change to _add, I can just do: if(add({a:"1", b:2}) != 12) boom()
- deleted 6y ago[deleted]
- presentation 6y agoI understand the point, and also wish typescript were a bit stricter about this, but the main reason I still do this sometimes is for the filter function when the function I pass in is a type guard - wrapping it in an anonymous function requires also duplicating the type guard logic, which is enough repetition for me to sidestep it. That said it’s easy enough to just disable the ESLint rule that enforced this limitation for those lines.
- timdaub 6y ago> but I still got folks on Twitter telling me to "just use TypeScript", This type of behavior is exactly the reason why I think the existence of Typescript adds downsides to being a JS dev. Saying "just use TS" is of equal value as saying: "just use Assembly" or "just use Dart". It has no value. First an foremost this type of logical behavior is a problem that needs to be adressed in JavaScript. The dynamically typped language that is embedded into every major browser. A heuristic will have to be discovered by JS devs. Or TC39 will have to extend the standard. Ultimately, it then comes down to a contexual personal choice of using TS over JS. When it comes to separation of concerns, I can recommend reading this essay by Dijkstra: https://www.cs.utexas.edu/users/EWD/transcriptions/EWD04xx/EWD447.html https://www.cs.utexas.edu/users/EWD/transcriptions/EWD04xx/E...
- ragnese 6y agoPersonally, I'm not a fan of TypeScript anyway. JavaScript is a lost cause to me. I'm not a full time JS Dev, so every time I have to touch one of our Node projects at work, I mentally prepare myself for very slow dev speed (ironic, considering the arguments that dynamically typed, loosey goosey, language speed you up) and frustrating bugs around `this`, mixed up function arguments, forgetting to await promises returned from functions, etc. But TypeScript really doesn't actually help that much compared to an IDE that understands JSDoc. Its type system is unsound and it's too accommodating of JavaScript's nonsense. If I ever start a new project that just has to run on Node, I'd probably try one of these languages that transpiles to JS, but is totally different, like Clojure or OCaml.
- gorjusborg 6y ago1. Not every function invocation is a callback. 2. A JS library that changes function arity as if it doesn't break API isn't being responsible. 3. This sort of paranoia defensiveness in API usage is a direct result of language (not developer) shortcomings.
- mpawelski 6y agoWell, typescript doesn't solve this because "passing callback function that doesn't use all provided parameters" is so common in JS world that it would cause much hurdle when migrating JS project or when writing idiomatic JS code. But I definitely can image typescript could provide another "strict" (or even something outside of "strict" group, like recent "pedantic" option for index access [1]) option that would check against that potential errors. [1] https://github.com/microsoft/TypeScript/pull/39560 https://github.com/microsoft/TypeScript/pull/39560
- Aldipower 6y agoThe article content is great and a good advice, but let me nit-pick on the title a little bit. It should be "Don't use functions as _arguments_ unless they're desdigned for it". Function passed as arguments to higher-order function are just that, arguments. A function callback on the other hand is just that, a call back, after a longer asynchronous run.
- z3t4 6y agoIf you did not use map, Promise nor object destructuring you would not have these problems. Code would not look as cool though.
- ragnese 6y agoI agree and will take it a step further: if you didn't use JavaScript, you would not have these problems.
- z3t4 6y agoI'm sure other languages have the same problems too, like features getting added to the language which makes it harder to understand, leading to subtle bugs. I'm not a grumpy old saying that the old times where better, there are plenty of old JavaScript features that I also don't use unless I'm working with someone that is so in love with those features that they can't live without them, I just inform them of the implications so at least they are aware of the warts. The problem is when you are in love/hyped you easily neglect the ugly parts, and can tolerate a lot of pain. And then there's the curse of knowledge that once you learn something it becomes apparent/obvious.
- spinningarrow 6y ago> The developers of toReadableNumber felt they were making a backwards-compatible change. To me that’s the real point. In JavaScript where a function’s signature is not changed by the number of parameters, adding even an optional argument probably constitutes a breaking change.
- spion 6y agoPositional optional arguments seems to be the bad idea here. If you have to have optional arguments, its probably best to make them named. This would this make an unintended clash a lot less likely. The name has to match - and if you have the types, both the type AND the name would need to match. Not only that, it also it lets you have any number of optional arguments with a lot less fuss, and pass any subset of them.
- moron4hire 6y agoCan't have a rando library break on you if you don't import any rando libraries.
- tengbretson 6y agoPoint-free function passing is only valuable from a stylistic point of view. In any dynamic language I don't think the downsides make it worthwhile.
- hansvm 6y agoI'll admit to not being fully ready for all of JavaScript's footguns when I first switched from other languages, and most of the function related ones can be (as this article points out) mitigated by blindly wrapping everything in lambdas. As one fun example suppose you're working with the snippet: const f = foo.predicate; return arr.filter(f); If foo is a class instance and references any instance variables via `this`, then just storing the method before you attempt to use it will cause the whole house of cards to blow up. So...adding a reference to `this` is a breaking change for even moderately sane code. That problem is also easily mitigated with lambdas: const foo = (x) => foo.predicate(x);
- brundolf 6y agoFwiw adding new arguments to a function should be a minor release increment (1.2.3 -> 1.3.0), so theoretically you could prevent this issue by locking the minor version numbers on dependencies (or you could definitely prevent it by locking your patch numbers) Practically speaking, I've virtually never run into this problem in 6 years of writing JS professionally. Of course your mileage may vary
- pietrovismara 6y agoThe javascript hate, combined with the elitist / superior programmer attitude, is the lamest thing I ever witnessed in this industry, especially on HN. Who cares about the web, right? While people keep crying about how bad JS is, I (and many others) use it for what it is, a tool with his flaws but also its advantages. I wrote Rust, Python, PHP, a bit of C and honestly JS is still my favourite language to write together with rust. People are lame.