Function argument hints

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Function argument hints

Gareth Heyes
Hi all

I propose a new syntax for defining function argument hints. If you have a lot of arguments in a function it can be difficult to remember which argument is which. Using a hint allows you to easily see the argument required and can prevent you from passing an incorrect argument.

It would work like this:
```javascript
function x(domNode, obj, someFlag) {
}

//here are the argument hints
x(domNode:dom, obj: o, someFlag: true)
```

So here the js engine looks up the argument hint and compares it to the function definition. If the hint does not match the function argument in the definition then a exception is thrown.

```javascript
x(obj: o, someFlag: true) //error "obj" hint does not match "domNode" hint.
```

The hint easily allow you to see what argument you are using without having to look at the function definition and also prevent you passing an incorrect argument.

Cheers
Gareth

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Function argument hints

T.J. Crowder-2
I think you're talking about named parameters, or close enough anyway. Most recent discussion of named parameters (originally just "is there discussion of them?" but it went from there):

Others probably as well if you search back further in the archives and meeting notes.

Note also overlap with parameter destructuring. It's a totally different thing, but can be used for some of the same use cases (long argument lists in particular).

-- T.J. Crowder

On Wed, Jul 26, 2017 at 2:09 PM, Gareth Heyes <[hidden email]> wrote:

Hi all

I propose a new syntax for defining function argument hints. If you have a lot of arguments in a function it can be difficult to remember which argument is which. Using a hint allows you to easily see the argument required and can prevent you from passing an incorrect argument.

It would work like this:
```javascript
function x(domNode, obj, someFlag) {
}

//here are the argument hints
x(domNode:dom, obj: o, someFlag: true)
```

So here the js engine looks up the argument hint and compares it to the function definition. If the hint does not match the function argument in the definition then a exception is thrown.

```javascript
x(obj: o, someFlag: true) //error "obj" hint does not match "domNode" hint.
```

The hint easily allow you to see what argument you are using without having to look at the function definition and also prevent you passing an incorrect argument.

Cheers
Gareth

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Function argument hints

Gareth Heyes
Ah yeah true there is some overlap there thanks.

On 26 July 2017 at 14:19, T.J. Crowder <[hidden email]> wrote:
I think you're talking about named parameters, or close enough anyway. Most recent discussion of named parameters (originally just "is there discussion of them?" but it went from there):

Others probably as well if you search back further in the archives and meeting notes.

Note also overlap with parameter destructuring. It's a totally different thing, but can be used for some of the same use cases (long argument lists in particular).

-- T.J. Crowder

On Wed, Jul 26, 2017 at 2:09 PM, Gareth Heyes <[hidden email]> wrote:

Hi all

I propose a new syntax for defining function argument hints. If you have a lot of arguments in a function it can be difficult to remember which argument is which. Using a hint allows you to easily see the argument required and can prevent you from passing an incorrect argument.

It would work like this:
```javascript
function x(domNode, obj, someFlag) {
}

//here are the argument hints
x(domNode:dom, obj: o, someFlag: true)
```

So here the js engine looks up the argument hint and compares it to the function definition. If the hint does not match the function argument in the definition then a exception is thrown.

```javascript
x(obj: o, someFlag: true) //error "obj" hint does not match "domNode" hint.
```

The hint easily allow you to see what argument you are using without having to look at the function definition and also prevent you passing an incorrect argument.

Cheers
Gareth

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss




_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss