Why you should take care of Rider’s suggestions

I must say I am a Rider addict. Or, to be fair, a JetBrains IDE addict. I am still expecting the Dromedary IDE for Perl. Jokes aside, I use Rider and PyCharm often. They are my default IDE for C# and Python. I also love to use the code refactoring suggestions. Not just because they (often) make the code more readable but also because it allows me to learn new language features and different syntax enhancements.

But today, at work, I found a problematic suggestion. At first I just accepted blindly the suggestion, but after discussing with a colleague, we found out we can’t really do that.

The complete code for my test case is presented at the bottom of this post. But this is the main situation:

Take note on Rider’s warning. It says: Method with optional parameter is hidden by overload. While at first it makes sense, it does not. Remember that in C# you have named arguments. Thus, you can call this method and take advantage of that default value:

var invoice = new Invoice();
invoice.AddLine("Burger", 8.75);
invoice.AddLine("Coke", 2, quantity: 2);
invoice.Show();
C#

It is interesting that, even after I added this code, Rider is not able to understand that I can’t really delete the default value.

Thus, please take care, and look properly to Rider’s suggestions before applying them.

For those who argue that this is a bad practice, note that your first implementation, without the optional parameters, can take advantage of knowing the lack of arguments to have a more efficient implementation.

Complete Sample Code
var invoice = new Invoice();
invoice.AddLine("Burger", 8.75);
invoice.AddLine("Coke", 2, quantity: 2);
invoice.Show();

public class Invoice
{
    private readonly List<Line> _lines = new();

    public void AddLine(string description, double amount)
        => _lines.Add(new Line(description, amount, 0, 1, amount));

    public void AddLine(string description, double amount, double discount = 0, double quantity = 0)
        => _lines.Add(new Line(description, amount, discount, quantity, amount * quantity * (1 - discount)));

    public void Show()
    {
        _lines.ForEach(l => Console.WriteLine($"{l.Description} -> {l.NetValue} x {l.Quantity} - {l.Discount} = {l.TotalValue}"));
    }
}

internal record Line
{
    internal readonly string Description;
    internal readonly double NetValue, Discount, Quantity, TotalValue;

    public Line(string description,  double netValue, double discount, double quantity, double totalValue)
    {
        Discount = discount;
        Quantity = quantity;
        TotalValue = totalValue;
        NetValue = netValue;
        Description = description;
    }
}
C#

Leave a Reply