RefactorMCP catalog

Inline Constant

Replaces every use of a constant field, across the solution, with its value and removes the constant.

Precondition

  • The target is a const field.
  • No use of it is inside nameof, which needs a symbol rather than a value.

Transformation

  • Each use, including a qualified one such as Limits.MaxItems, is replaced by the constant's initialiser.
  • Names in the initialiser are qualified as each use needs them: Base * 2 declared in Limits becomes Limits.Base * 2 in another type.
  • The value is parenthesised only where the use binds more tightly than the initialiser, as in total / (Limits.Base * 2).
  • A constant declared alone is removed with its comments, and the blank lines around it close up. A constant declared alongside others is removed from the declaration, which keeps the rest.

Preserved

  • Every value the code computes.
  • Comments at each use.

Limitations

  • Code outside the solution that uses a public constant is not updated.
  • Preprocessor directives in the constant's leading trivia are removed with it.

Error codes

CodeMeaning
not-a-constantthe targeted field is not const
used-in-nameofa use of the constant is inside nameof

Cases

·

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

#across-files

Uses in other files are replaced too; names in the value are qualified where the use needs it, and the value is parenthesised where precedence requires

success
Targetsymbol F:Shop.Limits.MaxItems
Basket.csmodified
11 namespace Shop
22 {
33 public class Basket
44 {
5− public int Remaining(int count) => Limits.MaxItems - count;
5+ public int Remaining(int count) => Limits.Base * 2 - count;
66
7− public int Share(int total) => total / Limits.MaxItems;
7+ public int Share(int total) => total / (Limits.Base * 2);
88 }
99 }
Limits.csmodified
11 namespace Shop
22 {
33 public static class Limits
44 {
55 public const int Base = 5;
6− public const int MaxItems = Base * 2;
76
8− public static bool IsFull(int count) => count >= MaxItems;
7+ public static bool IsFull(int count) => count >= Base * 2;
98 }
109 }

#not-a-constant-rejected

Refuses a field that is not a constant

refusal
Targetsymbol F:Shop.Sample.Limit
Refusesnot-a-constant; every file is left unchanged
Sample.csinput
1 namespace Shop
2 {
3 public class Sample
4 {
5 public static readonly int Limit = 10;
6
7 public bool IsLarge(int x) => x > Limit;
8 }
9 }

#one-of-several-declarators

A constant declared alongside others is removed from the declaration, which keeps the rest

success
Targetsymbol F:Shop.Sizes.Medium
Sizes.csmodified
11 namespace Shop
22 {
33 public class Sizes
44 {
5− private const int Small = 1, Medium = 5, Large = 10;
5+ private const int Small = 1, Large = 10;
66
7− public int Total() => Small + Medium + Large;
7+ public int Total() => Small + 5 + Large;
88 }
99 }

#removes-comments-with-declaration

The constant's own comments go with it, the blank line after it does not linger, and comments at the uses stay

success
Targetsymbol F:Shop.Greeter.Greeting
Greeter.csmodified
11 namespace Shop
22 {
33 public class Greeter
44 {
5− /// <summary>What every message starts with.</summary>
6− private const string Greeting = "Hello";
7−
85 public string Greet(string name)
96 {
107 // Always polite.
11− return Greeting + ", " + name; // no punctuation
8+ return "Hello" + ", " + name; // no punctuation
129 }
1310 }
1411 }

#simple

Replaces every use of a private constant with its value and removes the declaration

success
Targetsymbol F:Shop.Session.SecondsPerMinute
Session.csmodified
11 namespace Shop
22 {
33 public class Session
44 {
5− private const int SecondsPerMinute = 60;
6−
75 public int TimeoutSeconds(int minutes)
86 {
9− return minutes * SecondsPerMinute;
7+ return minutes * 60;
108 }
119
12− public int Minutes(int seconds) => seconds / SecondsPerMinute;
10+ public int Minutes(int seconds) => seconds / 60;
1311 }
1412 }

#used-in-nameof-rejected

Refuses a constant that nameof names, since nameof needs a symbol rather than a value

refusal
Targetsymbol F:Shop.Settings.Retries
Refusesused-in-nameof; every file is left unchanged
Settings.csinput
1 namespace Shop
2 {
3 public class Settings
4 {
5 public const int Retries = 3;
6
7 public string Describe() => nameof(Retries) + " = " + Retries;
8 }
9 }