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
Yes, An interface should define properties when it really in need.
Like a class, Interface can have methods, properties, events, and indexers as its members. But interfaces will contain only the declaration of the members. The implementation of the interface's members will be given by class who implements the interface implicitly or explicitly.
A remote item is one that can be accessed from another computer. A cloneable interface is a marker interface. Cascade is an algorithm, not a feature of tagged interfaces. Hence, option (d) is the correct answer.
Interface attributes are by default public , static and final. An interface cannot contain a constructor (as it cannot be used to create objects)
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With