Dan Raimond

Dan Raimond

  • NA
  • 10
  • 539

is this code good? can be better?

Feb 1 2017 4:58 PM
Please check if this piece of code if good or could be better if yes then how and why?
 I think its purpose is pretty obvious so I will skip this part. The only thing I can think of is using while instead of for. Anything else can be done better or more efficient way?
 
  1. public static string GetUniqueName(List<string> existedNames, string name)  
  2. {  
  3.     name = Regex.Replace(name, @" \([0-9]+\)$""");  
  4.   
  5.     if (existedNames.Any(n => n == name) == false)  
  6.         return name;  
  7.       
  8.     for (int j = 0; j < 1000; j++)  
  9.     {  
  10.         var newName = name + " (" + j + ")";  
  11.         if (existedNames.Any(n => n == newName) == false)  
  12.         {  
  13.             return newName;  
  14.         }  
  15.     }  
  16.   
  17.     return "";  
  18. }  
 

Answers (2)