Resharper - keep named parameters when doing code cleanup
Asked Answered
F

5

16

We've adopted a convention that when calling a C# function with a "non-obvious" parameter, we use a named parameter even when it's not necessary.

E.g.

obj.Process(save: true)

rather than

obj.Process(true)

While it's unnecessary, it makes it a lot easier when glancing through the code to see what's going on, particularly with booleans or magic numbers.

However, resharper's code cleanup has a habit of removing these. I haven't been able to find a way to tell it to keep named parameters - is there one?

Floruit answered 8/7, 2015 at 16:28 Comment(0)
V
15

Although you can achieve it by doing what @EricWalker said, I want to propose another option.

You can start up the ReSharper options, look for Inspection Severity then go to Redundant explicit argument name specification and change this to do not show. This way you won't lose all the other good cleanups (like removing full name qualifiers) that remove redundant code offers.

Vinni answered 8/7, 2015 at 18:11 Comment(4)
Thanks, that's exactly what I want (especially the fact that it can deal separately with constants and other expressions, which is our most common case). A shame you can't say "hint but don't cleanup".Floruit
Thanks @Matthias, figured there had to be a better way, but had no idea the Inspection Severity and Code Cleanup were linked like that. Learned something new today.Palumbo
In ReSharper Ultimate 2016.1.2 this I had to make 'Use preferred argument style for named expressions' -> 'Do not show'Schorl
Haven't gotten this working with .editorconfig. Anyone know the right lines for that? Resharper CLI continues to blow away our named parameters when running anything but the most basic built-in format.Komarek
L
3

In ReSharper 2018.1

There are two relevant steps. You will likely want to do both, but it depends on how you want ReSharper configured.

First, in Resharper -> Options -> Code Inspection -> Inspection Severity, disable the "Use preferred argument style for literal values" code style. (For bools, "[..] for literal values" is the relevant setting, though I chose to disable all of them.)

inspection-severity

This setting is also linked to the ReSharper -> Options -> Code Editing -> Code Style -> Arguments settings, so these should now be automatically changed to "Do not show" instead of "Hint":

code-style-hints

Second, the default ReSharper Code Cleanup profile cannot be used due to the "Apply arguments style (named vs. positional)" - this option must be disabled in your code cleanup profile.

code-cleanup-settings

Lastex answered 28/8, 2018 at 22:42 Comment(1)
Speaking strictly in context of clean-up (for example to use the cleanupcode.exe cmd tool), the only necessary step is to duplicate and modify the "Full Cleanup" profile to not do the "Apply arguments style (named vs. positional)".Iceskate
Y
2

To show argument names in your method calls, goto:

ResharperOptionsCode EditingC#Syntax StyleArguments

Then set all dropdown values to "Named Argument."

Also, check "Skip single arguments" to show named parameters for the method only when there is more than one parameter.

enter image description here


The above approach was verified on Resharper version 2020.2.4

Yasmin answered 9/11, 2020 at 18:30 Comment(0)
P
1

The setting you're looking for is under Code Cleanup\C#\Remove code redundancies

I know that's probably not the answer you were hoping for, but you can stop it removing your parameter names by unchecking that setting (along with leaving behind every other redundancy.)

You might be able to setup different profiles in Code Cleanup to work around the issue, but you'd have better luck asking JetBrains folks for solutions.

HTH,
Eric

Palumbo answered 8/7, 2015 at 17:36 Comment(0)
D
1

UPDATE:

It seems that this solution no longer works starting with v2017.1.3 (2017-08-28)


I'm currently using ReSharper v2017.1 (2017-06-01) and it seems JetBrains hasn't solved this problem yet.

As @Colin Harkness noticed, currently the last resort for keeping "named parameters" is to set the option "Named expressions (variables, properties, methods, etc)" to "Named argument".

This is certainly not the best way out.

enter image description here

UPDATE:

I a little trick found at JetBrains' forum. You can cancel considering named parameters as a redundancy by adding this line of code at the top of file.

// ReSharper disable ArgumentsStyleNamedExpression

You have to do some minor configuration within ReSharper settings. In order to keep automatic addition of the // ReSharper disable ArgumentsStyleNamedExpression simple, I have added this instruction to File Header Text as is shown in fig. 2.

enter image description here Fig.2 - Add ArgumentsStyleNamedExpression Rule

After that, you have to check Update File Header option in Code Cleanup Configuration as is shown in fig. 3

enter image description here Fig.3 - Check "Update File Header" option

In this case, when a Code Cleanup starts, it first adds ArgumentsStyleNamedExpression rule, and applies code style to file.

After adding this rule, you can go to Tools | Options | Environment | Fonts and Colors | ReSharper Parameter Identifier and change the highlighting color for this case in order to visually distinguish arguments and parameters names as is shown in fig 4.

enter image description here

Fig.4 - Parameter name highlighting


Unfortunately, this way of keeping arguments' names doesn't always work (ReSharper can selectively keep/remove names of arguments).

Desecrate answered 1/6, 2017 at 10:14 Comment(1)
@MarcelPopescu, I've tried it with v2017.1.3. It seems that this solution no longer works.Desecrate

© 2022 - 2024 — McMap. All rights reserved.