2

I'm getting started with JavaScript after a lot of experience with Python and some with C. I've noticed in a few tutorials that there seems to be a strong preference among JavaScript programmers to use lambda (arrow) functions when they could use a named function instead. For example, I found this code at https://www.electronjs.org/docs/latest/tutorial/quick-start:

const createWindow = () => {
  const win = new BrowserWindow({
    width: 800,
    height: 600
  })

  win.loadFile('index.html')
}

app.whenReady().then(() => {
  createWindow()
})

The definition for createWindow could have used function createWindow() {...} instead, and the call to app.whenReady could have just passed the createWindow identifier instead of creating a new anonymous function to pass. So this code could have been rewritten as follows:

function createWindow() {
    const win = new BrowserWindow({
        width: 800,
        height: 600
    })

    win.loadFile('index.html')
}

app.whenReady().then(createWindow)

That to me seems more readable, but maybe I'm biased coming from a Python background? Python, like JavaScript, has lambdas and functions as objects. So Python programmers could write code like the first example above, but they never do -- the convention is just to define a function and then pass its identifier when needed for callbacks. On the other hand, the examples above seem extremely common in the JavaScript code I've seen.

Can anyone comment on the philosophy that drives this in JavaScript? Is it just that JavaScript is almost always used asynchronously, so code is almost always called via a callback, and coders just get used to writing everything as a lambda to be called later? Or is it that this improves maintainability, e.g., if you end up wanting to add more behavior to the whenReady callback? I could understand that, but I really don't understand why people prefer the const createWindow = () => {...} approach when the language already has the equivalent(?) and more fundamental function createWindow() {...} syntax.

Matthias Fripp
  • 17,670
  • 5
  • 28
  • 45
  • 1
    For many cases it's a personal preference thing. But be aware that arrow functions and regular functions *do* have meaningful differences in how they behave https://stackoverflow.com/questions/34361379/are-arrow-functions-and-functions-equivalent-interchangeable . So if those differences matter, then you must use the right tool for that job. – Nicholas Tower Jun 09 '23 at 16:18
  • There's little difference in your example, it's likely to be force of habit. But if the function takes parameters, you need to use the lambda. – Barmar Jun 09 '23 at 16:20
  • There are some differences, for example`.bind` does not work on arrow functions – bill.gates Jun 09 '23 at 16:23
  • Your `createWindow` function doesn't do anything that depends on the differences between arrow and regular functions. It's not clear why you changed that. – Barmar Jun 09 '23 at 16:42
  • 1
    @Barmar, I was just pointing out that in other languages, it would be more common to define a function with the specified name, rather than create an anonymous function (lambda) and assign it to a variable with that name. I was wondering why people usually take use the anonymous function approach in JavaScript. – Matthias Fripp Jun 09 '23 at 22:06
  • I thought your question was about using a lambda that doesn't do anything other than call the named function. Is it actually about whether you use `function functionName` or `const functionName =` to define the function? That's almost always just personal style. – Barmar Jun 09 '23 at 22:08
  • To clarify, I thought you were asking about `.then(createWindow)` versus `.then(() => { createWindow(); })` – Barmar Jun 09 '23 at 22:10
  • @MatthiasFripp There's no good reason to use `const createWindow = () => {` over `function createWindow() {` - the latter is shorter and more declarative. One would use arrow functions when their `this` binding is needed or when a concise body makes things simpler. Other than that, some people like to be consistent (always, not just sometimes, use arrow functions), some are fanatic about `const`, some consider arrow functions more "stylish" and "modern" (having been introduced in ES6, it was a way to show off). And some just don't know any better since they've never seen anything else. – Bergi Jun 10 '23 at 01:58
  • Similarly, the simplification of `() => createWindow()` to `createWindow` is something that simply eludes some people - reasoning about the equivalence requires [some theoretical understanding](https://en.wikipedia.org/wiki/Lambda_calculus#%CE%B7-reduction). Many people just are in the habit of always writing an (arrow) function expression when passing a callback, regardless of what the callback does. – Bergi Jun 10 '23 at 02:04

0 Answers0