# What does the following syntax do? =\_=\>

**URL:** <https://discourse.processing.org/t/what-does-the-following-syntax-do/20927>\
**Category:** Beginners\
**Created:** [May 16, 2020, 1:41am UTC](https://discourse.processing.org/t/what-does-the-following-syntax-do/20927 "2020-05-16T01:41:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [May 16, 2020, 1:41am UTC](https://discourse.processing.org/t/what-does-the-following-syntax-do/20927/1 "2020-05-16T01:41:30Z")

</div>

Looking at code from open processing and I come across this piece of code.

```auto
C=0
setup=_=>{
  createCanvas(W=720,W,WEBGL)  
  noStroke()
  T=createGraphics(W,W).noStroke()
  texture(T)
}

draw=_=>{
  C+=1e-3
  background(0)
  for(y=0;y<60;y++){
    for(x=0;x<60;x++){
      n=int(noise(mag(x,y)/W,C)*2**24);
      T.fill('#'+hex(n,6)).rect(x*12,y*12,10,10);
    }
  }
  rotateX(C*4)
  rotateY(C*6)
  torus(400,300)
}

```

What does `=_=>` do, does it allow for anything new and I’m assuming this has a name of sorts.

Thanks

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 16, 2020, 2:07am UTC](https://discourse.processing.org/t/what-does-the-following-syntax-do/20927/2 "2020-05-16T02:07:52Z")

</div>

This example has it:  
[https://www.openprocessing.org/sketch/856969](https://www.openprocessing.org/sketch/856969)

Some discussion here:  
[https://www.openprocessing.org/sketch/683375/](https://www.openprocessing.org/sketch/683375/)

`:)`

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [May 16, 2020, 5:38am UTC](https://discourse.processing.org/t/what-does-the-following-syntax-do/20927/3 "2020-05-16T05:38:41Z")

</div>

> [@paulgoux](#):
>
> `setup=_=>`

This is an [arrow function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) which is shorthand for a “normal” function. So you can write something like this:

```auto
function myFunction(x) {
  console.log(x);
}

```

as this:

```auto
const myFunction = x => { console.log(x) };

```

So the `_` is “just” an unused variable to make it possible to define the `setup` and `draw` functions without using “normal” functions.

Arrow functions are most useful when you need to pass a function in as a parameter. In my humble opinion, the syntax you’re seeing just makes the code harder to read, so I would personally just stick with using “normal” functions for `setup` and `draw`.

Just because code is shorter, doesn’t mean it’s better. Making your code easier to understand is much more important.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [May 16, 2020, 8:16am UTC](https://discourse.processing.org/t/what-does-the-following-syntax-do/20927/4 "2020-05-16T08:16:30Z")

</div>

> [@paulgoux](#):
>
> What does `=_=>` do,

- As you already know by now from others, those are actually 2 operators + 1 parameter.
- The assignment operator `=`, followed by a parameter named as `_`, then the fat arrow (a.K.a. lambda function expression) operator `=>`:

> **[Arrow function expressions - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions)**
>
> An arrow function expression is a compact alternative to a traditional function expression, with some semantic differences and deliberate limitations in usage:

- Although JS’ parser is OK w/ just `=_=>`, by both convention & human-readability’s sake, we should place spaces between them in order to make it clear they’re 3 diff. things: `= _ =>`.
- So a line like `draw=_=>{` should be retyped in as `draw = _ => {`.
- BtW, a parameter named as just `_` is another convention for saying it’s gonna be completely ignored inside its function body.
- It’s taking advantage that JS fat arrow functions w/ exactly 1 parameter parenthesis are optional; so we don’t need to type in like this: `draw = (_) => {`.
- Personally I’d drop the `_` ignored param “convention” and just type in like this: `draw = () => {`.
- Now it clearly states the lambda now is parameterless.
- But wait, there’s more: your posted sketch example declares none of its variables!
- If we activate JS Strict Mode by placing `'use strict'` as the 1st statement of a JS file, that whole sketch would crumble down:

> **[Strict mode - MDN Web Docs Glossary: Definitions of Web-related terms | MDN](https://developer.mozilla.org/en-US/docs/Glossary/strict_mode)**
>
> JavaScript's strict mode is a way to opt in to a restricted variant of JavaScript, thereby implicitly opting-out of "sloppy mode". Strict mode isn't just a subset: it intentionally has different semantics from normal code.

- Under Strict Mode all new variables must be declared before their 1st usage!
- JS got 6 keywords which can be used to declare variables:  
`var`, `let`, `const`, `function`, `class`, `import`.
- And outta those 6, we can use 4 in order to declare function variable _draw_:
  1. `var draw = () => {`
  2. `let draw = () => {`
  3. `const draw = () => {`
  4. `function draw() {`

- Obviously if we use keyword `function`, it isn’t a fat arrow compact function anymore.
- However there’s another “danger” about the choice between regular functions & fat arrow compact functions: the keyword `this`.
- While under a regular function the keyword `this` (if neither [**call()**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call), [**apply()**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply) nor [**bind()**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) are in effect) represents whatever object which would have invoked it, fat arrows got its `this` permanently determined by its immediate context on creation time.
- So be extra careful which 1 you pick if the keyword `this` happens to be accessed inside a function body!
- Such logical errors involving keyword `this` are very hard to debug later!

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [May 17, 2020, 3:12am UTC](https://discourse.processing.org/t/what-does-the-following-syntax-do/20927/5 "2020-05-17T03:12:39Z")

</div>

Can we mark a topic with 2 acceptable solutions?
