-4

Following the responses from the below questions and answers, is it really worth the effort to remove all the Null instances from a middle-weight [50,000 lines] project if that Null value has never been used throughout the entire project? It might look like a simple right click in resharper but is it really worth the effort? What would I gain? Just a better-practice code?

Is it a good practice to add a “Null” or “None” member to the enum

C# Enums: Nullable or 'Unknown' Value?

Community
  • 1
  • 1
MHOOS
  • 5,146
  • 11
  • 39
  • 74

3 Answers3

2

Whether you should delete an unused "None/null" value of an enum is identical to your answer to the question whether you believe you should have "None/null" value. If you think they should not exist, delete it. If you think it should exist keep it.

I believe this question is a tiny extension of the already existing question. Deleting an unused enum member is a one-line change that is easily reversible.

One possible pitfall is that removing an enum member changes the output of Enum.ToString.

usr
  • 168,620
  • 35
  • 240
  • 369
2

Consider this enum:

enum Color { None, Red, Green, Blue }

This will print "None":

Console.WriteLine(default(Color));

If you remove the None, that statement will print something else.

If will print "0" if you do this:

enum Color { Red = 1, Green, Blue }

It will print "Red" if you do this:

enum Color { Red, Green, Blue }

In addition, the latter change will have an impact on serialized values.

Be careful when assuming a None value is not used, simply because you see no reference to it in code.

Similar observations apply to this code:

var colors = new Color[10];

Console.WriteLine(colors[0]);

or

var color = (Color) 1;
Kris Vandermotten
  • 10,111
  • 38
  • 49
  • Very good point Kris. Those Null enums are sitting there in the code doing no harm to me right now but I think removing them could be the start of trouble. – MHOOS Jun 11 '14 at 13:11
  • @Socrates Their "harm" is mostly limited to some metadata. When in doubt, don't change working code for such little potential benefit. – Kris Vandermotten Jun 11 '14 at 13:13
1

Looking at the different aspects, some pros for removing the unused enum values:

  • Removing unused code is usually good for making it more readable and maintainable.
  • Values without a purpose can cause confusion.

and some possible pitfalls:

  • An unused enum value might still have a purpose for documentation by just existing.
  • An unused enum value might still affect what value other enum values get.
  • Changing code always has a potential risk of introducing bugs.
Guffa
  • 687,336
  • 108
  • 737
  • 1,005