What do you think about a C# 6 like nameof() expression for

Ron Buckton Ron.Buckton at microsoft.com
Fri Jun 14 21:49:18 UTC 2019


I’m not sure I understand what you mean by “false-positive” in this instance.

Consider this case:

```
const someObject = { value: 1 };
function setValue(value /*1*/) {
  if (typeof value /*2*/ !== "number") throw new TypeError(`Number expected: ${nameof value /*3*/}`);
  someObject["value" /*4*/] = value /*5*/;
}
```

If you rename the parameter `value` of the function `setValue` in an editor with a rename refactoring, you want to rename the symbols at 1, 2, 3, and 5, but not the string at 4.

Ron

From: guest271314 <guest271314 at gmail.com>
Sent: Friday, June 14, 2019 2:43 PM
To: Ron Buckton <Ron.Buckton at microsoft.com>
Cc: es-discuss at mozilla.org
Subject: Re: Re: What do you think about a C# 6 like nameof() expression for

How is that behaviour related to the use cases presented by OP? Would such behaviour not lead to false-positive relevant to the 2 use cases?

On Fri, Jun 14, 2019 at 9:36 PM Ron Buckton <Ron.Buckton at microsoft.com<mailto:Ron.Buckton at microsoft.com>> wrote:
> `nameof whatever` → `Object.keys({ whatever })[0]`, but I'm a bit confused why it'd be better to type `nameof foo` in code, rather than `'foo'` - if you change `foo` to `bar`, you have to change both of them anyways.

If you are using an editor that supports rename refactoring, its generally easier to rename the symbol `foo` and have all references (including `nameof foo`) be updated. You cannot safely automatically rename `'foo'` to `'bar'` since an editor or language service cannot guarantee that by the string `'foo'` you meant “the text of the identifier `foo`”.

From: es-discuss <es-discuss-bounces at mozilla.org<mailto:es-discuss-bounces at mozilla.org>> On Behalf Of Jordan Harband
Sent: Friday, June 14, 2019 2:29 PM
To: guest271314 <guest271314 at gmail.com<mailto:guest271314 at gmail.com>>
Cc: es-discuss <es-discuss at mozilla.org<mailto:es-discuss at mozilla.org>>
Subject: Re: Re: What do you think about a C# 6 like nameof() expression for

`nameof whatever` → `Object.keys({ whatever })[0]`, but I'm a bit confused why it'd be better to type `nameof foo` in code, rather than `'foo'` - if you change `foo` to `bar`, you have to change both of them anyways.

On Fri, Jun 14, 2019 at 1:31 PM guest271314 <guest271314 at gmail.com<mailto:guest271314 at gmail.com>> wrote:
Am neither for nor against the proposal. Do not entertain "like"s or "dislike"s in any field of endeavor. Am certainly not in a position to prohibit anything relevant JavaScript. Do what thou wilt shall be the whole of the Law.

Have yet to view a case where code will be "broken" by ```nameof``` not being a JavaScript feature. "robustness", as already mentioned, is a subjective adjective that is not capable of being objectively evaluated as to code itself. That description is based on preference or choice.

In lieu of the proposal being specificed, use the posted code example of ```Object.keys()``` that "works".

```
function func1({userName = void 0} = {}) {
  console.assert(userName !== undefined, [{userName}, 'property needs to be defined'])
}
```

provides a direct indication that the property value is required to be defined. Note that the example code posted thus far does not first check if ```options``` is passed at all, for which ```nameof``` will not provide any asssitance.

Usually try to meet requirement by means already available in FOSS browsers. Have no interest in TypeScript or using an IDE.

FWIW, have no objection to the proposal.

On Fri, Jun 14, 2019 at 7:53 PM Stas Berkov <stas.berkov at gmail.com<mailto:stas.berkov at gmail.com>> wrote:
guest271314, what is you point against `nameof` feature?

If you don't like it - don't use it. Why prohibit this feature for
those who find it beneficial?

I see `nameof` beneficial in following cases

Case 1. Function guard.
```
function func1(options) {
...
   if (options.userName == undefined) {
       throw new ParamNullError(nameof options.userName); //
`ParamNullError` is a custom error, derived from `Error`, composes
error message like "Parameter cannot be null: userName".
 // `Object.keys({options.userName})[0]` will not work here
   }
}
```

Case 2. Accessing property extended info
Those ES functions that accept field name as string.
e.g.
```
const descriptor1 = Object.getOwnPropertyDescriptor(object1, 'property1');
```
vs
```
const descriptor1 = Object.getOwnPropertyDescriptor(object1, nameof
object1.property1);
 // `Object.keys({options1.property1})[0]` will not work here
```
2nd variant (proposed) has more chances not to be broken during
refactoring (robustness).

It would make devs who use IDE more productive and make their life
easier. Why not give them such possiblity and make them happy?
_______________________________________________
es-discuss mailing list
es-discuss at mozilla.org<mailto:es-discuss at mozilla.org>
https://mail.mozilla.org/listinfo/es-discuss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190614/91734449/attachment-0001.html>


More information about the es-discuss mailing list