{"id":382,"date":"2024-10-15T18:21:19","date_gmt":"2024-10-15T18:21:19","guid":{"rendered":"https:\/\/permutationcity.co.uk\/bp\/?p=382"},"modified":"2024-10-13T08:45:15","modified_gmt":"2024-10-13T08:45:15","slug":"missing-operators","status":"publish","type":"post","link":"https:\/\/permutationcity.co.uk\/bp\/2024\/10\/15\/missing-operators\/","title":{"rendered":"Missing operator"},"content":{"rendered":"\n<p>A couple of weeks ago I was talking about error handling and\none option using.\n<a href=\"https:\/\/permutationcity.co.uk\/bp\/2024\/10\/01\/error-handling\/#short-circuit-logic\">short-circuit logic<\/a>.\nIt had lines that looked like this:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">    success = success &amp;&amp; FileGetLine(file, headerLine);\n    success = success &amp;&amp; headerLine == &quot;Date list&quot;;<\/pre><\/div>\n\n\n\n<p>But why couldn&#8217;t they look like this:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">    success &amp;&amp;= FileGetLine(file, headerLine);\n    success &amp;&amp;= headerLine == &quot;Date list&quot;;<\/pre><\/div>\n\n\n\n<p>While <em>you<\/em> might understand what that means C++ doesn&#8217;t, very few languages do.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"short-circuit-logic\">Short-circuit logic<\/h2>\n\n\n\n<p>The normal logic operators we deal with are <strong>and<\/strong>, <strong>or<\/strong> and <strong>xor<\/strong> (exclusive-or).\nThese are part of introductory computing science courses and then fade into the background.\nIn many languages they are represented as <code>&amp;<\/code>, <code>|<\/code> and <code>^<\/code> respectively but\nthe English words maybe be used or available as an alternative.<\/p>\n\n\n\n<p>Short-circuit logic operators are available for <strong>and<\/strong> and <strong>or<\/strong> but not <strong>xor<\/strong>.\nWith the first two we can potentially skipping the evaluation of the second operand,\nwith the later it&#8217;s just not possible.\nComputationally they are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>a &amp;&amp; b<\/code> is equivalent to <code>!a ? false : b<\/code><\/li>\n\n\n\n<li><code>a || b<\/code> is equivalent to <code>a ? true : b<\/code><\/li>\n<\/ul>\n\n\n\n<p>They are guaranteed to be logically equivalent,\nthe result of the calculations are going to be the same.\nHowever the executed code can have effects beyond the simple logic calculation.\nWe can take advantage of that.\nThe example above can be re-written:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">    success = !success ? false : FileGetLine(file, headerLine);\n    success = !success ? false : (headerLine == &quot;Date list&quot;);<\/pre><\/div>\n\n\n\n<p>Assuming your happy with your \n<a href=\"https:\/\/en.wikipedia.org\/wiki\/Ternary_conditional_operator\">ternary conditional operator<\/a>.\nIf not:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">    if (success) {\n        success = FileGetLine(file, headerLine);\n    }\n    if (success) {\n        success = (headerLine == &quot;Date list&quot;);\n    }<\/pre><\/div>\n\n\n\n<p>Both show the action of the code more strongly. Either <code>success<\/code> is already <code>false<\/code> and will stay that way or it will take the value of the new calculation. However, the conditional operator representation is also more wordy. That means it can be harder to get an overview of because their are more details that could matter. Extra details also leave more room for bugs to creep in. The if-statement representation is probably the clearest but, I think, suffers similar problems with larger blocks of code. Overall I prefer the shortened form.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"compound-assignment\">Compound assignment<\/h2>\n\n\n\n<p>Many languages have\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/Augmented_assignment\">augmented assignment<\/a>\nwhich perform an operation and an assignment in one go:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>a = a + b;<\/code> is equivalent to <code>a += b;<\/code><\/li>\n<\/ul>\n\n\n\n<p>This means less repetition and, perhaps, a clearer sense here that <code>a<\/code> is accumulating changes.\nThere could be a performance advantage as the compiler doesn&#8217;t have to create and\nthen throw away a temporary variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"missing-operator\">Missing operator<\/h2>\n\n\n\n<p>If you look through C++ list of\n<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/operator_assignment\">augmented assignment operators<\/a>\nyou&#8217;ll find arithmetic operators, logic operators but not\nshort-circuit logic operators.\nThere are <code>&amp;=<\/code> and <code>|=<\/code>, the logic assignment operators, but\nthose will always perform an evaluation.\nThere is no <code>&amp;&amp;=<\/code> or <code>||=<\/code>, the short-circuit logic assignment operators.\nIt&#8217;s not in C, C++, C# or Objective-C.\nIt&#8217;s not in D, Go, Java, Pascal, Perl, PHP, Rust or Swift.\nIt <em>is<\/em> in\n<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/Logical_AND_assignment\">JavaScript<\/a>\nand\n<a href=\"https:\/\/rubyreferences.github.io\/rubyref\/language\/assignment.html\">Ruby<\/a>.\nI&#8217;m not going to promise that list is entirely correct.\nI&#8217;m looking at this\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/Short-circuit_evaluation\">list on Wikipedia<\/a>\nand it misclassified Ruby.\nThese operators are definitely uncommon.<\/p>\n\n\n\n<p>I couldn&#8217;t find a good discussion of why these operators aren&#8217;t present.\nFrom what little is out there it seems that combining these two things is\nconsidered to be confusing or conflicted and without clear benefit.\nI&#8217;m not sure I agree.\nTo me the presence of one set of augmented assignment operators suggests the others.\nIndeed some languages go out of there way to say that they are\n<a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/language-reference\/operators\/boolean-logical-operators#compound-assignment\">not available<\/a>.<\/p>\n\n\n\n<p>The actually behaviour of such an operator:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>a &amp;&amp;= b;<\/code> would be equivalent to <code>a = a &amp;&amp; b;<\/code><\/li>\n<\/ul>\n\n\n\n<p>Makes sense to me.\nIt won&#8217;t operate in exactly the same way as the other augmented assignment operators.\nThe second operand doesn&#8217;t necessary get evaluated.\nHowever that&#8217;s just a peculiarity of short-circuit logic operators.\nIt&#8217;s something that you learn and then get use to.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"a-different-story\">A different story<\/h2>\n\n\n\n<p>I&#8217;ve been away from day-to-day C# development for a while. Since then they&#8217;ve gained a <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/language-reference\/operators\/null-coalescing-operator\">null-coalescing operator<\/a>. The operator, <code>??<\/code>, takes the value of it&#8217;s left-hand operand if it isn&#8217;t <code>null<\/code> and, otherwise, the value of it&#8217;s right-hand operand. That means:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>a ?? b<\/code> is equivalent to <code>(a != null) ? a : b<\/code><\/li>\n<\/ul>\n\n\n\n<p>That looks familiar. Re-writing one of my examples above I can get:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>a || b<\/code> is equivalent to <code>(a != false) ? a : b<\/code><\/li>\n<\/ul>\n\n\n\n<p>And while the short-circuit operators are all alone the null coalescing operators gets:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>a ?? = b;<\/code> is equivalent to <code>a = (a != null) ? a : b;<\/code><\/li>\n<\/ul>\n\n\n\n<p>Is that fair?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"on-balance\">On balance<\/h2>\n\n\n\n<p>I&#8217;d like to try using short-circuit augmented assignment operators in a language.\nI can already see a use for them in error handling and\nit would be interesting to see what else they could do.\nThey&#8217;re certainly not essential but they would round out a language.\nIndeed I could argue that not having them is more complicated as it creates special cases.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A couple of weeks ago I was talking about error handling and one option using. short-circuit logic. It had lines that looked like this: But why couldn&#8217;t they look like this: While you might understand what that means C++ doesn&#8217;t, very few languages do. Short-circuit logic The normal logic operators we deal with are and, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[38],"class_list":["post-382","post","type-post","status-publish","format-standard","hentry","category-general","tag-language"],"_links":{"self":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts\/382","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/comments?post=382"}],"version-history":[{"count":3,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts\/382\/revisions"}],"predecessor-version":[{"id":385,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts\/382\/revisions\/385"}],"wp:attachment":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/media?parent=382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/categories?post=382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/tags?post=382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}