2
\$\begingroup\$

I have this logic that seems complicated checking the length and adding a colon. I'd like to see if any of you C# experts can help me simplify it. If the replace remove the full text then I am just adding the word Result. If it removes the make then I am just displaying the model.

Is there a way I can remove the if statement and combine into one?

namespace DupName
{
    public class Program
    {
        static void Main(string[] args)
        {
            StringBuilder childName = new StringBuilder();

            List<Item> items = new List<Item>()
                .AddAlso(new Item { Id = 1, Name = "Audi", ChildName = "Audi A3 Premium Plus" })
                .AddAlso(new Item { Id = 2, Name = "Audi", ChildName = "Audi" })
                .AddAlso(new Item { Id = 1, Name = "Audi", ChildName = "Audi" })
                .AddAlso(new Item { Id = 2, Name = "Audi", ChildName = "S5 Premium Plus 3.0 TFSI quattro" })
                .AddAlso(new Item { Id = 2, Name = "Audi", ChildName = "S3 Premium Plus 3.0 TFSI quattro" });

            foreach (var item in items)
            {

                string temp = item.ChildName.Replace(item.Name, "").Trim();

                if (temp.Length == 0)
                {
                    Console.WriteLine(string.Format("{0}","Result"));

                }else
                {

                    Console.WriteLine(string.Format("{0}: {1} - {2}", "Result", temp, item.Name));
                }
            }
        }
    }

    public static class Extensions
    {
        public static List<T> AddAlso<T>(this List<T> list, T item)
        {
            list.Add(item);
            return list;
        }
    }

    public class Item
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string ChildName { get; set; }
    }

Output

Result: A3 Premium Plus - Audi
Result
Result
Result: S5 Premium Plus 3.0 TFSI quattro - Audi
Result: S3 Premium Plus 3.0 TFSI quattro - Audi
\$\endgroup\$
6
  • 1
    \$\begingroup\$ Welcome to Code Review. To get a useful review, you should edit your question so that it follows the recommendations for questions. \$\endgroup\$ Commented Feb 7, 2020 at 18:40
  • 1
    \$\begingroup\$ please clarify your question. Simplify what exactly ? what is wrong with temp.Length == 0 ?what complications that you are experiencing ? and what do you want to achieve ? \$\endgroup\$
    – iSR5
    Commented Feb 7, 2020 at 20:53
  • \$\begingroup\$ Just wanted to see if there is a better way to do that \$\endgroup\$
    – Jefferson
    Commented Feb 7, 2020 at 21:10
  • 2
    \$\begingroup\$ @Jefferson: To do what exactly? You haven't explained the goal of the code, nor exactly what you want reviewed (if not the full code) \$\endgroup\$
    – Flater
    Commented Feb 7, 2020 at 23:32
  • \$\begingroup\$ Why have you not taken into account any of the reviews of codereview.stackexchange.com/q/235780/10582 , which is basically the same code as you've posted here? \$\endgroup\$
    – BCdotWEB
    Commented Feb 10, 2020 at 17:49

2 Answers 2

3
\$\begingroup\$

Is there a way I can remove the if statement and combine into one?

Not sure if you can remove it completely, but you may simplify it by using the ternary operator (:?) and extracting the logic into a method within the Item class (see example below) or into it's own class which make sense if the logic is more complicated.

Furthermore, you could

  • use string interpolation for concatenate strings which makes the code more readably.
  • Use array initialization syntax which makes the AddAlso extension obsolete.

Restructuring the code using the suggestions above could look like that:

public class Program
{
    static void Main(string[] args)
    {
        StringBuilder childName = new StringBuilder();

        var items = new List<Item>
        {
                new Item { Id = 1, Name = "Audi", ChildName = "Audi A3 Premium Plus" },
                new Item { Id = 2, Name = "Audi", ChildName = "Audi" },
                new Item { Id = 1, Name = "Audi", ChildName = "Audi" },
                new Item { Id = 2, Name = "Audi", ChildName = "S5 Premium Plus 3.0 TFSI quattro" },
                new Item { Id = 2, Name = "Audi", ChildName = "S3 Premium Plus 3.0 TFSI quattro" },
        };

        items.ForEach(x => Console.WriteLine(x.ToDisplayName()));
    }
}

public class Item
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string ChildName { get; set; }

    public string ToDisplayName()
    {
        string childNamePrefix = ChildName.Replace(Name, "").Trim();
        return childNamePrefix.Length == 0
            ? "Result: -"
            : $"Result: {childNamePrefix} - {Name}";
    }
}
\$\endgroup\$
0
5
\$\begingroup\$

If I understand correctly, your frustration is with the necessary computation for knowing whether to print Result or Result: temp - childname. That's because you're doing more steps than necessary.

Instead of:

  1. Calculating the (ChildName - Name) string
  2. Trimming any whitespaces in the resulting string
  3. And then checking if this string is empty

You could just check if both the Name and ChildName are equal. If they are, your variable temp isn't used. Also, since result is written independently of such calculation, you could always print it.

So, I would suggest exchanging

foreach (var item in items) {
    string temp = item.ChildName.Replace(item.Name, "").Trim();
    if (temp.Length == 0) {
        Console.WriteLine(string.Format("{0}","Result"));
    } else {
        Console.WriteLine(string.Format("{0}: {1} - {2}", "Result", temp, item.Name));
    }
}

for

foreach (var item in items) {
    Console.Write("Result");

    if (item.Name != item.ChildName) {
        string childNameWithoutName = item.ChildName.Replace(item.Name, "").Trim();
        Console.Write(string.Format(": {1} - {2}", childNameWithoutName, item.Name));
    }

    Console.WriteLine();
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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