{"id":71,"date":"2024-02-18T16:56:27","date_gmt":"2024-02-18T16:56:27","guid":{"rendered":"https:\/\/permutationcity.co.uk\/bp\/?p=71"},"modified":"2024-04-28T09:08:47","modified_gmt":"2024-04-28T09:08:47","slug":"being-explicit","status":"publish","type":"post","link":"https:\/\/permutationcity.co.uk\/bp\/2024\/02\/18\/being-explicit\/","title":{"rendered":"Being explicit"},"content":{"rendered":"\n<p>Last year I watched a YouTube video about &#8220;no comment code&#8221; which, to me, sounded like a terrible idea.\nA quick web search shows that not commenting code has it&#8217;s proponents. Let&#8217;s not get into that right now.<\/p>\n\n\n\n<p>While I didn&#8217;t agree with the video as a whole I did like one specific idea:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Use types, rather than comments, to show what data is required.<\/p>\n<\/blockquote>\n\n\n\n<p>Actually, that one thing sounds kinda great.<\/p>\n\n\n\n<p>Instead of:<\/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;}\">\/\/ Duration is measured in seconds.\nvoid Sleep(float duration);<\/pre><\/div>\n\n\n\n<p>You can have:<\/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;}\">void Sleep(std::chrono::duration duration);<\/pre><\/div>\n\n\n\n<p>The first requires a comment so you know how use it. That is something that could easily be ignored if,\nsay, someone just expected <code>duration<\/code> to be in milliseconds. The second doesn&#8217;t need the comment. Given how\n<code>std::chrono::duration<\/code> is defined it doesn&#8217;t matter whether <code>duration<\/code> is given in seconds, milliseconds,\nor whatever. However the function is called it will be translated into the correct units when it is used.\nThat&#8217;s not as good as using a comment, it&#8217;s better.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"units-of-measurement\">Units of measurement<\/h2>\n\n\n\n<p>It&#8217;s easy to imagine extending this beyond time. There could be types to represent all sorts of quantities that\nwe measure: time, distance, mass, temperature,\n<a href=\"https:\/\/en.wikipedia.org\/wiki\/International_System_of_Units\">all the scientific units<\/a>.\nThen there are all the ways those can be combined: force, pressure, energy and more.\nThat would be a lot of types to define individually but we don&#8217;t have to.\nWe can define base units and then combine them together to create derived units.\nThe details of this are going to be complicated but <a href=\"https:\/\/github.com\/mpusz\/mp-units\">mp-units<\/a> shows it can be done.\nYou could end up with something 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;}\">    auto distance = 10 * meters;\n    auto time = 5 * seconds;\n    auto speed = distance \/ time;\n    std::cout &lt;&lt; speed &lt;&lt; std::endl;\n    \/\/ Prints: 2 meters \/ seconds<\/pre><\/div>\n\n\n\n<p>Again, the advantage here is not only in documentation but also that the units that you use don&#8217;t matter any more.\nThe <a href=\"https:\/\/en.wikipedia.org\/wiki\/Mars_Climate_Orbiter\">Mars Climate Orbiter<\/a> is a failed Mars mission.\nIt was destroyed because one team used <a href=\"https:\/\/en.wikipedia.org\/wiki\/International_System_of_Units\">SI units<\/a> and\nanother team used <a href=\"https:\/\/en.wikipedia.org\/wiki\/International_System_of_Units\">US customary units<\/a>.\nUsing these typed measurements different teams could use different units and they would be converted automatically.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"success-and-failure\">Success and failure<\/h2>\n\n\n\n<p>It&#8217;s common to have functions that return the indication of success or failure via a boolean.\nIt may be obvious that this is the intent or it may not be.\nIt leads to many comments of the form:<\/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;}\">\/\/ Returns true if successful and false otherwise.<\/pre><\/div>\n\n\n\n<p>Sometimes an enumeration is used instead of a boolean to make this more explicit:<\/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;}\">enum Outcome {\n    Success,\n    Failure\n};<\/pre><\/div>\n\n\n\n<p>It could be even better to have a fully formed class:<\/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;}\">class Outcome {\n    Outcome(bool value);\n    \n    operator bool() const;\n...\n};\n\nconst Outcome Success(true);\nconst Outcome Failure(false);<\/pre><\/div>\n\n\n\n<p>This doesn&#8217;t need a comment about any return value, is easy to create,\nand can be used directly in, say, an if-statement to control the response to the outcome.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"error\">Error<\/h2>\n\n\n\n<p>If you want to know more than a simple success or failure then an enumeration or\nexception handling are the typical ways to go. A custom class is another option.\nSomething 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;&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">class Error {\n    Error(const std::string&amp; message);\n\t\n    operator bool() const;\n...\n};\n\nconst Error NoError = Error::GetNoError();\nconst Error FileNotFound(&quot;File not found&quot;);<\/pre><\/div>\n\n\n\n<p>Again this doesn&#8217;t need to comment the return value and can be used directly in control flow.\nA set of standard errors can be defined alongside the <code>Error<\/code> class. However, unlike enums, other\nsystems can add their own error values and their is no risk of re-using values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"count\">Count<\/h2>\n\n\n\n<p>I was working on <code>ByteArray<\/code> classes recently.\nThis allowed bytes to be easily re-interpreted as immediate types, structs and arrays.\nThe interface was easy for most types but less clear for arrays:<\/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;}\">class ByteArray {\n...\n    template &lt;typename Type&gt;\n    const Type&amp; Get(uint64_t position);\n    \n    template &lt;typename Type&gt;\n    const std::range&lt;Type&gt;&amp; GetRange(uint64_t position, uint64_t size_in_bytes);\n...\n};<\/pre><\/div>\n\n\n\n<p>Given that the position was measured in bytes it seemed to make sense that the size should also be\nmeasured in bytes. However it was typically used to get a know number of objects.\nThat meant an extra size conversion every time. This can be made explicit.<\/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;}\">    const auto intsByByte = byteArray.GetRange&lt;uint32_t&gt;(0, Count&lt;byte&gt;(40));\n    const auto intsByObject = byteArray.GetRange&lt;uint32_t&gt;(0, Count&lt;uint32_t&gt;(10));\n    assert(intsByByte == intsByObject);<\/pre><\/div>\n\n\n\n<p>I&#8217;m slightly dissatisfied with this as it makes the code seem bulky. However it reduces the chance\nof someone getting the wrong range. Even if someone hasn&#8217;t read the documentation they have to use it correctly.\nThis could also be useful in an interface where several things are being counted and they could be confused.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"quantity\">Quantity<\/h2>\n\n\n\n<p>You might also want to have finer discrimination of what is being measured.\nA typed variant of a floating-point number or even the units of measurement from earlier could\nknow what it is measuring.<\/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;}\">    const Quantity&lt;Oxygen&gt; oxygen(50 * litres);\n    const Quantity&lt;Hydrogen&gt; hydrogen(100 * litres);\n    const auto water = React(oxygen, hydrogen);\n    std::cout &lt;&lt; water &lt;&lt; std::endl;\n    \/\/ Prints: H2 O * 50 litres<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"no-comment-code\">On balance<\/h2>\n\n\n\n<p>That&#8217;s just a few possibilities that I threw together quickly, I&#8217;m sure there are more out there.\nWhile I didn&#8217;t like the &#8220;no comment code&#8221; in general that doesn&#8217;t mean it doesn&#8217;t have something to say.\nDon&#8217;t dismiss and entire set of ideas just because you don&#8217;t agree with a few of them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last year I watched a YouTube video about &#8220;no comment code&#8221; which, to me, sounded like a terrible idea. A quick web search shows that not commenting code has it&#8217;s proponents. Let&#8217;s not get into that right now. While I didn&#8217;t agree with the video as a whole I did like one specific idea: Use [&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":[16],"class_list":["post-71","post","type-post","status-publish","format-standard","hentry","category-general","tag-methodologies"],"_links":{"self":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts\/71","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=71"}],"version-history":[{"count":3,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts\/71\/revisions"}],"predecessor-version":[{"id":196,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts\/71\/revisions\/196"}],"wp:attachment":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/media?parent=71"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/categories?post=71"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/tags?post=71"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}