Another example of problem similar to hashCode() mutability is an option for broken «consistent with equals» contract imposed by implementation of java.util.TreeSet while implementing java.util.Comparator.

Actually, both of these cases (as well as a numerous others) are just an evidence of much wider problem – the requirement to follow some kind of contract various infrastructure implementations rely on and lack of ability to validate/impose such a constraints using existing IDEs and tools. The basic of this is the fact no IDE vendor can track an implementations of an infrastructure libraries existing (the best support feasible to achieve is Microsoft-style solution: an inspection for .Net framework libraries embedded into Visual Studio) and thus creating inspections “by reaction” will always loose the battle.

The best way to avoid the gap between software component and the inspections is to eliminate the “broken phone” between the software developer, the documentation developer and the inspections developer. This presents an «inspection by creator» paradigm – inspections are developed in parallel with a software component (and in ideal case by the the same software developer), embedded within the component, and shipped with it.

Summarizing the basic requirements for such a framework:

  • Development simplicity: the inspection language should provide a way to create an inspections in declarative manner, rather than by reflection-style coding.
  • Lexical compliance: the inspections declaration language should be close enough to the syntax of programming language constructs inspections apply to (thus, there should coexist inspections framework implementations of the common functionality for the different platforms like Java and .Net and different per programming language extensions on the same platform like C# and VB.Net).
  • Multiple inspection sources: there should be and option for predefined inspections (shipped with the framework), vendor inspections (developed and shipped with particular software component) and ad-hoc custom inspections (in-house development of software component users).
  • Binding: there should be a way to associate the inspections with the related code structures (classes, methods, fields and properties) and ideally — keep and maintain them within the code.
  • Universality: inspections should be applied in «to subject» and «to object» manners (i.e. relate to the code structure associated with the inspection or to the code caller).

Upon meeting the two first requirements the last three can be easily accomplished by using existing standard mechanisms like annotations and attributes.

Advertisements

How many times have you seen somebodys code that acts like following:

public class ClassA
{
    private string _myPropertyContainer;

    public string MyProperty {
        get { return _myPropertyContainer; }
        set {
            doSomething1();
            _myPropertyContainer = value;
        }
    }
}

I guess it was at least more than a couple of times, including the code of your own. And if you ask the programmer: “Why did you choose to access (or even worse, modify) private member instead of property?” — his answer with 99.99% probability will be “I don’t want the code inside the property getter or setter to be involved here. I just want to access/change the private member value.” And if you ask “Don’t you think this is a symptom of bad design”, you’ll get an answer “Not at all, the overall design is perfect, only this one little thing does not suit into it. It’s ok.”
Thus, everybody understand this is one of the worst thing you can do to your code, but everybody continue doing it. Why do you think people do this? No, this is not because they are bad designers. No this is not because they don’t think that writing error-prone and maintainable code is no important. People do this because they can do.
Yes, I meant that. People write code as bad as language and compiler permit them. If the compiler was able to restrict the access to private member _myPropertyContainer from the Method method, the programmer was investigating more time in design and was creating more clean code.
You may think I’m stupid. What does it mean “have no access to private members”? There is no such a thing! Yes, you’re right. There is no such a thing. But I want it.
Let’s illustrate my basic idea:

public class ClassA
{
    public void ClassA(string s) {
        MyProperty.container = s;
    }    

    public string MyProperty {
     get { return container; }
     set {
           doSomething1();
           container = value;
        }
    }
}

What does this piece of code changes? Note the private member _myPropertyContainer declaration has moved into MyProperty declaration. And I want that from now on _myPropertyContainer will not be visible for any class members except MyProperty.get and MyProperty.set. That is, if someone tries to access _myPropertyContainer from doSomething(), he will receive a compilation error. Thus a feature proposed will prevent a programmer from making a stupid mistakes by accessing a private member instead of the property.
Now lets take look on the paradigm proposed. First, if we want all of our properties to define a private member, we do not need to define this members at all — let compiler to perform this task instead of us.

public class ClassA
{
    public string MyProperty {
        get { return container; }
        set {
            doSomething1();
            container = value;
        }
    }
}

The piece of code above illustrates this approach: if you want to save a value as a private property member, you can use a special value container which corresponds to it. Of course, at the compile time each reference to this value is replaced by reference to actual property private member. If the container value never used inside property code, that means we have a read-only or calculated property. In this case compiler have no reason to generate private property member since it never used.
Sounds good? Yes, but not good enough. Sometimes you really need to access a private member, mostly in constructor when you want to initialize the property the the default value and any access to property set will change the object state (and, in general, the object is not fully initialized yet). So the second point is to enable an access to private property member from constructor, exactly as done for read only members.

public class ClassA
{
    public void ClassA(string s) {
        MyProperty.container = s;
    }
    public string MyProperty {
        get { return container; }
        set {
            doSomething1();
            container = value;
        }
    }
}

Now the picture is complete. MyProperty has a full access to its auto generated private member through container value. No one else from class members has an access to this property except constructor. Constructor accesses a private members by denoting them as MyProperty.container, so there is no ambiguity here.

Is there some way to connect Microsoft CLR debugger to Eclipse debugger?
The “How to write an Eclipse debugger” article claims this is a bit tricky…