Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an automated way to catch property self-assignment?

Tags:

c#

Consider the following code:

class C
{
    public int A { get; set; }
    public int B;

    public C(int a, int b)
    {
        this.A = A;    // Oops, bug! Should be `this.A = a`. No warning
        this.B = B;    // Oops, bug! Should be `this.B = b`. `warning CS1717: Assignment made to same variable; did you mean to assign something else?`
    }
}

A and B are almost exactly the same thing, but one has a bug I will miss.

Is there a way I can get catch the first case at compile time?

EDIT: Some of the answers & comments want to explain to me that properties and fields aren't the same thing. I know that already. They explain why the compiler doesn't have a warning here; I get that. But I wrote a bug, and I don't like writing bugs. So my question is "How can I make sure I never, ever write this bug ever again?"

like image 522
Jay Bazuzi Avatar asked Oct 26 '12 04:10

Jay Bazuzi


3 Answers

Potentially you could use a tool such as FxCop and write a custom rule using VisitAssignmentStatement:
Some examples:
Example1
Example2

like image 200
coder_bro Avatar answered Nov 02 '22 04:11

coder_bro


This isn't cast-iron, but if you installed ReSharper and set its inspection for 'Unused parameter' to 'Error', and further turned its Solution-Wide Analysis on, you'd see this in the code window:

enter image description here

One of these red indicators over in the right margin:

enter image description here

And this down in the status bar:

enter image description here

Which together make a combo that as a ReSharper user you'd soon become unable to ignore :)

Wouldn't stop you compiling, though.

like image 20
AakashM Avatar answered Nov 02 '22 04:11

AakashM


Jay,

A great question. I believe most of the points which are relevant have been covered in various responses, but just to summarise:

  1. There is no way to handle this implicitly. The .Net compiler allows recursive properties, which is at the heart of this issue (in terms of not being able to 'capture' it). Note that, as per this question, that's not likely to change.
  2. As such, if you want to enforce the capture of this 'bug', you would need to do so explicitly via a 3rd party tool. I'm a ReSharper fan like many others, and that allows us to highlight property recursion where necessary. Also as others have mentioned, using FXCop along with effective Unit Testing patterns will also help you prevent such bugs from arising.
  3. Naming conventions are king. By providing an example as you have with deliberately 'useless' (!) variable and property naming, you've highlighted exactly WHY we should all be using an effective naming convention. The issue you highlight is one which has caught us all out, and becomes more pervasive as the code base grows.

Whilst potentially not the answer you were after, I think #3 is the most important point here. Naming items properly is the single most effective solution. Not only that but people might not have the luxury of relying on ReSharper and other such tools.

To that end I'd also recommend StyleCop. It can be a little invasive, but it is a great tool for helping a developer, or team of developers, adhere to a set of syntax conventions which you'll find will pretty quickly eradicate bugs such as the one you've highlighted.

Happy coding!

like image 26
Nick Avatar answered Nov 02 '22 06:11

Nick