csharpier icon indicating copy to clipboard operation
csharpier copied to clipboard

How can I avoid wrapping the Hint typing comment in Dictionary?

Open seobilee opened this issue 6 months ago • 3 comments

Input:

public class ClassName
{
    public Dictionary<string /*key*/, int /*value*/> Dict { get; set; }
    
    public bool DoSomething(Dictionary<string /*key*/, int /*value*/> parameter1)
    {
    }
}

Output:

public class ClassName
{
    public Dictionary<
        string /*key*/
        ,
        int /*value*/
    > Dict { get; set; }

    public bool DoSomething(
        Dictionary<
            string /*key*/
            ,
            int /*value*/
        > parameter1
    ) { }
}

Expected behavior:

public class ClassName
{
    public Dictionary<string /*key*/, int /*value*/> Dict { get; set; }
    
    public bool DoSomething(Dictionary<string /*key*/, int /*value*/> parameter1)
    {
    }
}

Or maybe you guys have some good ideas?

seobilee avatar Jun 25 '25 15:06 seobilee

One way to get better formatting is to move the comment like so.

public class ClassName
{
    public Dictionary<
        string, /*key*/
        int /*value*/
    > Dict { get; set; }

    public bool DoSomething(
        Dictionary<
            string, /*key*/
            int /*value*/
        > parameter1
    ) { }
}

What I prefer is a naming convention that tells me what the key and value are.

var nameBySSN = new Dictionary<string, string>();
var productIdsByCategoryId = new Dictionary<Guid, List<Guid>>();

I've also seen naming where the key comes first, I think something like this.

var ssnToName = new Dictionary<string, string>();
var categoryIdToProductIds = new Dictionary<Guid, List<Guid>>();

belav avatar Jun 25 '25 16:06 belav

What I prefer is a naming convention that tells me what the key and value are.

var nameBySSN = new Dictionary<string, string>(); var productIdsByCategoryId = new Dictionary<Guid, List<Guid>>(); I've also seen naming where the key comes first, I think something like this.

var ssnToName = new Dictionary<string, string>(); var categoryIdToProductIds = new Dictionary<Guid, List<Guid>>();

Thank you very much for sharing your perspective. I wholeheartedly agree with your point. However, in this particular case, might we enhance clarity by providing just a bit more context?

For example:

var shippingList = new Dictionary<Guid /*userId*/, List<Guid> /*itemGuids*/>();

From my experience, explicitly stating the underlying purpose can sometimes be more informative than relying solely on the variable name. I did consider using aliases (akin to typedef in C++) or record, but since they aren’t applicable in every scenario, I often find this inline-comment approach useful.

In any event, I truly appreciate your insight. If this suggestion doesn’t quite fit the intended purpose or context, please feel free to disregard it. 🙏

seobilee avatar Jun 26 '25 03:06 seobilee

If this suggestion doesn’t quite fit the intended purpose or context, please feel free to disregard it. 🙏

I'm not opposed to csharpier formatting a comment of this form in this manner, but the way trailing comments are currently implemented I don't know if it is feasible. There are also enough other issues I want to address that it probably wouldn't be prioritized for quite a while.

belav avatar Jun 26 '25 04:06 belav

This issue is stale because it has been open 180 days with no activity and was not assigned a label or milestone. Leave a comment if this still needs to be addressed or this will be closed in 7 days.

github-actions[bot] avatar Dec 20 '25 01:12 github-actions[bot]