1

I allow my user to create profiles, meaning they potentially could create some with duplicate names. This can be problematic so I want to prevent them from doing so. I coded up this first pass recursive-ish algorithm for detecting what number copy I should assign to the Name, however I'm almost certain there is a better way to write it, but my writers block has dropped the hammer down on me--in fact, I wasn't even sure how to word the question or Google something that might already exist. Here is the code:

//Check to see if there is another profile with the same name
var exiProf = profiles.FirstOrDefault(p => p.Name == newProf.Name);
int counter = 1;
while (exiProf != null)
{
    // Only match with the last number surrounded with parens
    Regex exp = new Regex(@"\((\d)\)$");
    Match ma = exp.Match(exiProf.Name);
    if(ma.Success)
    {
        num = int.Parse(ma.Groups[1].Value);
        var numChar = (counter).ToString();
        var nextNumChar = ( ++counter).ToString();
        newProf.Name = newProf.Name.Replace(numChar, nextNumChar);

        //Loop to make sure new name doesn't already exist
        exiProf = profiles.FirstOrDefault(p => p.Name == newProf.Name);
    }
    else
    {
        newProf.Name += " (" + num + ")";

        //Loop to make sure new name doesn't already exist
        exiProf = profiles.FirstOrDefault(p => p.Name == newProf.Name);
    }
}

profiles.Add(newProf);

Meaning, if I try to add "New", "New", "New", "New" in that order, "New (1)", "New (2)", "New (3)", "New (4)" is what shows up in my list.

Note: I have tagged algorithms in here because I am capable of turning pseudocode into C#.

  • 4
    Generally the approach to something like this, since it is closely linked to authentication and security, is to simply tell the user that the profile indicated already exists and leave it at that. Otherwise, you deal with generated ids that, for all intents and purposes, are guaranteed to never clash and "name" becomes nothing more than a unique description. – Neil Nov 11 '14 at 7:09
  • Very good point! – Kcvin Nov 11 '14 at 7:21
  • 3
    If you store your profiles in an Sql database. Why don't you just use an unique index for profile? – Esben Skov Pedersen Nov 11 '14 at 9:35
  • @EsbenSkovPedersen I'm not using an SQL database, or any database for that matter, which is why I never mentioned it. Your comment is off-topic. – Kcvin Nov 11 '14 at 17:38
  • It is just a question. Yes my comment is off topic because the question is off topic since it is a better fit for codereview.stackexchange.com . Also there is nothing recursive about the algorithm. – Esben Skov Pedersen Nov 11 '14 at 21:28
1

I would think this will do it:

if (profiles.Any(p => p.Name == newProf.Name))
{
    string template = newProf.Name + " ({0})";
    int counter = 1;
    string candidate = String.Format(template, counter);

    while (profiles.Any(p => p.Name == candidate))
    {
        counter++;
        candidate = String.Format(template, counter);
    }

    newProf.Name = candidate;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.