{"id":478,"date":"2010-05-17T10:03:43","date_gmt":"2010-05-17T10:03:43","guid":{"rendered":"http:\/\/kera.name\/articles\/?p=478"},"modified":"2011-06-22T09:28:08","modified_gmt":"2011-06-22T09:28:08","slug":"tomalaks-monday-monstrous-rant-i-align-your-asterisks","status":"publish","type":"post","link":"https:\/\/kera.name\/articles\/2010\/05\/tomalaks-monday-monstrous-rant-i-align-your-asterisks\/","title":{"rendered":"Tomalak&#039;s Monday Monstrous Rant #1: Align Your Asterisks!"},"content":{"rendered":"<p>Hi. I&#039;m here to address the age-old debate of C and C++; which pointer declaration syntax to use?<\/p>\n<p><textarea name=\"code\" class=\"cpp:nocontrols:nogutter\" cols=\"60\" rows=\"10\">Foo<em> ptr; \/\/ (1)\nFoo <\/em> ptr; \/\/ (2)\nFoo *ptr; \/\/ (3)<\/textarea>\n<\/p>\n<p>All three are equivalent because arbitrary spacing is meaningless in C\/C++.<\/p>\n<p>The problem with C and C++ is that its built-in operators have multiple meanings. In particular <code>*<\/code> means &#034;pointer type&#034;, &#034;multiplication&#034; and &#034;dereference&#034;. My goal when picking a convention is to, as much as possible, avoid using one that jars with another.<\/p>\n<p>Therefore, we&#039;ll ignore syntax 2 because it looks so much like multiplication.<\/p>\n<p>Unfortunately, there is no clear winner between the other two as each has at least one hindrance. Syntax 1 has the following:<\/p>\n<p><textarea name=\"code\" class=\"cpp:nocontrols:nogutter\" cols=\"60\" rows=\"10\">Foo* ptr1, ptr2;<\/textarea>\n<\/p>\n<p>In the code above, <code>ptr1<\/code> is a <code>Foo*<\/code>, but <code>ptr2<\/code> is only a <code>Foo<\/code>. One might have expected both to be <code>Foo*<\/code>s. Syntax 3 avoids this by keeping the asterisk with the name:<\/p>\n<p><textarea name=\"code\" class=\"cpp:nocontrols:nogutter\" cols=\"60\" rows=\"10\">Foo <em>ptr1, <\/em>ptr2;<\/textarea>\n<\/p>\n<p>This is really a throwback to C&#039;s desire to have you think in terms of &#034;I am declaring a <code>Foo<\/code> to which <code>ptr<\/code> points&#034;.<\/p>\n<p>Unfortunately, this is misleading to language newcomers because in fact you&#039;re not declaring or defining a <code>Foo<\/code> at all. You&#039;re merely declaring a pointer; you still haven&#039;t allocated any actual <code>Foo<\/code> for it to point to.<\/p>\n<p>A newbie reading &#034;<code>int *a<\/code>&#034; might think he&#039;s created an <code>int<\/code> and now gets to use &#034;the variable <code>*a<\/code>&#034; to access it, when in fact this is not the case at all; dereferencing such an undirected pointer would be very dangerous.<\/p>\n<p>Further, aligning the asterisk with the variable name looks like a dereference operation. One of the biggest stumbling blocks to language newcomers is the multiple meaning of operators such as <code>*<\/code> and <code>&amp;<\/code> &mdash; learning the difference between <code>*<\/code> on the LHS as a pointer type indicator and <code>*<\/code> on the RHS as a dereference operator is not aided if you see code that always aligns the asterisk in the same way.<\/p>\n<p>Additionally, the name of the type itself is &#034;<code>Foo*<\/code>&#034; (though array declaration and function type declaration syntax oddities do make this slightly more complicated in practice).<\/p>\n<p>To summarise:<\/p>\n<table width=\"100%\" border=\"1\" cellspacing=\"0\">\n<tr>\n<th>Rationale<\/th>\n<th>#1<\/th>\n<th>#2<\/th>\n<th>#3<\/th>\n<\/tr>\n<tr>\n<td>Indicates the type properly<\/td>\n<td style=\"background-color: green\">\u00a0<\/td>\n<td style=\"background-color: red\">\u00a0<\/td>\n<td style=\"background-color: red\">\u00a0<\/td>\n<\/tr>\n<tr>\n<td>Not surprising when declaring multiple variables<\/td>\n<td style=\"background-color: red\">\u00a0<\/td>\n<td style=\"background-color: white\">\u00a0<\/td>\n<td style=\"background-color: green\">\u00a0<\/td>\n<\/tr>\n<tr>\n<td>Does not lie about allocation<\/td>\n<td style=\"background-color: green\">\u00a0<\/td>\n<td style=\"background-color: white\">\u00a0<\/td>\n<td style=\"background-color: red\">\u00a0<\/td>\n<\/tr>\n<tr>\n<td>Doesn&#039;t look like dereference<\/td>\n<td style=\"background-color: green\">\u00a0<\/td>\n<td style=\"background-color: green\">\u00a0<\/td>\n<td style=\"background-color: red\">\u00a0<\/td>\n<\/tr>\n<tr>\n<td>Doesn&#039;t look like multiplication<\/td>\n<td style=\"background-color: green\">\u00a0<\/td>\n<td style=\"background-color: red\">\u00a0<\/td>\n<td style=\"background-color: green\">\u00a0<\/td>\n<\/tr>\n<\/table>\n<p>In conclusion:<\/p>\n<p><code>Foo *p<\/code> is rubbish; it has three cons and two pros. <code>Foo* p<\/code> is great; it has one con and four pros&#8230; important pros.<\/p>\n<p>Thanks for reading.<\/p>\n<p><strong>Updated 31\/07\/2010:<\/strong> Improved table layout for clarity.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I address the age-old debate of C and C++; which pointer initialisation syntax to use?<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[21,49,20],"_links":{"self":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts\/478"}],"collection":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/comments?post=478"}],"version-history":[{"count":14,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts\/478\/revisions"}],"predecessor-version":[{"id":723,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts\/478\/revisions\/723"}],"wp:attachment":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/media?parent=478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/categories?post=478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/tags?post=478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}