使用枚举与布尔值?
这最初看起来可能很通用,但事实上,我实际上正在做出我需要使用的决定。
我目前正在处理的工作涉及就业申请,其中需要在某些时候标记为活动或非活动。提交申请后,它将默认为有效。由于某些原因,它稍后可能会被设置为非活动。它只能是其中之一,并且永远不会null(以防万一这会改变任何内容)。
我将其与 Java + Hibernate + PostgresSQL 一起使用,以防这也产生任何影响。我的第一直觉是使用 Boolean 作为我的解决方案,以便它真正充当标志,但我的同事建议使用 enums 或 ints > 更多的是一种地位而不是一个旗帜。
我已经使用上述所有解决方案解决了此类问题,并且它们对彼此来说似乎都有些透明。
有没有一种方法更适合这种情况?
This may initially seem generic, but in fact, I am actually making the decision on which I need to use.
What I am currently working on involves Employment Applications, those in which will need to be marked at some point Active or Inactive. When an application is submitted, it will default to Active. For certain reasons, it might later be set to Inactive. It can only be one of these and never null(In case this changes anything).
I am using this with Java + Hibernate + PostgresSQL, in case this also makes any difference. My first instinct is to use Boolean as my solution so that it truly acts as a flag, but I have coworkers who have suggested using enums or ints as more of a status rather then a flag.
I have solved problems such as this using all of the above solutions, and they all seem somewhat transparent to eachother.
Is there one way that is better for this situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
即使忽略将来添加更多状态类型的可能性(这无疑是枚举的一个很好的论据),我认为枚举绝对是正确的方法。您不是在建模布尔条件,而是在建模应用程序的状态。想一想:应用程序的状态不是true 或false,而是活动或非活动!状态
enum
将以最自然的方式表示这一点。您还可以通过使用枚举获得许多内置优势,例如将每个状态的文本描述直接与其绑定,因此您不必执行类似“
您可以这样做”
的操作此外,您可以直接绑定特定行为使用每个枚举实现不同的抽象方法到每个状态,将特定数据与每个状态相关联,等等。
您还可以轻松地允许基于状态的布尔
isActive
检查...您不能如果您只存储一个布尔值,则可以轻松地以相反的方式做到这一点。事实上
null
不应该是一个有效的状态是无关紧要的......只需确保存储状态的任何类(例如,您的EmploymentApplication
类或其他类)抛出如果有人尝试对其设置null
状态,则会出现NullPointerException
。Even ignoring the possibility of adding more status types in the future (which is certainly one good argument for an
enum
), I think anenum
is absolutely the right way to go. You are not modelling a boolean condition, you are modelling the status of an application. Think about it: the application's status is not true or false, it's active or inactive! A statusenum
will represent this in the most natural way.You also get a lot of built in advantages from using an enum, such as having a text description of each status tied directly to it, so you don't have to do things like
You can just do
Additionally, you can tie specific behavior directly to each status with abstract methods that each enum implements differently, associate specific data with each status, etc.
You can also easily allow a boolean
isActive
check that is based on the status... you can't easily do that the other way around if you just store aboolean
.And the fact that
null
shouldn't be a valid status is irrelevant... just ensure that any classes that store the status (say, yourEmploymentApplication
class or whatever) throw aNullPointerException
if anyone tries to set anull
status on it.这完全取决于您的要求/规格。如果您只想将状态记录为活动或非活动,最好的方法是使用
boolean
。但如果将来,您将拥有诸如
Enums 之类的状态,那么 Enums 非常适合您。就您而言,目前,布尔值就足够了。不要过早尝试过于复杂的事情,你会失去设计和设计的重点。您的系统的开发。
It totally depends on your requirement/specification. If you only want to record the status as active or inactive, the best way is to use
boolean
.But if in the future, you will have a status such as,
Enums is perfect for you. In your case, for now, a boolean is sufficient. Don't try overcomplicate things too early, you'll lose focus in your design & development of your system.
绝对不要使用 int。使用枚举是面向未来的;您必须自己决定什么更具可读性,以及 YAGNI 是否适用。请注意,
boolean
与Boolean
不同;Boolean
是一个类名,因此,Boolean
类型的变量可以为 null;而boolean
是一个原语。Definitely don't use an int. Using an enum is future-proofing; you have to decide for yourself what's more readable, and whether YAGNI applies. Be aware that
boolean
is not the same thing asBoolean
;Boolean
is a class name, and as such, variables of typeBoolean
can be null; whereasboolean
is a primitive.布尔值在商业中很少见 根据我为商业
构建自定义应用程序的经验,诸如“女性/男性”或“开/关”之类的明显二元性几乎总是会演变或变形为多个值。
业务规则往往无法及早完全了解 -上,或随着时间的推移而改变。
和我的许多同事一样,我经历了惨痛的教训才知道永远不要将此类值定义为布尔值。稍后从布尔值更改为多个值是相当昂贵且有风险的。
Java 枚举
与布尔值相比,枚举还有其他优点。
Java 枚举非常灵活且易于使用。强大
Java 中的
Enum
工具比大多数其他语言中的枚举更加灵活、强大和有用。请参阅 Oracle 教程。在枚举定义中,您可以容纳值的表示形式以呈现给用户以及存储在数据库或文件中。这为程序员提供了一个方便的一站式商店,可以阅读有关此枚举的所有信息以及它如何在您的应用程序中使用。
这是一个完整的例子。这一类显示了我们在用户界面中使用的值以及我们保留的值。
将持久值转换回枚举对象
请注意,静态方法可以返回与从数据库检索的持久值相匹配的
Status
对象:灵活的集合/映射
在 Java 中,枚举有自己的
Set 实现
和Map
在使用很少的内存和非常快的执行方面都进行了高度优化。请注意,如果应用程序状态的定义发生更改,更新这些集定义是多么容易。
信息丰富的
toString
值虽然布尔值以字符串形式显示为
true
或false
,但在枚举中您可以覆盖toString
生成更具信息性的值,例如app-status-inactive
。这些值在记录、跟踪或调试时可能非常有用。
Booleans are rare in business & industry
In my experience building custom apps for business, an apparent duality such as "Female/Male" or "On/Off" nearly always evolve or morph into multiple values.
Business rules are often not fully known early-on, or change over time.
Like many of my colleagues, I learned the hard way to never define such values as booleans. Changing from boolean to multiple values later is quite expensive and risky.
Java enums
Enums have other benefits over a boolean.
Java enums are flexible & powerful
The
Enum
facility in Java is much more flexible, powerful, and useful than enums in most other languages. See Oracle Tutorial.Within your enum definition you can house representations of the values for presentation to the user and for storage in databases or files. This makes a handy one-stop-shop for a programmer to read all about this enum and how it used in your app.
Here is a complete example. This one class shows what values we use in the user-interface and what values we persist.
Translating persisted values back to enum object
Notice the static method that can return a
Status
object matching a persisted value retrieved from a database:Flexible sets/maps
In Java, enums have their own implementation of
Set
andMap
that are highly optimized both in terms of very little memory used and very fast execution.Notice how easy to update these set definitions if your definition of application-status changes.
Informative
toString
valueWhereas a boolean appears as
true
orfalse
as a string, in your enum you can overridetoString
to generate a much more informative value such asapp-status-inactive
.Such values may be quite useful when logging, tracing, or debugging.
难道两者都不能做吗?
这难道不能让你两全其美吗?它允许您使用名称为 ACTIVE 和 INACTIVE 的布尔值,而不是 true 和 false?
Is it not possible to do both?
Does this not get you the best of both worlds? It lets you use a boolean with the names ACTIVE and INACTIVE, rather than true and false?
如果真/假是唯一的可能性,则布尔值比枚举更有意义(开销更少)。建议:使用布尔类而不是布尔原语,这样您就可以检测“未知/未定义”状态以及真/假。
If true/false are the only possibilities, boolean makes more sense than enum (less overhead). Recommendation: Use the Boolean class instead of the boolean primitive, so you can detect the "unknown/undefined" state as well as true/false.
如果您可能需要除“活动”和“非活动”之外的更多状态,那么您会想要使用 enum 或 int 状态标志?这使得您的代码对于未来的状态更加灵活。
If you might ever have a need for more statuses other than Active and Inactive then you would want to use and enum or int status flag? That makes your code more flexible for future statuses.
在你的情况下,有一个布尔值就足够了。由于要求是“IsActive”,并且立即答案可以是 true 或 false。有一个枚举是可以的,但在我看来,布尔值是合适的
In your case having a boolean value should suffice. Since the requirement is 'IsActive' and the immediate answer can be either true or false. Having an enum is ok but IMO, a boolean is right suited
我认为使用 Enum 而不是 Boolean 更好。
Joshua Bloch (2008):
列出了双元素枚举相对于布尔值的以下优点:
源自幻灯片 (PDF) - 可能受到限制:
https://www.cs.umd.edu/ class/fall2009/cmsc132H/slides/still- effective.pdf
或者存档但可在以下位置访问:
https://web.archive.org/web/20120404070225/http://www.cs.umd.edu/class/fall2009/cmsc132H/slides/still- effective.pdf
I think it is better to use Enum instead of Boolean.
The book
lists following advantages of two-element enums over booleans:
Sourced from slides (PDF) - might be restricted:
https://www.cs.umd.edu/class/fall2009/cmsc132H/slides/still-effective.pdf
alternatively archived but accessible at:
https://web.archive.org/web/20120404070225/http://www.cs.umd.edu/class/fall2009/cmsc132H/slides/still-effective.pdf
我更喜欢枚举器,因为我与远程团队合作理解他们的代码,然后在深夜编写包含样板代码的测试用例,我相信使用
enum
比使用enum
要好得多boolean
不仅有助于良好的设计,还可以减少思考负担并提高可读性。I prefer enumerators as I collaborate with remote team comprehending their code and then writing test cases at the dead of night containing boilerplate code I believe use of
enum
are way and way better thanboolean
not only it aids in good designs but also reduces cogitative load and increases readability.如果在
isX
/hasX
和isY
/hasY
之间为布尔属性进行选择似乎是任意的,请使用枚举。在您的情况下,布尔值应该足够了(最好是
isActive
而不是isInactive
)。If it seems arbitrary to choose between
isX
/hasX
andisY
/hasY
for your boolean property, use an enum.In your case a boolean should suffice (preferably
isActive
overisInactive
).