Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring constants with the nameof() the constant as the value

Scenario

I have a class for declaring string constants used around a program:

public static class StringConstants
{
    public const string ConstantA = "ConstantA";
    public const string ConstantB = "ConstantB";
    // ...
}

Essentially, it doesn't matter what the actual value of the constant is, as it used when assigning and consuming. It is just for checking against.

The constant names will be fairly self-explanatory, but I want to try and avoid using the same string values more than once.


What I would like to do

I know that nameof() is evaluated at compile-time, so it is entirely possible to assign the const string's value to the nameof() a member.

In order to save writing these magic strings out, I have thought about using the nameof() the constant itself.

Like so:

public static class StringConstants
{
    public const string ConstantA = nameof(ConstantA);
    public const string ConstantB = nameof(ConstantB);
    // ...
}

Question...

I guess there is no real benefit of using the nameof(), other than for refactoring?

Are there any implications to using nameof() when assigning constants?

Should I stick to just using a hard-coded string?

like image 619
Geoff James Avatar asked Nov 30 '16 12:11

Geoff James


Video Answer


1 Answers

Whilst I think the use of nameof is clever, I can think of a few scenarios where it might cause you a problem (not all of these might apply to you):

1/ There are some string values for which you can't have the name and value the same. Any string value starting with a number for example can't be used as a name of a constant. So you will have exceptions where you can't use nameof.

2/ Depending how these values are used (for example if they are names of values stored in a database, in an xml file, etc), then you aren't at liberty to change the values - which is fine until you come to refactor. If you want to rename a constant to make it more readable (or correct the previous developer's spelling mistake) then you can't change it if you are using nameof.

3/ For other developers who have to maintain your code, consider which is more readable:

public const string ConstantA = nameof(ContantA);

or

public const string ConstantA = "ConstantA";

Personally I think it is the latter. In my opinion if you go the nameof route then that might give other developers cause to stop and wonder why you did it that way. It is also implying that it is the name of the constant that is important, whereas if your usage scenario is anything like mine then it is the value that is important and the name is for convenience.

If you accept that there are times when you couldn't use nameof, then is there any real benefit in using it at all? I don't see any disadvantages aside from the above. Personally I would advocate sticking to traditional hard coded string constants.

That all said, if your objective is to simply to ensure that you are not using the same string value more than once, then (because this will give you a compiler error if two names are the same) this would be a very effective solution.

like image 162
Brian Cryer Avatar answered Oct 13 '22 17:10

Brian Cryer