2

I have to code a class with heavy memory allocation considerations.

This class have strings all over the place like so

var data = GetPropertyValue<GSData>(response.BaseData, "scriptData");

Just like "scriptData" there are over 10 strings declared "in place"

Another programmer made some public const string for a few of these strings that where needed outside the class.

I was wondering if I should create private const string for the rest of the "in place" strings that are repeated in multiple times in the class.

My questions are:

  • What the difference in memory consumption will be?
  • What about reserving memory using const against using "in place"?

I need to understand what the compiler does when there are strings like "textA" defined in several places in a class (over multiple methods), what happens in memory when the program run, and so on.

Thanks!

dymanoid
  • 14,771
  • 4
  • 36
  • 64
  • 5
    There's no difference in memory consumption due to [string-interning](https://learn.microsoft.com/en-us/dotnet/api/system.string.intern?view=netframework-4.7.2) of string literals. But there's a difference in maintainability. Use a constant if they mean the same. – Tim Schmelter Nov 26 '18 at 14:26
  • 1
    Using const is good practice, but in terms of how much memory it will save? My guess is almost zero. – Ross Miller Nov 26 '18 at 14:26
  • 2
    Magic strings is evil. As others suggests - use constants at least. – Renatas M. Nov 26 '18 at 14:34
  • 2
    @Reniuz: a string literal isn't magic, it's very readable and clear. But often they should be replaced with constant, enums, methods or localized strings. – Tim Schmelter Nov 26 '18 at 14:42
  • @TimSchmelter When you use same string 10 times in code and it has impact to application behavior, I think it is magic string...or I do not understand incorrectly what is magic string :/ – Renatas M. Nov 26 '18 at 15:36
  • @Reniuz imo a magic String is a string that appears for no apparent reason, for example a "magic" jQuery event that creates it at client side. If you use a string literal it's not magic at all. But you're right that you should not repeat the same item multiple items and use a common variable/constan, configuratio, method or what else can return it. – Tim Schmelter Nov 26 '18 at 15:41
  • @TimSchmelter Well magic string is string literal. Googled for magic string explanation. Couple of good results [here](https://help.semmle.com/wiki/display/JAVA/Magic+strings) and [here](https://deviq.com/magic-strings/) explaining what I call magic string. – Renatas M. Nov 26 '18 at 16:01
  • @Reniuz; I see where you're heading. Well, then maybe you're right, although the term "magic strings" normally refers to hidden values that trigger a certain functionality, so user-input that you think the user will never provide (like easter egg or entering "debug" mode of an app). The _magic_ string opens the door in this case. – Tim Schmelter Nov 26 '18 at 16:09
  • Thank guys! I tried to search before posting but didn't know how to ask the question. – UndercoverNoob Nov 26 '18 at 17:15

0 Answers0