Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overzealous String Constants

I am sure this has been asked before, but in this example I am curious as to what usefulness others can see in separating constants to this degree:

public class CoreStringConstants
 {
  // Common strings
  public const string SPACE = " ";
  public const string PERIOD = ".";
  public const string COMMA = ",";
  public const string COLON = ":";
  public const string SEMI_COLON = ";";
  public const string HYPHEN = "-";
  public const string UNDER_SCORE = "_";
  public const string LEFT_BRACKET = "(";
  public const string RIGHT_BRACKET = ")";
    public const string LEFT_SQUARE_BRACKET = "[";
    public const string RIGHT_SQUARE_BRACKET = "]";
    public const string LEFT_CURLY_BRACKET = "{";
    public const string RIGHT_CURLY_BRACKET = "}";
    public const string PIPE = "|";
    public const string CIRCUMFLEX = "^";
    public const string ASTERISK = "*";

... Really?

Is there really any benefit to separating these kinds of string constants from your code?

When is the character used for ASTERISK going to change in the foreseeable lifetime of the application?

like image 673
dwerner Avatar asked Mar 03 '26 00:03

dwerner


1 Answers

Actually, I'd consider this a disadvantage. Benefits are dubious to say the least, especially since there is support for pooling string literals anyway, and as a developer I'd have to look up the value of each constant when I encounter code like this for the first time.

Additionally, someone will come up with code that reads

public const string COLON = "*";
like image 172
Jim Brissom Avatar answered Mar 04 '26 12:03

Jim Brissom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!