dotnet / docs

This repository contains .NET Documentation.
https://learn.microsoft.com/dotnet
Creative Commons Attribution 4.0 International
4.28k stars 5.91k forks source link

usefulness of the `nameof` pattern is not clear #36396

Open daveyostcom opened 1 year ago

daveyostcom commented 1 year ago

A compelling use for the nameof pattern is not presented. I can’t see the usefulness from the example given.

let f (str: string) =
    match str with
    | nameof str -> "It's 'str'!"
    | _ -> "It is not 'str'!"

seems to work identically to this:

let f2 (s: string) =
    match s with
    | "str" -> "It's 'str'!"
    | _ -> "It is not 'str'!"

The page linked does not help to clarify.


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

gfoidl commented 1 year ago

The page linked does not help to clarify.

Are you refering to Nameof?

The benefit of nameof is that you can "reference" a symbol in the source, and if you rename that symbol, then nameof will use the new value. So no magic strings, no latent errors by forgotten updates of these magic strings, and so on.

In the docs for nameof there's

This is useful in various scenarios, such as logging, and protects your logging against changes in source code.

under Remarks, but this could be more prominent and w/o the example of logging given?

BillWagner commented 1 year ago

tagging @vzarytovskii for recommendation on the best fix.

daveyostcom commented 1 year ago

Are you refering to Nameof?

Yes

The benefit of nameof is that you can "reference" a symbol in the source, and if you rename that symbol, then nameof will use the new value. So no magic strings, no latent errors by forgotten updates of these magic strings, and so on.

That’s a nice way to put it.

In the docs for nameof there's

This is useful in various scenarios, such as logging, and protects your logging against changes in source code.

under Remarks, but this could be more prominent and w/o the example of logging given?

It would be compelling to explain as above and also give a logging example.