Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a breaking change that modifying the access modifier of a public property?

Tags:

If I change the access modifier of the setter of a public property from private to public, is that causes any breaking change in the other assemblies that refer it?

like image 418
mkus Avatar asked Dec 20 '11 19:12

mkus


1 Answers

UPDATE: This question was the topic of my blog in January 2012. Thanks for the great question!


I assume that by "breaking change" you mean "when I recompile code that depended on this assembly, does code that used to compile still compile?"

By that definition, strictly speaking, yes, making a property setter public that used to be private is a breaking change. Suppose you have this code:

// Assembly Alpha public class Charlie {     public int P { get; private set; } } public class Delta {     public int P { get; set; } } 

And then in another assembly that references Alpha:

// Assembly Echo class Foxtrot {     static void M(Action<Charlie> f) {}     static void M(Action<Delta> f) {}     static void Golf()     {         M(y=>{y.P = 123;});     } } 

You compile assembly Echo. Class Foxtrot has a method Golf which does overload resolution on M. M has two overloads; one that takes a action on Charlie and one that takes an action on Delta. If lambda parameter y is of type Charlie then the lambda body produces an accessibility error, and therefore that overload of M is not an applicable candidate. Overload resolution chooses the second overload and compilation succeeds.

Now you change assembly Alpha so that Charlie.P's setter is public. You recompile Echo. Now you get an overload resolution error because both overloads of M are equally valid and neither is better than the other. Compilation of Echo fails due to the change in Alpha. Your change to Alpha was a breaking change.

The question is not "is this a breaking change?" It clearly is; almost every change is some kind of breaking change. The question should be whether the breaking change will actually in practice break anyone, and if so, what is the cost of fixing the break compared to the benefit of the new feature?

like image 195
Eric Lippert Avatar answered Oct 18 '22 13:10

Eric Lippert