Marker Interfaces
So today I was doing some coding on a personal project and I had this scenario where I have a class that contains an object representing some data , but the nature of this data is very diverse so I could not create an interface to represent it. But I do needed to "mark it" as something belonging to an expected type. So what did I do ? Well nothing :P , I was using typescript and I left that member type as "any" but that looked so ugly to me so I had to do something! So after thinking for a while I thought "Ok , I will have an empty interface ! To mark my classes. But are empty interfaces even allowed??" . So I googled my concern and found out that it is indeed a thing and it is called a Marker Interface.
https://en.wikipedia.org/wiki/Marker_interface_pattern
So basically marker interface means an empty interface that you use to mark something. But it does have a lot of bad side effects according to SO and many other online platforms and in modern languages like Java and .NET the use of annotations and attributes are recommended instead of using empty interfaces.
But... I don't know about you guys but the marker interface does look like an easier way for type checking that attributes so I kind of like it more than the recommended attributes. What do you guys think ? I might be marked a code heathen for saying this but I kind of like marker interfaces better than attributes ! Let me know your thoughts , what do you use ?
Comments
Post a Comment