Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface should not have properties?

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

like image 521
Andrija Avatar asked Jun 09 '09 20:06

Andrija


People also ask

Should an interface have properties?

Yes, An interface should define properties when it really in need.

CAN interface have properties?

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.

Which of the following is not a property of interface?

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.

CAN interface have properties Java?

Interface attributes are by default public , static and final. An interface cannot contain a constructor (as it cannot be used to create objects)


3 Answers

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.

like image 107
mqp Avatar answered Nov 28 '22 14:11

mqp


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.

like image 40
James Conigliaro Avatar answered Nov 28 '22 14:11

James Conigliaro


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.

like image 41
Pavel Feldman Avatar answered Nov 28 '22 15:11

Pavel Feldman