This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Defining class constants

 
Barry Brashear
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone tell me how a class constant should be defined? Is it okay to
have a constant defined in the same file that it is used? Or should all
constants be in a class by themselves?

Thanks.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As usual, it depends on the situation. Some constants are values that you are allowed to pass into a particular method. Then it makes sense for the class that has the method to also have the constants. Some constants are used all over the system. Then it makes sense to make them more global in their own class.

It's a neat goal to have only one reason to change a class. That steers you away from a single class with all the constants in the system because it would have many reasons to change.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic