Object Oriented Programming – how to avoid duplication in processes that differ slightly depending on a variable

I would suggest encapsulating all options in one class:

public class ProcessOptions
{
    public bool Capitalise { get; set; }
    public bool RemovePunctuation { get; set; }
    public bool Replace { get; set; }
    public char ReplaceChar { get; set; }
    public char ReplacementChar { get; set; }
    public bool SplitAndJoin { get; set; }
    public char JoinChar { get; set; }
    public char SplitChar { get; set; }
}

and pass it into the Process method:

public string Process(ProcessOptions options, string text)
{
    if(options.Capitalise)
        text.Capitalise();

    if(options.RemovePunctuation)
        text.RemovePunctuation();

    if(options.Replace)
        text.Replace(options.ReplaceChar, options.ReplacementChar);

    if(options.SplitAndJoin)
    {
        var split = text.Split(options.SplitChar);
        return string.Join(options.JoinChar, split);
    }

    return text;
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)