List const int instead of listing

I started working on a large C # code base and found using a static class with multiple lines of int ints. This class acts just like an enumeration.

I would like to convert the class to an actual enumeration, but the credentials to be specified are not. The main reason I would like to convert it is because I could list it as a data type, not an int. This would help a lot with readability.

Is there a reason not to use enums and use const ints instead? This is currently the code:

public int FieldA { get; set; } public int FieldB { get; set; } public static class Ids { public const int ItemA = 1; public const int ItemB = 2; public const int ItemC = 3; public const int ItemD = 4; public const int ItemE = 5; public const int ItemF = 6; } 

However, I think it should be as follows:

 public Ids FieldA { get; set; } public Ids FieldB { get; set; } 
+8
enums c # design-patterns
source share
5 answers

I think many of the answers here ignore the consequences of enums semantics.

  • You should consider using an enumeration when the entire set of all valid values ​​(identifiers) is known in advance and small enough to be declared in program code.

  • You should consider using int when a set of known values ​​is a subset of all possible values ​​- and the code should know about this subset.

As for refactoring, when time and business restrictions allow, it is recommended to clear the code when the new design / implementation will have a clear advantage over the previous implementation and where the risk will be well understood. In situations where the benefits are low or high risk (or both), it may be better to take the “do no harm” position rather than “constantly improve” . Only you can judge what matters to your situation.

By the way, the case where neither enumerations or constant ints are a good idea is when identifiers represent the identifiers of records in external storage (for example, a database). It is often dangerous to hardcode such identifiers in the program logic, since these values ​​can be different in different environments (for example, Test, Dev, Production, etc.). In such cases, loading values ​​at runtime might be a more appropriate solution.

+6
source share

Your proposed solution looks elegant, but will not work, since you cannot use instances of a static type. This is a bit more complicated than emulating an enumeration.

There are several possible reasons for choosing enum or const-int for implementation, although I cannot come up with a lot of strengths for the actual example that you posted - at first glance, this is an ideal candidate for listing.

Some ideas spring should pay attention to:

Transfers

  • They provide type safety. You cannot pass any old number where an enum value is required.
  • Values ​​can be auto-generated.
  • You can use reflection to easily convert between values ​​and names
  • You can easily list the values ​​in an enumeration in a loop, and then if you add new members to the enumeration, the loop will automatically take them into account.
  • You can insert new enunm values ​​without worrying about collisions if you accidentally repeat the value.

<strong> Const-Ints

  • If you do not understand how to use enumerations (for example, not knowing how to change the underlying data type of an enumeration, or how to set explicit values ​​for enumeration values ​​or how to assign the same value to mulitple constants), you may mistakenly believe that you have reached something that you cannot use an enum using const.
  • If you are used to other languages, you can just naturally approach the problem with consts, not realizing that there is a better solution.
  • You can get from classes to extend them, but it is annoying that you cannot get a new enumeration from an existing one (which would be a really useful function). Potentially, you could therefore use a class (but not the one that I am your example!) To achieve an "extensible enumeration".
  • You can easily pass ints. Using an enumeration may require that you constantly add (for example) the data you receive from the database to and from the enumerated type. What you lose in the type of security you get in convenience. At least until you mix up the wrong number somewhere ... :-)
  • If you use readonly rather than const, the values ​​are stored in real memory cells, which are read if necessary. This allows you to publish constants in another assembly that are read and used at runtime, rather than embedded in another assembly, which means that you do not need to recompile the dependent assembly when any of the constants in your own assembly change. This is an important consideration if you want to be able to fix a large application by simply releasing updates for one or two builds.
  • I suppose this is a way to make it clear that enumeration values ​​should remain unchanged. With enum, another programmer will simply launch the new value without hesitation, but the list of consts will make you stop and think “why is this so? How to add the new value safely?”. But I would achieve this by putting explicit values ​​in the enumerations and adding a clear comment, instead of resorting to consts.

Why should you leave the implementation alone?

  • The code may have been written by an idiot who has no good reason for what he did. But, changing his code and showing him, he is an idiot not a smart or useful move.
  • This can be a good reason, and you can break something if you change it (for example, maybe it should be a class due to the fact that it is accessed through reflection, exposed to external interfaces or stop people who are easily serialized values ​​because they will be violated by the obfuscation system that you use). No end to unnecessary errors is introduced into the system by people who do not quite understand how something works, especially if they do not know how to check their changes to make sure that they did not break anything.
  • A class can be auto-generated by an external tool, so this is the tool you need to fix, not the source code.
  • There may be a plan in the future to do something else with this class (?!)
  • Even if it is safe to change, you will have to double-check everything that the change affected. If the code works the way it costs, is the gain worth the pain? When we work on legacy systems, we often see existing low-quality code or just do things that we don’t like personally, and we must admit that it’s inappropriate to “fix” it, no matter how much it mocks. Of course, you can also bite "I told you so!" when a constant-based implementation fails due to a lack of type security. But besides type safety, an implementation is ultimately no less efficient or efficient than enumeration.
+3
source share

If it does not break, do not correct it.

I do not know the design of the system you are working on, but I suspect that the fields are integers, which, as it turned out, have a number of predefined values. To say that they may in some future state contain more than predetermined values. Although enum allows this scenario (through casting), this means that only the values ​​enumerated are enumerated.

In general, change is semantic, but it is not necessary. Unnecessary changes like this are often a source of errors, additional overhead and other headaches with minor advantages. I say add a comment expressing that it can be enum and leave it as it is.

+1
source share

Yes, it helps with readability, and I can't think of any reason against it.

Using const int is a very common "old school" programming practice for C ++.

0
source share

The reason I see it is that if you want to be loosely coupled to another system that uses the same constants, you avoid hard communication and use the same type of enumeration.

Like in RPC calls or something ...

0
source share

All Articles