RefactorMCP catalog

Inline Parameter

When every call passes the same constant for a parameter, removes the parameter and writes the constant where the body used it.

Arguments

ArgumentMeaning
parameterthe name of the parameter to inline

The target is the method or constructor, by symbol.

Precondition

  • The method is called at least once, and only called: it is not used as a method group.
  • Every call passes a compile-time constant for the parameter, and all of them are the same value. A call that leaves an optional parameter out passes its default.
  • The body never assigns the parameter, increments it or passes it by reference.
  • The parameter is not ref, out, in or a params array.
  • The method is not virtual, abstract or an override and implements no interface member, since the other members of its hierarchy have bodies of their own.
  • The result compiles.

Transformation

  • Every use of the parameter in the body becomes the constant, written as the first call wrote it, parenthesised where precedence needs it.
  • When the constant's type is not the parameter's type, it is cast, so an argument of 2 for a double parameter becomes (double)2 and arithmetic keeps its meaning.
  • The parameter is removed as by Change Signature, dropping its argument from every call.

Preserved

  • What the method computes for every call.
  • Comments in the body and at the calls. A comment written inline before the removed argument goes with it.

Limitations

  • Only compile-time constants are inlined. A static readonly field every call passes is refused, as is any other expression.
  • The constant is copied as written, so a named constant from another namespace may need a using in the method's file.
  • The body can end up with expressions a person would fold, such as "en-GB" ?? "en" (nullable-parameter), and a constant condition can make code unreachable, which the compiler reports as a warning.

Error codes

CodeMeaning
values-differcalls pass different values
not-constanta call passes something other than a constant
parameter-assignedthe body writes to the parameter
no-callsnothing calls the method
part-of-hierarchythe method is virtual, abstract, an override or an interface implementation
method-group-referencethe method is used as a method group

Cases

·

Diffs show each file from before to after. /*[*/ … /*]*/ marks a selection and /*^*/ a caret; the runner removes them before the refactoring runs.

#calls-in-another-file

Inlines a value passed by calls in another file into every use in the body

success
Targetsymbol M:Shop.Discount.Describe(System.Decimal,System.Decimal)
Arguments
parameter"rate"
Basket.csmodified
11 namespace Shop;
22
33 public class Basket
44 {
55 public string Total(Discount discount, decimal subtotal, decimal delivery)
66 {
7− return discount.Describe(subtotal, 0.1m) + discount.Describe(delivery, 0.1m);
7+ return discount.Describe(subtotal) + discount.Describe(delivery);
88 }
99 }
Discount.csmodified
11 namespace Shop;
22
33 public class Discount
44 {
5− public string Describe(decimal price, decimal rate)
5+ public string Describe(decimal price)
66 {
7− return (price - price * rate) + " after " + rate * 100m + "% off";
7+ return (price - price * 0.1m) + " after " + 0.1m * 100m + "% off";
88 }
99 }

#constant-at-every-call

Every call passes the same literal, so the body uses the literal and the parameter goes

success
Targetsymbol M:Shop.Greeting.Greet(System.String,System.String)
Arguments
parameter"punctuation"
Greeting.csmodified
11 namespace Shop;
22
33 public class Greeting
44 {
5− public string Greet(string name, string punctuation)
5+ public string Greet(string name)
66 {
7− return "Hello " + name + punctuation;
7+ return "Hello " + name + "!";
88 }
99
1010 public string Morning()
1111 {
12− return Greet("Ann", "!") + Greet("Bob", "!");
12+ return Greet("Ann") + Greet("Bob");
1313 }
1414 }

#converts-argument-type

An argument converted to the parameter's type is inlined with a cast, so integer division does not creep in

success
Targetsymbol M:Shop.Measure.Half(System.Double,System.Double)
Arguments
parameter"divisor"
Measure.csmodified
11 namespace Shop;
22
33 public class Measure
44 {
5− public double Half(double value, double divisor)
5+ public double Half(double value)
66 {
7− return value / divisor;
7+ return value / (double)2;
88 }
99
1010 public double Run()
1111 {
12− return Half(9, 2) + Half(5, 2);
12+ return Half(9) + Half(5);
1313 }
1414 }

#no-calls-rejected

Refuses when nothing calls the method, so there is no value to inline

refusal
Targetsymbol M:Shop.Greeting.Greet(System.String,System.String)
Arguments
parameter"punctuation"
Refusesno-calls; every file is left unchanged
Greeting.csinput
1 namespace Shop;
2
3 public class Greeting
4 {
5 public string Greet(string name, string punctuation)
6 {
7 return "Hello " + name + punctuation;
8 }
9 }

