{"id":32823,"date":"2006-01-03T03:16:18","date_gmt":"2006-01-03T03:16:18","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2006\/01\/03\/on-the-abuse-of-properties\/"},"modified":"2006-01-03T03:16:18","modified_gmt":"2006-01-03T03:16:18","slug":"on-the-abuse-of-properties","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20060103-18\/?p=32823","title":{"rendered":"On the abuse of properties"},"content":{"rendered":"<p>\nOne thing that I see occasionally is the abuse of property syntax.\nIDispatch and CLR objects (and C++ objects if you want to avail\nyourself of\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/vccelng\/htm\/msmod_30.asp\">\na Microsoft-specific extension<\/a>)\nsupport &#8220;properties&#8221;, which syntactically look like fields\nbut internally are treated as a pair of methods (&#8220;get&#8221; and &#8220;put&#8221;).\nAn important principle is that given an object <code>o<\/code>\nand a property <code>p<\/code>, the lines\n<\/p>\n<pre>\n(void)o.p;\no.p = o.p;\n<\/pre>\n<p>\nshould be effectively nops.\n(Mind you, they might be really inefficient nops.)\n<\/p>\n<p>\nMy favorite (or perhaps most hated) example of violating this\nprinciple is an object I saw many years ago that had a &#8220;print&#8221;\nproperty, which if set to true, caused the object to send itself\nto the printer.\nIf you did\n<\/p>\n<pre>\no.print = true;\no.print = true;\n<\/pre>\n<p>\nthen two copies of the object were printed.\n<\/p>\n<p>\nProperty syntax mimics field syntax for a reason:\nProperties should behave like fields.\n<\/p>\n<p>\n(Plenty more guidance on properties can be found in the\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/cpgenref\/html\/cpconpropertyusageguidelines.asp\">\n.NET Framework Property Usage Guidelines<\/a>.)\n<\/p>\n<p>\n[While Raymond was on vacation,\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2004\/01\/26\/63163.aspx\">\nthe autopilot stopped working<\/a>\ndue to\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/09\/19\/471240.aspx\">\na power outage<\/a>.\nThis entry has been backdated.]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One thing that I see occasionally is the abuse of property syntax. IDispatch and CLR objects (and C++ objects if you want to avail yourself of a Microsoft-specific extension) support &#8220;properties&#8221;, which syntactically look like fields but internally are treated as a pair of methods (&#8220;get&#8221; and &#8220;put&#8221;). An important principle is that given an [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-32823","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>One thing that I see occasionally is the abuse of property syntax. IDispatch and CLR objects (and C++ objects if you want to avail yourself of a Microsoft-specific extension) support &#8220;properties&#8221;, which syntactically look like fields but internally are treated as a pair of methods (&#8220;get&#8221; and &#8220;put&#8221;). An important principle is that given an [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/32823","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=32823"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/32823\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=32823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=32823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=32823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}