This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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?
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 ]