|
|
|
|
|
by tlrobinson
5468 days ago
|
|
These aren't very good examples. You shouldn't break up small pieces of code just to avoid nested callbacks. "Well, until you have two or three callbacks that respond differently to fs.readFile and you start running out of function names, right?" That's a red flag right there. If you can't figure out a unique name for the callback, perhaps it's not worth naming it. If you have a reusable piece of code, or it's just getting unwieldy (~3 levels deep or so), then you should consider breaking it out into it's own function. But otherwise for simple things I think it's more readable to have it inline. It's unfortunate JavaScript's anonymous function syntax is so verbose. It would be much more pleasant with lightweight syntax (CoffeeScript, perhaps?) |
|
Like what pg said somewhere (forgot where), having anonymous functions is a red flag in the language, because there shouldn't be any distinction between anonymous functions and named functions. Javascript kind of gets this right.
I believe CoffeeScript gets this even more right, by having a single way to declare functions which is also concise?