• Post Reply Bookmark Topic Watch Topic
  • New Topic

Naming a single use enum  RSS feed

 
Derik Davenport
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a variable that can take one of several values. I created an enumeration for this value because I thought it would clarify the code.

I have only one instance of this enumeration.



Now myEnum was created specifically for a private variable named databaseStatus. It is not used outside of this class or for any other variable. But "myEnum" is a stupid name. I considered naming the enum DatabaseStatus but that lead to a lot of code that looked like this

which seemed very redundant, and also likely to run into a lot of typos related to capitalization.

I was wondering if there was any sort of convention for handling the naming of such a "once off" class?

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derik Davenport wrote:But "myEnum" is a stupid name.

Agreed.

I considered naming the enum DatabaseStatus but that lead to a lot of code that looked like this

which seemed very redundant

Not seeing the problem. That looks perfectly fine to me.

and also likely to run into a lot of typos related to capitalization.

That's a poor reason to avoid meaningful names. A good IDE will help out here with autocompletion, and if all else fails the compiler will let you know when you do it wrong.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derik Davenport wrote:Now myEnum was created specifically for a private variable named databaseStatus. It is not used outside of this class or for any other variable. But "myEnum" is a stupid name. I considered naming the enum DatabaseStatus but that lead to a lot of code that looked like this

which seemed very redundant, and also likely to run into a lot of typos related to capitalization.

First: what Bear said.

Second: The only redundancy might be if the these things are inside a class called Database. If that's the case, why not just call them 'status' and Status?

Winston
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derik Davenport wrote: . . . I have only one instance of this enumeration. . . .
There is only ever one instance of every enum; the runtime ensures that the same instances of each member are used everywhere. More details here. But it may be difficult to read.
 
Derik Davenport
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston wrote:
If that's the case, why not just call them 'status' and Status?
Because I also have DownloadingPackagesStatus, and ActiveDeviceStatus. These have their own enums. The first is rather long when you have to type it twice. But I decided that was the least of all evils. I prefer verbose names to ones that are shorter but whose meaning can not be inferred from a combination of the name and the context.

Campbell said:
There is only ever one instance of every enum;
Let me clarify what I meant by "I have only one instance of this enumeration". There is only one variable that takes on the constants of this enumeration. The enumeration is created for this single private variable whose name is the same but for the fact that it begins with a lower case letter.

I think part of what was tripping me up, was the naming convention. The way it is declared, the enum looks sort of like a local variable and I try to reserve proper case for types of objects and not instances of them. If I think of the enum as an inner class, it makes more sense to name it with proper case, and to then name the single instance of that class with the same name in camel case.



 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derik Davenport wrote:Now myEnum was created specifically for a private variable named databaseStatus. It is not used outside of this class or for any other variable. But "myEnum" is a stupid name. I considered naming the enum DatabaseStatus but that lead to a lot of code that looked like this

which seemed very redundant, and also likely to run into a lot of typos related to capitalization.

You could use static import, like this:
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't like that style. It makes it less clear where missing comes from. Is it a local variable, instance field, something else? You can't tell if you read that line.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have little problem with the static import, if DatabaseStatus is used a lot. However if there are other enums declared and used extensively within the same code, it can get confusing, and the "redundant" DatabaseStatus may help remind you which enum you're dealing with.

As for confusing the enum value with a local variable, this goes away if you follow the enum naming pattern that Sun and Oracle use: put the name in all uppercase, like a constant:


 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!