Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constants class vs. web.config AppSettings

I saw some guy defining configurations (like key, some static names, etc.) inside a class names Constants which has const properties.

I know that it is most elegant way to use configuration files for this kind of scenarios as described here but what is the advantages? I can assume that there must a performance benefit even if it is a little.

like image 545
tugberk Avatar asked Dec 06 '22 16:12

tugberk


2 Answers

The advantages are that you can change those value at runtime, with constants you can only change the values at compile time (meaning if you need to change something, you need to recompile and redeploy).

I still use constants and "Constants" classes for internal settings that I know won't need to be changed at runtime. These save you a little bit of overhead in reading, parsing, and setting up your configuration classes (ConfigurationManager etc). But if there's even a remote chance you might want to make an adjustment at runtime or after deployment, the performance savings isn't worth the extra hassle (IMHO).

like image 86
CodingGorilla Avatar answered Dec 27 '22 15:12

CodingGorilla


The advantage to config files is that you can change values without compiling.

There is an advantage in using a const if there is no possible way that the value will ever change. The performance gain is negligible.

like image 20
eouw0o83hf Avatar answered Dec 27 '22 16:12

eouw0o83hf