1

I would like to ask a question regarding constants, I know the approach or recommendations regarding this but recently I've been assigned to a project where the previous developers were using resourcekeys that are present both in SQL DB and C# classes and the usage of repeating magic strings inside the project is... very sad, literally by the tens of thousands of magic strings (i vomited in my mouth)

So what i did was i created a simple sql to extract and create a variable declaration of each of those resourcekeys and simply to be pasted into target internal sealed classes. Around 4000 keys, into around 7 groups, so roughly 500+ resourcekeys per class.

Now another developer told me a concern about performance in loading all these keys, I haven thnought of that honestly, but i believe that it shouldn't be much of an issue.

is there really such a thing is TOO MANY constants?

Thanks in advance

user1465073
  • 315
  • 8
  • 22
  • Have you considered benchmarking this in any way? – Krythic Feb 27 '15 at 04:38
  • If you have concerns about performance - create sample and measure, same for max number of whatever you are creating... As far as I know there is no formal limits and compilers/runtime free to pick whatever works. Limits are generally ridiculously high (i.e. it is rare to use 65K of [local variables](http://stackoverflow.com/questions/22504993/maximum-number-of-variables-in-method)). – Alexei Levenkov Feb 27 '15 at 04:39
  • I wouldn't be so judgemental about other people's code. You have the privilege of knowing how the system evolved, the people who built it didn't. – zneak Feb 27 '15 at 05:19
  • Honestly there's no need to benchmark this, yes it's perfectly fine and there's no real overhead there. Also you may want to automate that script and keep maintaining the data in the database (that way you can hand an editor to non dev people to the production DB and all their changes get there next time). However why not go another way and keep it all in the DB (obviously NOT making 1 query every time a label is required, but getting them all in a dictionary at runtime) and still using the generated class approach to access it? That way you get realtime updatability and static member usage – Ronan Thibaudau Feb 27 '15 at 05:29

2 Answers2

1

Constants from a compilers perspective are pretty boring. The C# compiler will simply embed all classes with constants in the output assembly and swap out any usage of constants with the actual value: e.g.

class C { public const T = "T" } 
...
Console.WriteLine(C.T);

will be compiled as such:

class C { public const T = "T" } 
...
Console.WriteLine("T");

Assuming that there is no dynamic code in play, the Jitter wont even look at the constant class at runtime, hence the performance impact will practically be neglect-able.

Polity
  • 14,734
  • 2
  • 40
  • 40
0

From the compilers and runtime point of view the use of 4000 constants is trivial and not a performance issue. Large applications may have this many, or more, spread though out them.

Phil Wright
  • 22,580
  • 14
  • 83
  • 137