views:

101

answers:

2

I have a class that has negative flag ("DoNot{Action}"). I would like to change this flag to positive "Do" and change all checks in the code, basically negating them. Is there such refactoring?

Here is an example. If I change the flage DoNotFilter to DoFilter all code that looks at this flag should change accordingly.

public bool DoNotFilter
{get;private set;}

changes to

public bool DoFilter
{get;private set;}

so

if (attribute.DoNotFilter == true)

need to change to

if (attribute.DoFilter == false)
+2  A: 

You might be able to get the Inline Property refactoring in ReSharper to do what you want. I'm not sure if it will work with attributes, though.

Example:

public bool DoNotFilter
{ get; private set; }

public void Foo()
{
    if (DoNotFilter == true)
    {
        Console.WriteLine("Aha!");
    }
}

Now change to:

public bool DoFilter
{ get; private set; }

public bool DoNotFilter
{ get { return !DoFilter; } }

Now refactor DoNotFilter and Inline Property:

public void Foo()
{
    if (!DoFilter == true)
    {
        Console.WriteLine("Aha!");
    }
}

If you try this, be sure to check everything in to source control first! :) No warranties!

You can also Remove redundant comparision to get the if down to:

if (!DoFilter)
TrueWill
Thanks, I might try this. Attribute was just an example, no attributes(annotations) involved
epitka
BTW - I'm assuming all the code is in one solution.
TrueWill
Yes it is all in one solution
epitka
+1  A: 

You can use Visual Studio Find and Replace dialog. To lunch the dialog press Ctrl+H.

Make sure that "Use" check box is checked and in dropdown list "Regular expression" is selected. I also would check "Search hidden text" and "Search up" check boxes.

In "Find what" type: <DoNot{:i+}

In "Replace with" type: Do\1

It will remove "Not" In all occurrences of DoNotXXX.

Next let replace

if (attribute.DoFilter == true)

with

if (attribute.DoFilter == false)

If you closed Find and Replace dialog, press Ctrl+H again.

In "Find what" type: <Do{:i+} == true

In "Replace with" type: Do\1 == false

Vadim
Vadim, thanks for your answer but the code above was just an example, and it is not as straight forward as above.
epitka

related questions