{"id":12,"date":"2003-10-24T13:31:48","date_gmt":"2003-10-24T13:31:48","guid":{"rendered":"http:\/\/www.pleus.net\/blog\/?p=12"},"modified":"2010-08-13T00:13:12","modified_gmt":"2010-08-12T22:13:12","slug":"attributes-the-new-macros","status":"publish","type":"post","link":"https:\/\/www.pleus.net\/blog\/?p=12","title":{"rendered":"Attributes &#8211; The New Macros ?"},"content":{"rendered":"<p>Do you remember the times when C++ was the mainstream programming language in enterprise level projects ?<br \/>\nA popular approach was to simplify the code by defining a lot of macros.<br \/>\nEven Microsoft did that very extensively in their MFC implementation for instance in their ubiquitous messagemaps.<br \/>\nThe benefit was an easy progamming model &#8211; just add a few macros and be happy.<br \/>\nOn the other hand it was very difficult to understand what really happened in the background.<br \/>\nThis was especially true for user defined macros which could only be used in the context of a certain project.<\/p>\n<p>Later languages like Java and C# took over and macros became history (C# has a preprocessor but no macros). The nightmare was over.<br \/>\nTodays software systems aren&#8217;t easier than in the past. Therefore the language engineers considered it to be a good idea to support some metainformation facility in form of attributed programming.<\/p>\n<p>In .NET you have attributes.<\/p>\n<p><font face=\"courier\"><br \/>\n[WebMethod]<br \/>\npublic string HelloWorld()<br \/>\n{<br \/>\n    return &#8220;Hello, .World!&#8221;;<br \/>\n}<br \/>\n<\/font><\/p>\n<p>Java introduces the Language metadata facility (JSR175) which is currently a community draft. This approach is based on javadoc comments. <\/p>\n<p><font face=\"courier\"><br \/>\n\/**<br \/>\n * @common:operation<br \/>\n *\/<br \/>\npublic String HelloWorld()<br \/>\n{<br \/>\n    return &#8220;Hello, World!&#8221;;<br \/>\n}<br \/>\n<\/font><\/p>\n<p>Looks somehow similar, right ?<\/p>\n<p>But it&#8217;s not.<\/p>\n<p>In .NET the attributes are stored in the assemblies metadata and therefore can be accessed at runtime.<br \/>\nThey can be determined by reflection as shown in the following example:<\/p>\n<p><font face=\"courier\"><br \/>\nclass Test<br \/>\n{<br \/>\n\tstatic void Main(string[] args)<br \/>\n\t{<br \/>\n            Type t = typeof (Test);<br \/>\n            MemberInfo[] members = t.GetMember(&#8220;DoIt&#8221;);<br \/>\n            object[] attrs = members[0].GetCustomAttributes(false);<br \/>\n            Console.WriteLine(attrs[0].ToString());<br \/>\n       \t}<\/p>\n<p>       \t[Cool()]<br \/>\n        public static void DoIt()<br \/>\n        {<br \/>\n\t}<\/p>\n<p>\t[AttributeUsage(AttributeTargets.All)]<br \/>\n        public class CoolAttribute : Attribute<br \/>\n        {<br \/>\n        }<br \/>\n}<br \/>\n<\/font><\/p>\n<p>In Java they merely serve to generate code at compile time. <br \/>\nJava supports reflection as well. But due to the fact that the attributes are not stored in the metadata they can&#8217;t be determined by reflection at runtime.  <\/p>\n<p>What&#8217;s the conclusion ?<\/p>\n<p>On one hand both approaches are very useful to simplify the programming model for the developer.<\/p>\n<p>On the other hand they are like C++ macros. A developer can define his\/her own attributes which nobody outside a project might know.<br \/>\nThis leads to applications which are hard to maintain.<br \/>\nIn order to create maintainable software systems I think developers should define their own attributes\/annotations as little as possible.<br \/>\nThis will ease the job especially for new team members which join a project at a later time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Do you remember the times when C++ was the mainstream programming language in enterprise level projects ? A popular approach was to simplify the code by defining a lot of macros. Even Microsoft did that very extensively in their MFC implementation for instance in their ubiquitous messagemaps. The benefit was an easy progamming model &#8211; &hellip; <a href=\"https:\/\/www.pleus.net\/blog\/?p=12\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Attributes &#8211; The New Macros ?<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-12","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/12","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12"}],"version-history":[{"count":1,"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/12\/revisions"}],"predecessor-version":[{"id":375,"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/12\/revisions\/375"}],"wp:attachment":[{"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}