<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/bad-coder-bad-coder/"></div></div><div><a data-url="http://www.r-bloggers.com/bad-coder-bad-coder/" data-counturl="http://www.r-bloggers.com/bad-coder-bad-coder/" data-count="horizontal" data-text="Bad Coder, Bad Coder!" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://matloff.wordpress.com/2016/07/07/bad-coder-bad-coder/"> Mad (Data) Scientist</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>My title here is in the sense of “Bad dog, bad dog!”, a scolding I sometimes see dog owners use to tame their pets, and is also an allusion to <a target="_blank" href="http://www.sfgate.com/comics/slideshow/Bad-Reporter-58048.php">Bad Reporter</a>, a sometimes hilarious and always irreverent political comic strip in the <em>San Francisco Chronicle</em>. And my title is intended to convey the point that I think that “good programming practice” rules are sometimes taken overly seriously.</p>
<p>I’ll comment on two aspects here:</p>
<ul>
<li>Variable names, spacing etc.</li>
<li>“Side effects.”</li>
</ul>
<p>The second is the more unsettling of the two, but the first is interesting because Hadley Wickham is involved, which always gets people’s attention.<img height="16" alt=":-)" width="16" class="wp-smiley" src="https://s1.wp.com/wp-content/mu-plugins/wpcom-smileys/simple-smile.svg" draggable="false"></p>
<p>What prompted this is that there is currently a discussion in the ASA Statistical Learning and Data Mining Section e-mail list on Hadley’s <a target="_blank" href="http://adv-r.had.co.nz/Style.html">coding style guidelines</a>.  Some of you may know that Google has also had <a target="_blank" href="https://google.github.io/styleguide/Rguide.xml">its own R style guidelines</a> for some years now.</p>
<p>Much as I admire Hadley and Google, I really think this is much ado about nothing. I do have my own personal style (if you are curious, just look at my code), but seriously, folks, I don’t think it really matters. As long as a programmer is consistent  and has given careful thought as to how to be clear — not only clear to others who read the code, but also clear to the programmer herself when she reads the code cold two months from now — it’s fine.</p>
<p>I would like to see better style in programmers’ <em>English</em>, though. Please STOP using the terms “curly braces” and “square brackets”! Braces ARE curly, by definition, and brackets ARE square, right? Or, at least be consistent in your redundancy, speaking of “round parentheses,” “dotty periods,” “long dashes,” “big capitals,” “elevated overbars” and so on.<img height="16" alt=":-)" width="16" class="wp-smiley" src="https://s1.wp.com/wp-content/mu-plugins/wpcom-smileys/simple-smile.svg" draggable="false"></p>
<p>And now, for the hard stuff, side effects. R is a <em>functional language</em>, meaning among other things that no variable is changed upon executing a function, other than assignment of the return value. In other words,</p>
<p>y &lt;- f(x)</p>
<p>will change <strong>y</strong> but neither <strong>x</strong> nor any other variables, e.g. globals, will be changed.</p>
<p>For many in the R community, this restriction is followed with religious-like fervor. They consider it safe, clear and easy to debug.</p>
<p>Yes, yes, I understand the arguments, and the No Side Effecters have a point. But it is much too dogmatic, in my opinion, and often causes one to incur BIG performance penalties.</p>
<p>I am not here to praise Caeser, but I’m not here to bury him either.<img height="16" alt=":-)" width="16" class="wp-smiley" src="https://s1.wp.com/wp-content/mu-plugins/wpcom-smileys/simple-smile.svg" draggable="false"> If you want to subscribe to that anti-side effects philosophy, fine. My only point is that, like it or not, R is slowly moving away from the banning of side effects. I would cite a few examples:</p>
<ul>
<li>Reference classes. R has always had some ways to create side effects for the antisocial<img height="16" alt=":-)" width="16" class="wp-smiley" src="https://s1.wp.com/wp-content/mu-plugins/wpcom-smileys/simple-smile.svg" draggable="false"> , one notable example being environments. Reference classes carry that loophole one step further.</li>
<li>The <strong>bigmemory</strong> package. Yes, it was developed as a workaround to R’s 32-bit memory constraints (somewhat but not completely resolved in more recent versions of R), but I’m told that one of the reasons for the package’s popularity is its ability change data in-place in memory. This is basically a side effects issue, as traditionally the assignment to <strong>y</strong> above has created a new, separate copy of <strong>y</strong> in memory, often a big performance killer. (Again, this too has been ameliorated somewhat in recent versions of R.)</li>
<li>The <strong>data.table</strong> package. Same comments here as for the “insidious” use of <strong>bigmemory</strong> above, but also consider the operation> setkey(mtc1,cyl).Ever notice that no assignment is made? If you rave about <strong>data.table,</strong> you should keep in mind what factors underly that lightning speed.</li>
</ul>
<p>I won’t get into the global variables issue here, other than to say again that I think the arguments against them often tend to be more ideological than logical.</p>
<p>In summary, I believe that careful thought, rather than an obsession with rules, is your ticket to good — clear and efficient — code.</p>
<p> </p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/matloff.wordpress.com/1346/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/matloff.wordpress.com/1346/" border="0"></a> <img alt="" src="http://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/bad-coder-bad-coder/"></div></div><div><a data-url="http://www.r-bloggers.com/bad-coder-bad-coder/" data-counturl="http://www.r-bloggers.com/bad-coder-bad-coder/" data-count="vertical" data-text="Bad Coder, Bad Coder!" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://matloff.wordpress.com/2016/07/07/bad-coder-bad-coder/"> Mad (Data) Scientist</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/4mEgx4omO8o"></a></div>