Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7956049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:52:41+00:00 2026-06-04T03:52:41+00:00

I have the next piece of code: internal static string GetNetBiosDomainFromMember(string memberName) { int

  • 0

I have the next piece of code:

    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 making some parsings for AD, so I have some strings like “DC=”, “objectCategory=”, “LDAP://”, “,”, “.” so and so.
I found the above code more readable than the code below:(You may found the opposed, 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 may have “DC” and “DC=”, I should think in the names for this variables or divide these in two :(. Then my question:
Should I avoid magic strings as possible?

UPDATED.

Some conclusions:

  • There are ways to avoid using strings at all, which might be better. To achieve it could be used: static classes, enumerators, numeric constants, IOC containers and even reflection.
  • A constant string help you to ensure you don’t have any typos (in all references to a string).
  • Constant strings for punctuation don’t have any global semantic. Would be more readable to use these as they are “,”. Use a constant for this case may be considered if that constant may change in the future, like change “,” by “.” (Have a constant may help you in that refactoring although modern tools as resharper do this without need of a constant or variable).
  • If you only use it string once you do not need to make it into a constant. Consider however that a constant can be documented and shows up in documentation (as Javadocs). This may be important for non-trivial string values.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:52:42+00:00Added an answer on June 4, 2026 at 3:52 am

    I would certainly make constants for the actual names like “DC” and “objectCategory”, but not for the punctuation. The point of this is to make sure you don’t have any typos and such and that you can easily find all of the references for the places that use that magic string. The punctuation is not really part of that.

    Just to be clear, I’m assuming the magic strings are things that you have to deal with, that you don’t have the option of making them a number defined by a constant. As in the comment to your question, that’s always preferable if that’s possible. But sometimes you must use a string if you have to interface with some other system that requires it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple piece of code: public string GenerateRandomString() { string randomString =
I have the next piece of code: $(#slider-cpu).slider({ range: min, value: 0, min: 0,
I have the next piece of code, one iVar with this property retained and
I have next code require 'rack/rpc' class Server < Rack::RPC::Server def hello_world Hello, world!
I have one piece of Cocoa code I wrote that takes in an XML
I have this piece of code // TR Fading when deleted $('.delete').live('click', function() {
I have this same piece of code in two different parts of my app.
Hi all I have a piece of code which looks like this: public class
I have a piece of code that presents an interesting question (in my opinion).
I have following piece of code for reversing the linked list. I am getting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.