Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

enums and files

 
Timmy Ryan
Ranch Hand
Posts: 63
IntelliJ IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

Question regarding enums and there use.
I created a checkers/draughts game for my final year project in college years ago and I am going over it again to update it.
I had various integer variables in the original program that I used to hold information such as what piece was on a square Who�s go is it etc.
Should I create a separate enum file for each new enum I need to create?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I had various integer variables in the original program that I used to hold information such as what piece was on a square Who�s go is it etc.

Not sure how this maps to an emun. Did you have constants numbering squares from (say) bottom left to top right or something?

What is the scope of your constant values? Do you use these Integer variables outside the cope of a single class? If not, why change them?
 
Timmy Ryan
Ranch Hand
Posts: 63
IntelliJ IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

I had a variable for say whos go it was.
1 would be the computer 2 would be the human.
This was ok but when reviewing code it sometimes would of been easier to view
computer rather than 1 in the code.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I imagine you have a bit of code that does this:

or something or other. If so this will look almost identical if you swap it to an enum. There is no harm in doing this, except that enums can be null, ints can't, so you have an extra case to cater for. If your constant is an Integer you already have this problem though.

My point about the scope of the constant valiable is that if you do expose this to other classes then yes, an enum is probably better. The old constant pattern was not type-safe, enums are. If your constant variables are private to the class that uses them, then you are not really gaining much by changing them. You are not really losing much either, so its up to you really. Whatever you feel makes more sense in the code.
[ August 19, 2008: Message edited by: Paul Sturrock ]
 
Timmy Ryan
Ranch Hand
Posts: 63
IntelliJ IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that. I guess there is no wrong or write answer to the question.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic