Reordering method parameters with Visual Studio and CodeRush
Sometimes you may find that various method parameters are ordered illogically, or when method parameters appear in a different order relative to one another, similar methods. In this case, if you prefer the parameters appear in a consistent and logical order, you must not only change the order in the method signature but also update all calls to the method. Take into account complex cases when you can move return values of a function into our parameters and vice versa. Refactoring such complex cases is a rather complex and time-consuming task, especially if there are too many calls.
It has a great animated interactive phase where you choose parameter positions that you’ve never seen before (unless you’ve used this CodeRush refactoring earlier). Here is it in action:
Once the refactoring is applied, all method calls are modified automatically in the entire solution along with method overloads, if any.
Of course, you can use the built-in Visual Studio refactoring version. The built-in refactoring command provides a dialog window that allows you to change the order of the parameters for a method as follows:
The dialog window shows the method’s parameters. To change the order of parameters, click the item that you wish to move in the list. Then, click the up or down arrow buttons to move the selected item. A preview method signature is displayed as you make changes. Once you are done with the new signature, click the OK button to perform the updates.
All in all, it’s up to you to decide which version of the refactoring – CodeRush or Visual Studio, is more convenient. However, if we compare the Reorder Parameters refactoring shipped in CodeRush and the Reorder Parameters refactoring built-in to Visual Studio (VS refactoring), we see the following advantages in the refactoring shipped in CodeRush:
- The VS refactoring does not allow you to move out parameters to a return value, but CodeRush refactoring does;
- The VS refactoring does not allow you to convert the return value to an out parameter, but CodeRush refactoring does;
- The CodeRush version of the refactoring provides an advanced preview hint of changes that are being applied to the resulting method declaration, for instance, if you are moving the return value to an out parameter for this method:
The refactoring must change all return statements and assign the returned value to the new out parameter as follows:
The CodeRush refactoring has several options available on the Editor | Refactoring | Reorder Parameters option page in the Options Dialog. You can tweak the following options for this refactoring:
- The ‘Animate parameter reordering’ option specifies whether or not an interactive animation is enabled.
- The ‘Use hint for the code changes’ option specifies whether or not additional preview hints are enabled.
- The ‘Leave changed code as comments’ option specifies whether or not modified code changes should be left in comments for a future code review.