{"id":14,"date":"2002-08-13T16:56:44","date_gmt":"2002-08-13T06:56:44","guid":{"rendered":"http:\/\/www.thunderguy.com\/plusplus\/20050421\/prefer-prefix-operators-over-postfix\/"},"modified":"2007-07-02T08:09:49","modified_gmt":"2007-07-01T22:09:49","slug":"prefer-prefix-operators-over-postfix","status":"publish","type":"post","link":"https:\/\/thunderguy.com\/semicolon\/2002\/08\/13\/prefer-prefix-operators-over-postfix\/","title":{"rendered":"Prefer prefix operators over postfix"},"content":{"rendered":"<p class=\"abstract\">Whenever you have a choice, you should use the C++ prefix increment and decrement operators (e.g. <code>++i<\/code>) instead of the postfix versions (e.g. <code>i++<\/code>). This will make your code more efficient, clear and consistent. This advice applies (more or less) to most languages that have both prefix and postfix operators.<\/p>\n<h3>1. Efficiency<\/h3>\n<p>Code like this is quite common:<\/p>\n<pre class=\"code\">\r\n<code>for (i = 0; i &lt; count; i++)\r\n    \/\/ stuff<\/code>\r\n<\/pre>\n<p>But consider what&#8217;s going on. The expression <code>i++<\/code> has a value, namely the value of <code>i<\/code> before the increment. <code>i<\/code> is incremented during evaluation of the expression, but the program must keep a copy of the un-incremented <code>i<\/code> to use as the value of the expression.<\/p>\n<p><!--more-->In this case, of course, the compiler will probably realize that the value of the expression is not used, and can therefore avoid the copy.<\/p>\n<p>But this assumes that <code>i<\/code> is an integral type. What if it&#8217;s an object with its own postfix increment operator (declared as <code>operator++(int)<\/code>)? Even if the compiler can see that the expression&#8217;s value is not used, it still has to call the postfix version of the increment operator. Here&#8217;s a typical implementation of <code>operator++<\/code>, for a class called <code>Incrementable<\/code>:<\/p>\n<pre class=\"code\">\r\n<code>\/\/ Prefix increment\r\nconst Incrementable&amp; Incrementable::operator++()\r\n{\r\n    this-&gt;Increment(); \/\/ or whatever\r\n    return *this;\r\n}\r\n\r\n\/\/ Postfix increment\r\nconst Incrementable Incrementable::operator++(int)\r\n{\r\n    Incrementable temp(*this);\r\n    this-&gt;Increment(); \/\/ or whatever\r\n    return temp;\r\n}<\/code>\r\n<\/pre>\n<p>Even allowing for typical optimizations, this still results in one useless object (<code>temp<\/code>) being copy constructed. Even if <code>i<\/code> is an integral type today, somebody might change it tomorrow to be some relatively heavyweight class type like an iterator: suddenly, you get a new object created and destroyed every time through the loop. Simply switching to <code>++i<\/code> would avoid this waste.<\/p>\n<h3>2. Visibility<\/h3>\n<p>When used on a line by themselves, the postfix operators can become lost. Especially when incrementing long expressions, the operator is much easier to spot when it&#8217;s at the left margin, rather than surrounded on all sides by other code. Since I am all for clarity and visibility in code, I recommend prefix operators over postfix. (See also <a href=\"http:\/\/www.thunderguy.com\/semicolon\/20001209\/clarifying-c-negation\/\">Clarifying C++ negation<\/a>.)<\/p>\n<pre class=\"code\">\r\n<code class=\"bad-code\">server.start(tracker.getInstance());\r\ntheRegister-&gt;indexCounter++; \/\/ too much punctuation\r\ncout &lt;&lt; \"Number \" &lt;&lt; server.last();<\/code>\r\n<\/pre>\n<pre class=\"code\">\r\n<code>server.start(tracker.getInstance());\r\n++(theRegister-&gt;indexCounter); \/\/ the ++ stands out on its own\r\ncout &lt;&lt; \"Number \" &lt;&lt; server.last();<\/code>\r\n<\/pre>\n<h3>3. Consistency<\/h3>\n<p>Every other unary operator is a prefix operator. Postfix increment and decrement are there to fill a specific need. If you don&#8217;t need them, don&#8217;t use them; it could make your intentions unclear to a future maintainer of your code. And you should be nice to this unknown future person &#8212; it may be you!<\/p>\n<h3>Summary<\/h3>\n<p>In many cases, C++ programmers can choose between prefix and postfix operators. For maximum efficiency, visibility and consistency, prefix operators are the best choice. There may come a time when the powers that be see the error of their ways and rename the language to &#8220;++C&#8221;. But until then, we can all do our own little bit to help.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Whenever you have a choice, you should use the C++ prefix increment and decrement operators (e.g. ++i) instead of the postfix versions (e.g. i++). This will make your code more efficient, clear and consistent. This advice applies (more or less) to most languages that have both prefix and postfix operators. 1. Efficiency Code like this [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[3],"class_list":["post-14","post","type-post","status-publish","format-standard","hentry","tag-cpp"],"_links":{"self":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts\/14","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/comments?post=14"}],"version-history":[{"count":0,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/posts\/14\/revisions"}],"wp:attachment":[{"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/media?parent=14"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/categories?post=14"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thunderguy.com\/semicolon\/wp-json\/wp\/v2\/tags?post=14"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}