Reputation: 9870
If you need to use a constant inside only one method, should you declare it in the method or as a field?
Also, if you should declare it in the method, then should you use a lowercase or uppercase first letter for the name?
EDIT: This is not a duplicate because I am asking whether or not you should declare the const in your method or as a field.
Upvotes: 5
Views: 3505
Reputation: 5101
In general, declare variables in the narrowest scope practical. If you need that thing to have global scope in the future, ok fine, then make it global in the future.
The above reinforces appropriate, desired, encapsulation and abstraction.
Upvotes: 10
Reputation: 633
Standards are just that, standards. My team happens to use NameOfConstant instead of NAMEOFCONSTANT or NAME_OF_CONSTANT for constants. If the constant is only used within a method, the best (from a coding maintenence/readability perspective) is to place it inside the most constrained place possible. In this case that would be inside the only method using it.
Upvotes: 5