Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to store a group of constants that my program uses? [closed]

Tags:

c#

constants

I have various constants that my program uses... string's, int's,double's, etc... What is the best way to store them? I don't think I want an Enum, because the data is not all the same type, and I want to manually set each value. Should I just store them all in an empty class? Or is there a better way?

like image 551
Matthew Avatar asked Nov 12 '09 17:11

Matthew


People also ask

Where do you put constants files?

Putting it in a constant file just spams the file with unnecessary stuff. The less the constant has the character of a constant but a variable (like version number) the more you can put it outside. The less variable the constant is, so the more constant it is, the more it should placed inside it's scope.

Where should global constants be placed in a program?

Really global (application-level) constants should be in the application's namespace (provided your application is inside it's own namespace, as it should be). For module-level constants, the module's own namespace is the natural place.

What's the best practice to keep all the constants in flutter?

The preferred solution is to make your own Dart library.

Where should I store constants Java?

Even if a constant is used throughout the code but always in relation to one class, it should clearly be part of that class (e.g. BorderLayout. CENTER).


2 Answers

You probably could have them in a static class, with static read-only properties.

public static class Routes {     public static string SignUp => "signup"; } 
like image 150
Daniel A. White Avatar answered Sep 21 '22 07:09

Daniel A. White


IMO using a class full of constants is fine for constants. If they will change semi-occasionally I recommend using AppSettings in your config and the ConfigurationManager class instead.

When I have "constants" that are actually pulled in from AppSettings or similar I still will always have a "constants" class that wraps the reading from configuration manager. It's always more meaningful to have Constants.SomeModule.Setting instead of having to resort directly to ConfigurationManager.AppSettings["SomeModule/Setting"] on any place that wants to consume said setting value.

Bonus points for this setup, since SomeModule would likely be a nested class inside the Constants file, you could easily use Dependency Injection to inject either SomeModule directly into classes that depend on it. You could also even extract an interface on top of SomeModule and then create a depenedency to ISomeModuleConfiguration in your consuming code, this would then allow you to decouple the dependency to the Constants files, and even potentially make testing easier, especially if these settings come from AppSettings and you change them using config transformations because the settings are environment specific.

like image 40
Chris Marisic Avatar answered Sep 17 '22 07:09

Chris Marisic