{"id":29953,"date":"2006-08-28T10:00:18","date_gmt":"2006-08-28T10:00:18","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2006\/08\/28\/try-to-avoid-having-bool-function-parameters\/"},"modified":"2006-08-28T10:00:18","modified_gmt":"2006-08-28T10:00:18","slug":"try-to-avoid-having-bool-function-parameters","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20060828-18\/?p=29953","title":{"rendered":"Try to avoid having BOOL function parameters"},"content":{"rendered":"<p>Generally speaking, I believe that you should try to avoid giving functions a boolean parameter (<code>BOOL<\/code>, <code>bool<\/code>, etc.) unless the meaning of that boolean parameter is blatantly obvious. Examples of obvious meaning would be the second parameter to the <code>EnableWindow<\/code> function (<code>TRUE<\/code> obviously means the window is being enabled and <code>FALSE<\/code> means that it&#8217;s being disabled) and the final parameter to <code>ShowScrollBar<\/code> (<code>TRUE<\/code> obviously means the scroll bar is being shown and <code>FALSE<\/code> means that it&#8217;s being hidden). In both of these cases, the meaning of the boolean parameter is encoded in the name of the function itself.\n But for functions like <code>CreateEvent<\/code>, what does that first <code>BOOL<\/code> parameter mean? First, you have to realize that the first <code>BOOL<\/code> parameter controls whether you get an auto-reset event or a manual-reset one. Does <code>FALSE<\/code> create a manual-reset event? Or is that done by passing <code>TRUE<\/code>? I can never remember and I have to go looking it up each time. That first parameter should have been declared as, say, a <code>DWORD<\/code> or, even better, an <code>enum<\/code> with two legal values, <code>EVENTTYPE_AUTORESET<\/code> and <code>EVENTTYPE_MANUALRESET<\/code>.\n Even worse is that <code>CreateEvent<\/code> has <strong>two<\/strong> <code>BOOL<\/code> parameters. Like anybody reading the code later can remember which comes first.\n And the mystery <code>bool<\/code>s keep coming. Consider, for example, <a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/cpref\/html\/frlrfsystemiostreamreaderclassctortopic3.asp\"> <code>StreamReader(Stream, bool)<\/code><\/a>. What does <code>true<\/code> mean? Or <code>false<\/code>? Heck if I know.<\/p>\n<p> Mind you, this is just my opinion. Others may disagree with me. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Generally speaking, I believe that you should try to avoid giving functions a boolean parameter (BOOL, bool, etc.) unless the meaning of that boolean parameter is blatantly obvious. Examples of obvious meaning would be the second parameter to the EnableWindow function (TRUE obviously means the window is being enabled and FALSE means that it&#8217;s being [&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-29953","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Generally speaking, I believe that you should try to avoid giving functions a boolean parameter (BOOL, bool, etc.) unless the meaning of that boolean parameter is blatantly obvious. Examples of obvious meaning would be the second parameter to the EnableWindow function (TRUE obviously means the window is being enabled and FALSE means that it&#8217;s being [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/29953","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=29953"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/29953\/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=29953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=29953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=29953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}