[英]List of const int instead of enum
I started working on a large c# code base and found the use of a static class with several const ints fields. 我开始研究一个大的c#代码库,发现使用了一个带有几个const int字段的静态类。 This class is acting exactly like an enum would.
这个类的行为与枚举完全相同。
I would like to convert the class to an actual enum, but the powers that be said no. 我想把这个类转换成一个实际的枚举,但权力说不。 The main reason I would like to convert it is so that I could have the enum as the data type instead of int.
我想转换它的主要原因是我可以将枚举作为数据类型而不是int。 This would help a lot with readability.
这对可读性有很大帮助。
Is there any reason to not use enums and to use const ints instead? 是否有任何理由不使用枚举并使用const int代替? This is currently how the code is:
目前这是代码的方式:
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 the following instead: 但是,我认为应该是以下内容:
public Ids FieldA { get; set; }
public Ids FieldB { get; set; }
I think many of the answers here ignore the implications of the semantics of enums
. 我认为这里的许多答案都忽略了
enums
语义的含义。
You should consider using an enum when the entire set of all valid values (Ids) is known in advance, and is small enough to be declared in program code. 当事先知道所有有效值(Ids)的整个集合并且小到足以在程序代码中声明时,您应该考虑使用枚举。
You should consider using an int when the set of known values is a subset of all the possible values - and the code only needs to be aware of this subset. 当已知值集是所有可能值的子集时,您应该考虑使用int - 并且代码只需要知道此子集。
With regards to refactoring - when time and business contraints allow, it's a good idea to clean code up when the new design/implementation has clear benefit over the previous implementation and where the risk is well understood. 关于重构 - 当时间和业务约束允许时,当新设计/实现比先前实现具有明显优势并且风险得到充分理解时,清理代码是个好主意。 In situations where the benefit is low or the risk is high (or both) it may be better to take the position of "do no harm" rather than "continuously improve" .
在利益低或风险高(或两者兼有)的情况下,采取“不伤害”而非“持续改进”的立场可能更好。 Only you are in a position to judge which case applies to your situation.
只有您能够判断哪种情况适用于您的情况。
By the way, a case where neither enums or constant ints are necessarily a good idea is when the IDs represent the identifiers of records in an external store (like a database). 顺便说一句,那里既没有枚举或常量是整数的情况下,一定是一个好主意,当这些ID代表的外部存储(如数据库)的记录的标识符。 It's often risky to hardcode such IDs in the program logic, as these values may actually be different in different environments (eg. Test, Dev, Production, etc).
在程序逻辑中对这些ID进行硬编码通常是有风险的,因为这些值在不同的环境中实际上可能是不同的(例如,测试,开发,生产等)。 In such cases, loading the values at runtime may be a more appropriate solution.
在这种情况下,在运行时加载值可能是更合适的解决方案。
Your suggested solution looks elegant, but won't work as it stands, as you can't use instances of a static type. 您建议的解决方案看起来很优雅,但不会按原样运行,因为您不能使用静态类型的实例。 It's a bit trickier than that to emulate an enum.
这比模拟枚举有点棘手。
There are a few possible reasons for choosing enum or const-int for the implementation, though I can't think of many strong ones for the actual example you've posted - on the face of it, it seems an ideal candidate for an enum. 为实现选择enum或const-int有几个可能的原因,虽然我不能想到你发布的实际例子中有很多强大的理由 - 从表面来看,它似乎是枚举的理想候选者。
A few ideas that spring to mind are: 我想到的一些想法是:
Enums 枚举
const-ints 常量,整型
Why should you leave the implementation alone? 你为什么要单独执行这个实现?
If it ain't broke, don't fix it. 如果没有损坏,请不要修理它。
I don't know the design of the system you're working on, but I suspect that the fields are integers that just happen to have a number of predefined values. 我不知道您正在处理的系统的设计,但我怀疑这些字段是恰好恰好具有许多预定义值的整数。 That's to say they could, in some future state, contain more than those predefined values.
也就是说,在未来某个状态下,它们可能包含超过这些预定义值。 While an
enum
allows for that scenario (via casting), it implies that only the values the enumeration contains are valid. 虽然
enum
允许该场景(通过强制转换),但它意味着只有枚举包含的值才有效。
Overall, the change is a semantic one but it is unnecessary. 总的来说,这种变化是一种语义变化,但这是不必要的。 Unnecessary changes like this are often a source of bugs, additional test overhead and other headaches with only mild benefits.
像这样的不必要的变化通常是错误的来源,额外的测试开销和其他令人头疼的问题,只有轻微的好处。 I say add a comment expressing that this could be an
enum
and leave it as it is. 我说添加评论表示这可能是一个
enum
并保持原样。
Yes, it does help with readability, and no I cannot think of any reason against it. 是的,它确实有助于提高可读性,而且我无法想出任何反对它的理由。
Using const int is a very common "old school" of programming practice for C++. 使用const int是C ++编程实践中非常常见的“旧学校”。
The reason I see is that if you want to be loosely coupled with another system that uses the same constants, you avoid being tightly coupled and share the same enum type. 我看到的原因是,如果你想与另一个使用相同常量的系统松散耦合,你可以避免紧密耦合并共享相同的枚举类型。
Like in RPC calls or something... 就像在RPC调用中那样......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.