{"id":111660,"date":"2025-10-08T07:00:00","date_gmt":"2025-10-08T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=111660"},"modified":"2025-10-08T09:56:37","modified_gmt":"2025-10-08T16:56:37","slug":"20251008-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20251008-00\/?p=111660","title":{"rendered":"Windows Runtime API design principles around read-write properties: Idempotence and self-assignment"},"content":{"rendered":"<p>Objects in the Windows Runtime can have properties, and those properties could be read-only or read-write. For read-write properties, there are a few general principles. Today we&#8217;re going to look at this one:<\/p>\n<ul>\n<li>Setting a property to its current value is legal and has no effect.\u00b9<\/li>\n<\/ul>\n<p>For example:<\/p>\n<pre>\/\/ C#\r\no.Color = o.Color;\r\n\r\n\/\/ C++\/WinRT\r\no.Color(o.Color());\r\n<\/pre>\n<p>We are setting a property&#8217;s value to the value it already has.<\/p>\n<p>Now, this may look like a pointless thing to write on its own, but it could fall out of other code.<\/p>\n<pre>\/\/ C#\r\no.Color = EnsureSufficientContrast(o.Color, m_backgroundColor);\r\n\r\n\/\/ C++\/WinRT\r\no.Color(EnsureSufficientContrast(o.Color(), m_backgroundColor);\r\n<\/pre>\n<p>The hypothetical <code>Ensure\u00adSufficient\u00adContrast<\/code> function returns a foreground color that provides sufficient contrast against a background color. It&#8217;s possible that the existing foreground color already provides sufficient contrast, in which case the original color is returned unaltered.<\/p>\n<p>This principle seems obvious, but it comes with corollaries.<\/p>\n<ul>\n<li>Corollary: Setting a property twice to the same value has no effect.<\/li>\n<\/ul>\n<p>Setting the property multiple times is the same as setting it once.<\/p>\n<p>I remember many years ago I was looking at a customer program, and they had a property called <code>Print<\/code>. Setting it to <code>True<\/code> caused a document to print. I don&#8217;t mean that it resulted in a printout happening when you submitted the document. I mean it printed the document at the moment you set the <code>Print<\/code> property. If you wanted to print two copies, you would do<\/p>\n<pre>document.Print = True ' Print it\r\ndocument.Print = True ' Print it again\r\n<\/pre>\n<p>This was definitely a violation of the idempotence principle. As well as a violation of other principles like &#8220;What were you thinking?&#8221;<\/p>\n<p>Next time, I&#8217;ll look another simple corollary to the idempotence principle that people often are tempted to violate.<\/p>\n<p>\u00b9 By &#8220;no effect&#8221; here, I mean no functional effect. Clearly the code runs more slowly because you&#8217;re performing a redundant operation, and there might be follow-on calculations that occur when the property is set. But the point is that the correctness of the program is not affected by the redundant write to the property.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Setting it to the value it already has is not a crime.<\/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-111660","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Setting it to the value it already has is not a crime.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/111660","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=111660"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/111660\/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=111660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=111660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=111660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}