The internal build 1 of JustCode comes with three new refactorings to easily manipulate method parameters.

Move to Parameter

There are times when one has hardcoded a constant value within the body of a method, but the method would have more flexibility if the value were a parameter instead. This refactoring allows you to move the variable to a parameter, and it will automatically update calls to the method with the defined value for the variable.

 

class Program
{ static void Main(string[] args) { Ninja ninja = new Ninja(); ninja.Equip(Weapon.Katana); } } public class Ninja
{ Dictionary<Hand, Weapon> weapons = new Dictionary<Hand, Weapon>(); public void Equip(Weapon weapon) { var hand = Hand.Right; weapons[hand] = weapon; } }
 

Hover over the hand variable and drop down the smart tag. Choose F and select “Move to Parameter.”

 

 

class Program
{ static void Main(string[] args) { Ninja ninja = new Ninja(); ninja.Equip(Weapon.Katana, Hand.Right); } } public class Ninja
{ Dictionary<Hand, Weapon> weapons = new Dictionary<Hand, Weapon>(); public void Equip(Weapon weapon, Hand hand) { weapons[hand] = weapon; } }
 

The variable has now been made a parameter, and calls to the method have been updated with the value originally used by the variable. This maintains expected behavior for the system. If it had used a different default values, tests would have broken as a result of this refactoring.

Extract Class From Parameters

When two or more things are commonly passed together (e.g. Weapon and Hand), it may be time to extract them into a class of their own. This task is quite simple with the Extract Class from Parameters refactoring. Place your cursor in the parameter list and select it from the list of refactorings.

 

 

class Program
{ static void Main(string[] args) { Ninja ninja = new Ninja(); ninja.Equip(new EquipParameters(Weapon.Katana, Hand.Right)); } } public class Ninja
{ Dictionary<Hand, Weapon> weapons = new Dictionary<Hand, Weapon>(); public void Equip(EquipParameters equipParameters) { weapons[equipParameters.Hand] = equipParameters.Weapon; } }
 

Of course, if the class has more meaning than its usage as parameters to the Equip method, then a Rename Refactoring is in order and appropriate functionality should be defined on the new class

Reverse Assignments

What does reversing assignments have to do with parameters? Nothing really, except it comes it handy when making mappers that accept arguments.

 

public static class Map
{ public static void NinjaToPerson(Ninja ninja, Person person) { person.FirstName = ninja.GivenName; person.LastName = ninja.FamilyName; } }

Copy the method, rename it to PersonToNinja. Then, refactor the parameter list using the Move or Delete Parameter refactoring. Change the position of the parameters. Highlight the assignment areas, then select Reverse Assignment(s).

 

 

public static void PersonToNinja(Person person, Ninja ninja)
{
    ninja.GivenName = person.FirstName;
    ninja.FamilyName = person.LastName;
}
 

Conclusion

These are a few new things in the first internal release for JustCode that you will see in the upcoming Q2 release. Stay tuned for more as Q2 ramps up!


About the Author

Chris Eargle

is a Microsoft C# MVP with over a decade of experience designing and developing enterprise applications, and he runs the local .NET User Group: the Columbia Enterprise Developers Guild. He is a frequent guest of conferences and community events promoting best practices and new technologies. Chris is a native Carolinian; his family settled the Dutch Form region of South Carolina in 1752. He currently resides in Columbia with his wife, Binyue, his dog, Laika, and his three cats: Meeko, Tigger, and Sookie. Amazingly, they all get along... except for Meeko, who is by no means meek.

Related Posts

Comments