Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should these arguments be added or removed?

When Resharper argues with itself, how does one know which persona to give more credence?

I think I have found some code that does confuse Resharper (this is apparently a very unusual case - after using it for a day, I think Resharper is the bee's knees/the greatest thing since liquified bread, etc.).

With this line of code:

ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));

Resharper tells me to "add argument name 'rgbkey'" and then "add argument name 'rgbIV'"

So that the line is then:

ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(rgbKey: SecretKey.GetBytes(32), rgbIV: SecretKey.GetBytes(16));

When I run Resharper again, though, it says,

"Redundant argument name specification" - "Remove argument name specification" (rgbkey) (and then rgbIV).

It seems either way works fine, though...

like image 666
B. Clay Shannon-B. Crow Raven Avatar asked Jun 08 '12 17:06

B. Clay Shannon-B. Crow Raven


2 Answers

Explicit parameter naming is optional on mandatory parameters, so both forms are "correct," the question is which do you like better? Like vcsjones said, Resharper is just giving you some refactoring options to suit your preferences.

like image 185
Daniel Moore Avatar answered Oct 05 '22 22:10

Daniel Moore


Resharper tells me to

Actually, it doesn't. There are (broadly) two categories of things R# communicates to the user: things it thinks the user should do, and things that the user might want to do, that it can facilitate being done more quickly.

An example of the first:

var i = 4;
i = 5;
DoSomething(i);

The assignment of 4 will produce the "Assignment is not used" inspection, with a light bulb icon in the left margin, offering a quick-fix action to fix it (by removing the assignment).

An example of the second:

if ((new Random()).Next() > 5)
{
    DoSomething();
}
else
{
    DoSomethingElse();
}

Positioning the cursor on the if will produce a pencil icon in the left margin, offering a context action to invert the if. It's not saying you should - it's saying, "hey, if you want to do this, just select this menu item and I'll do it for you".

Adding an argument name is in the second category, a context action. If you don't want to be offered it, you can turn it off in ReSharper | Options | Code Editing | C# | Context Actions. For Code Inspections, the popup menu itself offers the opportunity to change the inspection severity; or you can look at all of them in ReSharper | Options | Code Isnpection | Inspection Severity.

Personally there are some context actions I don't think I've ever used (eg "convert to hex"), but there are others that I find invaluable for speedy coding (various combinations of switching between ?: and if and inverting, for example)

like image 31
AakashM Avatar answered Oct 05 '22 21:10

AakashM