#not-constant-rejected

Refuses when a call passes something other than a constant, even if every call passes the same expression

refusal
Targetsymbol M:Shop.Greeting.Greet(System.String,System.String)
Arguments
parameter"punctuation"
Refusesnot-constant; every file is left unchanged
Greeting.csinput
1 namespace Shop;
2
3 public class Greeting
4 {
5 public static readonly string Mark = "!";
6
7 public string Greet(string name, string punctuation)
8 {
9 return "Hello " + name + punctuation;
10 }
11
12 public string Morning()
13 {
14 return Greet("Ann", Mark) + Greet("Bob", Mark);
15 }
16 }

#nullable-parameter

Inlines a string into a nullable parameter's uses in a nullable context

success
Targetsymbol M:Shop.Formatter.Money(System.Decimal,System.String)
Arguments
parameter"culture"
Projectnullable enable
Formatter.csmodified
11 namespace Shop;
22
33 public class Formatter
44 {
5− public string Money(decimal amount, string? culture)
5+ public string Money(decimal amount)
66 {
7− return amount + " " + (culture ?? "en");
7+ return amount + " " + ("en-GB" ?? "en");
88 }
99
1010 public string Price()
1111 {
12− return Money(9.99m, "en-GB");
12+ return Money(9.99m);
1313 }
1414 }

#omitted-argument-uses-default

A call that leaves an optional parameter out passes its default, which matches the calls that pass it explicitly

success
Targetsymbol M:Shop.Columns.Pad(System.String,System.Int32)
Arguments
parameter"width"
Columns.csmodified
11 namespace Shop;
22
33 public class Columns
44 {
5− public string Pad(string text, int width = 10)
5+ public string Pad(string text)
66 {
7− return text.PadLeft(width);
7+ return text.PadLeft(10);
88 }
99
1010 public string Row()
1111 {
12− return Pad("name") + Pad("price", 10);
12+ return Pad("name") + Pad("price");
1313 }
1414 }

#parameter-assigned-rejected

Refuses when the body assigns the parameter

refusal
Targetsymbol M:Shop.Greeting.Greet(System.String,System.String)
Arguments
parameter"punctuation"
Refusesparameter-assigned; every file is left unchanged
Greeting.csinput
1 namespace Shop;
2
3 public class Greeting
4 {
5 public string Greet(string name, string punctuation)
6 {
7 punctuation = punctuation.Trim();
8 return "Hello " + name + punctuation;
9 }
10
11 public string Morning()
12 {
13 return Greet("Ann", "!") + Greet("Bob", "!");
14 }
15 }

#part-of-hierarchy-rejected

Refuses a virtual method, whose overrides have bodies of their own

refusal
Targetsymbol M:Shop.Greeting.Greet(System.String,System.String)
Arguments
parameter"punctuation"
Refusespart-of-hierarchy; every file is left unchanged
Greeting.csinput
1 namespace Shop;
2
3 public class Greeting
4 {
5 public virtual string Greet(string name, string punctuation)
6 {
7 return "Hello " + name + punctuation;
8 }
9
10 public string Morning()
11 {
12 return Greet("Ann", "!");
13 }
14 }

#preserves-comments

Comments in the body and at the calls stay where they are

success
Targetsymbol M:Shop.Labels.Label(System.String,System.Boolean)
Arguments
parameter"upper"
Labels.csmodified
11 namespace Shop;
22
33 public class Labels
44 {
5− public string Label(string text, bool upper)
5+ public string Label(string text)
66 {
77 // upper-case labels shout
8− return upper ? text.ToUpperInvariant() : text; // chosen by the caller
8+ return true ? text.ToUpperInvariant() : text; // chosen by the caller
99 }
1010
1111 public string Banner()
1212 {
1313 // both are headings
14− return Label("sale", true) + Label("new", /* loud */ true);
14+ return Label("sale") + Label("new");
1515 }
1616 }

#values-differ-rejected

Refuses when calls pass different values

refusal
Targetsymbol M:Shop.Greeting.Greet(System.String,System.String)
Arguments
parameter"punctuation"
Refusesvalues-differ mentioning “punctuation”; every file is left unchanged
Greeting.csinput
1 namespace Shop;
2
3 public class Greeting
4 {
5 public string Greet(string name, string punctuation)
6 {
7 return "Hello " + name + punctuation;
8 }
9
10 public string Morning()
11 {
12 return Greet("Ann", "!") + Greet("Bob", "?");
13 }
14 }