Home > front end >  Optimize the Algotithm
Optimize the Algotithm

Time:03-17

So, i have a method

public void AddToSearch(List<FullName> fullNames)
        {
            foreach (var fullName in fullNames)
            {
                if (fullName.Surname != null)
                    _sb.Append(fullName.Surname.Trim()   " ");
                if (fullName.Name != null)
                    _sb.Append(fullName.Name.Trim()   " ");
                if (fullName.Patronymic != null)
                    _sb.Append(fullName.Patronymic.Trim());
                fullNamesList.Add(_sb.ToString().TrimEnd());
                _sb.Clear();
            }

it takes a list of FullName and by using StringBuilder instance converts each element into a string(which format is "$Surname $Name $Patronymic"). At the end i put the result into my list. The Question is - how can i optimize all of that "Trim" stuff. It bothers me that i use it in multiple occassions and i am pretty sure it effects the time.

CodePudding user response:

how can i optimize all of that "Trim" stuff

Very simple, simply don't call Trim() on those strings. What spaces are you worried about? Who's entering those values in your business objects? Because short of solar flares randomly flipping bits enough to append spaces to your strings, you're in full control from beginning to end, so simply don't add the spaces.

You also don't need the two string builders, just insert in your main one. There's no need for yet another Trim() here either, because simply decrementing the Length property of your string builder is a constant operation (it literally decrements one integer with guaranteed no extra allocations).

CodePudding user response:

Try and extension something like this.

public static class Helper
{
  public static StringBuilder AppendValue(this StringBuilder builder,string value)
  {
    if(!string.IsNullOrEmpty(value))
    {
      builder.Append(value.Trim());
      return builder;
    }
  }
}

call as follows:

sb.AppendValue(fullName.Name);
sb.AppendValue(fullName.Surname);
...

You will get the StringBuilder back with the value if it is not empty otherwise nothing will be added to it.

CodePudding user response:

the strings normalization process should be done in the data layer (in application or database) for stored strings. While dynamic strings such as user input, needs to be normalized as soon as you get them to prepare them for the next task.

For your current code, you can modify the FullName class, adjust the setters to trim the value before it's been stored, and override the ToString to return the full name.

Example :

public class FullName
{       
    public string Name 
    {
        get => Name;
        set => Name = value?.Trim();
    }
    
    public string Surname 
    {
        get => Surname;
        set => Surname = value?.Trim();
    }
    
    public string Patronymic 
    {
        get => Patronymic;
        set => Patronymic = value?.Trim();
    }

    public override string ToString()
    {
        return $"{GetValueOrEmpty(Surname)}{GetValueOrEmpty(Name)}{GetValueOrEmpty(Patronymic, false)}";
    }
    
    private string GetValueOrEmpty(string name, bool addSpaceAfter = true)
    {
        if(!string.IsNullOrWhiteSpace(name))
        {
            return name   (addSpaceAfter ? " " : string.Empty);
        }
        
        return string.Empty;
    }
}

Then, you can do this :

fullNamesList.AddRange(fullNames.Select(x=> x.ToString()));
  • Related