Should I avoid magic strings as much as possible?

I have the following code snippet:

    internal static string GetNetBiosDomainFromMember(string memberName)
    {
        int indexOf = memberName.IndexOf("DC=", StringComparison.InvariantCultureIgnoreCase);
        indexOf += "DC=".Length;
        string domaninName = memberName.Substring(indexOf, memberName.Length - indexOf);

         if (domaninName.Contains(","))
         {
             domaninName = domaninName.Split(new[] { "," }, StringSplitOptions.None)[0];
         }

         return domaninName;
     }

I am doing some disassembly for AD, so I have some lines, such as "DC =", "objectCategory =", "LDAP: //", ",", ".". anyway. I found the code above to be more readable than the code below: (You can find the opposite, let me know.)

    private const string DcString = "DC=";
    private const string Comma = ",";

    internal static string GetNetBiosDomainFromMember(string memberName)
    {
        int indexOf = memberName.IndexOf(DcString, StringComparison.InvariantCultureIgnoreCase);
        indexOf += DcString.Length;
        string domaninName = memberName.Substring(indexOf, memberName.Length - indexOf);

         if (domaninName.Contains(CommaString))
         {
             domaninName = domaninName.Split(new[] { CommaString }, StringSplitOptions.None)[0];
         }

         return domaninName;
     }

Even I can have "DC" and "DC =", I have to think in the names of these variables or divide them into two parts :( Then my question is: Should magic lines be avoided?

UPDATED .

Some conclusions:

  • , . : , , , .
  • , ( ).
  • . , "," . , , , "," ". ( , , resharper ).
  • , . , ( Javadocs). .
+5
1

, , , "DC" "objectCategory", . , , .., , . .

, , - , , , . , , . , - , .

+1

All Articles