DEV Community

Discussion on: Magic Numbers (in programming)

Collapse
 
kfwerf profile image
Kenneth van der Werf

Interesting approach, why not just hide it in a method? Now you have a variable that doesn't make sense outside of a very specific constant thats public. E.g.

getHomeAddress(final String[] splitString) and getEmail(final String[] splitString), these should probably be private too as splitString would be specific.

Or just naming the variable? e.g. final var homeAddress = splitString[7], actually that would probably make more sense as the reusability is likely limited. Then the code to me is self documenting, e.g. 7 = home address as the var is named.

Collapse
 
anthonyjdella profile image
offline

@kfwerf Yea these also seem like great ideas! My post was one possible example but as you've shown, there are many other ways to do this. Thanks for brainstorming other ideas!