{"id":455,"date":"2025-07-29T20:12:21","date_gmt":"2025-07-29T20:12:21","guid":{"rendered":"https:\/\/permutationcity.co.uk\/bp\/?p=455"},"modified":"2025-07-29T20:12:21","modified_gmt":"2025-07-29T20:12:21","slug":"picking-a-name","status":"publish","type":"post","link":"https:\/\/permutationcity.co.uk\/bp\/2025\/07\/29\/picking-a-name\/","title":{"rendered":"Picking a name"},"content":{"rendered":"\n<p>I&#8217;ve talked about\n<a href=\"https:\/\/permutationcity.co.uk\/bp\/2024\/02\/27\/good-names\/\">good names<\/a>\nbefore but\nit only briefly touched on the importance of exactly\n<a href=\"https:\/\/permutationcity.co.uk\/bp\/2024\/02\/27\/good-names\/#choose-carefully\">which words<\/a>\nyou choose.\nWords have meanings and connotations.\nPick the wrong word and people can get the wrong impression.\nHowever good your documentation is that won&#8217;t help if the developer thinks they already know how to use it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"stdremove\"><code>std::remove<\/code><\/h2>\n\n\n\n<p>The C++ template library provides a lot of useful functionality.\nDuring my early years with C++ it hadn&#8217;t really been a feature but\ncoming back to it later on it was much more extensive.\nThe\n<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/header\/algorithm.html\">algorithm header<\/a>\ndeals with containers and iterators.<\/p>\n\n\n\n<p>So, for example, we have\n<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/algorithm\/replace.html\">replace<\/a>.\nIt goes through replacing anything matching <code>old_value<\/code> with <code>new_value<\/code>.\nThat makes sense.<\/p>\n\n\n\n<p>So if we wanted to remove some values we would use&#8230;\n<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/algorithm\/remove.html\">remove<\/a>?\nLet&#8217;s quickly skim the documentation:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Removes all elements satisfying specific criteria from the range.<\/p>\n<\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Removes all elements that are equal to <code>value<\/code><\/p>\n<\/blockquote>\n\n\n\n<p>Sounds great but, no, this is only part of what you want.\nWhat remove really does is move all the values to be kept to the start of the range and\nall the values you want to erase to the end of the range.\nYou&#8217;re actually need is 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 newEnd = pointers.remove(nullptr);\n    pointers.erase(newEnd, pointer.end());<\/pre><\/div>\n\n\n\n<p>Functionally this is&#8230; okay.\nYou want to erase some things from a vector and you can.\nIt does require you to use the <code>pointers<\/code> identifier three times so it is repetitive but\nthat&#8217;s another problem.<\/p>\n\n\n\n<p>For me the real problem here is the name.\nSurely <code>remove<\/code> should <em>remove<\/em> something?\nIf you&#8217;re picking names you want to reflect what&#8217;s really going on.\nHere, say, <code>partition<\/code> would be a better fit.\nThe collection is being divided into two.\nThe first section will not have the given value, the second section will only have the given value.<\/p>\n\n\n\n<p>Actually there is a\n<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/algorithm\/partition.html\">partition<\/a>\nand\n<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/algorithm\/stable_partition.html\">stable_partition<\/a>\nas well.\nSo what&#8217;s going on:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In <code>partition<\/code> the order of elements is not stable. No guarantees about the order within each section.<\/li>\n\n\n\n<li>In <code>stable_partition<\/code> the order of elements is stable. Within each section the original order will be preserved.<\/li>\n\n\n\n<li>In <code>remove<\/code> the order of elements is partially stable. Within the first section the original order will be preserved but not within the second.<\/li>\n<\/ul>\n\n\n\n<p>Functionally these are related but different.\nIt would make sense to reflect this in all of the names.\n<code>remove<\/code> not only doesn&#8217;t reflect the functionality it fails to signpost these other functions.<\/p>\n\n\n\n<p>At this point I often jump to a\n<a href=\"https:\/\/www.thesaurus.com\/\">thesaurus<\/a>.\nFor this we can start by looking up\n<a href=\"https:\/\/www.thesaurus.com\/browse\/remove\">remove<\/a> and\n<a href=\"https:\/\/www.thesaurus.com\/browse\/partition\">partition<\/a>.\nI&#8217;m less keen on the former because it doesn&#8217;t reflect what the function does but\nwe can check both.\nI would consider <code>subdivide<\/code>, <code>separate<\/code> and <code>split<\/code> as possibilities.\nAs a nice bonus <code>separate<\/code> is in both lists.<\/p>\n\n\n\n<p>Thinking about it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>I feel that <code>subdivide<\/code> means more than two sections.<\/li>\n\n\n\n<li>I associate <code>split<\/code> with strings and it&#8217;s functional partner <code>join<\/code>.<\/li>\n\n\n\n<li><code>separate<\/code> and <code>partition<\/code> could both work as the core word for a set of functions.<\/li>\n\n\n\n<li>For me <code>partition<\/code> wins out over <code>separate<\/code>. It feels more &#8220;algorithmy&#8221;.<\/li>\n<\/ul>\n\n\n\n<p>We still have to distinguish between the functions.\nI think <code>stable_partition<\/code> is good and\nthat suggests <code>unstable_partition<\/code> for the\n<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/algorithm\/partition.html\">opposite<\/a>.\nWe could try renaming the <code>remove<\/code> using a similar scheme,\n<code>semistable_partition<\/code> or <code>partially_stable_partition<\/code> perhaps.\nWhile the former <em>might<\/em> work the later feels far too long,\nespecially considering it&#8217;s probably the most used of the functions.\nIn the end I&#8217;d just stick with my original suggestion, <code>partition<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"in-the-end\">In the end<\/h2>\n\n\n\n<p>This might seem overly involved.\nEspecially when I go off searching through a thesaurus only to end back at my original idea.\nHowever, for me names really do matter.\nIt&#8217;s worth spending some time picking the right ones.\nIf you pick the wrong one you may end up being stuck with it.\nA good name keeps the developer on track.\nThe first time you read some code you want to be able to get the gist of what&#8217;s going on,\neven if the functions and classes are new.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve talked about good names before but it only briefly touched on the importance of exactly which words you choose. Words have meanings and connotations. Pick the wrong word and people can get the wrong impression. However good your documentation is that won&#8217;t help if the developer thinks they already know how to use it. [&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":[22,28],"class_list":["post-455","post","type-post","status-publish","format-standard","hentry","category-general","tag-algorithms","tag-documentation"],"_links":{"self":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts\/455","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=455"}],"version-history":[{"count":1,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts\/455\/revisions"}],"predecessor-version":[{"id":456,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts\/455\/revisions\/456"}],"wp:attachment":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/media?parent=455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/categories?post=455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/tags?post=455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}