views:

286

answers:

8

My office colleague told me today that is bad practice to use properties in interfaces. He red that in some MSDN article(s), which I couldn't find (well I was trying few times on google, probably with wrong key words). He also told me that only methods should be in interface. Now, I am aware that is is not strict rule, since obviously in .net you can make property signature in interface and compile it.

But is this true to be a bad practice/design/oop ? And why ?

Pointing out to right literature or web resource would be helpful too.

Thanks

+13  A: 

I have never encountered anyone making this claim, nor do I see any good reason for it. The .NET framework is full of interfaces with properties.

mquander
+6  A: 

An interface is a contract for a class to implement and I see no reason why properties should be excluded from such a contract. Furthermore, properties are intrinsic to .NET.

As an example: ICollection<T> has both Count and IsReadOnly.

Colin Burnett
+1  A: 

Never seen anything like this per se, but there was some talk a while ago about not using properties in cross-platform interface definitions as there are many platforms which don't support properties, but just about everything supports methods.

Wyatt Barnett
What do you mean by "cross-platform interface"?
Colin Burnett
Not interfaces in the BCL "public interface foo" sense. Rather, interface specifications. A good example would be the Document Object Model specification from the W3C.
Wyatt Barnett
A: 

I can think of no documentation to support that premise. Additionally I can think of many examples in the BCL which do quite the opposite. Take a look at pretty much any of the collection interfaces and you will see properties.

JaredPar
A: 

Practically, a property is set of two functions: one to get the value, and one to set the value. Even though properties are first class "features" of C#, this is still true.

Why wouldn't properties be allowed in interfaces?

Dave Van den Eynde
A: 

I can see no reason not to have Properties as part of your Interface. How else would you implement access to data members? Get Methods and Set Methods instead of properties. That would be just plain ugly and so 1990's.

Jeffrey Hines
+4  A: 

I'll just add my voice in here as well - I've never come across this recommendation. A property is effectively a pair of get/set methods.

Like every other design decision. If it genuintely makes sense; if it is appropriate for the system under design, if it doesn't cause maintenance problems, if it doesn't cause performance problems, there should be no reason you can't do it.

James Conigliaro
+7  A: 

There is widely recognized term "code smell". I suggest introducing "programmer smell" concept - if someone insists on some rule, but can not explain why - it is a smell. Your colleague should be able to explain why properties in the interface are bad. If he can not - he is probably wrong even if article he is referring to is right.

That article may be was talking about some specific kinds of interfaces, may be it had something to do with COM and interoperability or whatever. Or he may be just got it wrong. Understanding rules and being able to explain them is important part of using rules.

Pavel Feldman