<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/3-reasons-to-not-write-that-new-code-and-how-i-failed-at-it/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://jozefhajnala.gitlab.io/r/r904-dont-write-that-code/"> Jozef's Rblog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<h1>Introduction</h1>
<p>We all know that feeling. We have this great idea about a new project, feature, function, piece of code.</p>
<blockquote>
<p><em>What</em> do we want? <strong>Write that amazing new code</strong>! <br> <em>When</em> do we want it? <strong>Right NOW</strong>!</p>
</blockquote>
<p>The aim of this post is to try and give you at 3 good reasons to resist that urge and consider other options, be it in your business projects or your private projects. With an example of how I failed and how I tried to remedy that failure, on a very small scale.</p>
<div>
<img alt="He knows" src="https://i2.wp.com/i.imgur.com/7U5Qpii.jpg?w=456&ssl=1" data-recalc-dims="1">
<p>He knows</p>
</div>
</div>
<div>
<h1>The 3 reasons</h1>
<div>
<h2>1. New code takes time (and money)</h2>
<p>Writing new code is an investment. Time and money will be spent on designing, implementation and code review. These introductory investments are however only a minor part of the total cost of writing new code. The code must be well documented and maintained. The code must be integrated to other parts of the systems. Last but not least, the code must be tested, and writing tests usually involves writing, well, more new code.</p>
</div>
<div>
<h2>2. New code means new bugs</h2>
<p>Even through our best efforts and testing, bugs will be found and will need fixing. Numbers on this seem to vary a lot, <a rel="nofollow" href="https://www.amazon.com/Code-Complete-Practical-Handbook-Construction/dp/0735619670" target="_blank">Code Complete</a> by Steve McConnel estimates an industry average of 15-50 bugs per 1 000 lines of code.</p>
</div>
<div>
<h2>3. We write what we know</h2>
<p>Perhaps the most compelling reason to reconsider and resist the code-writing is not in the numbers and statistics, but in the simple realization that we usually write new code using our current knowledge.</p>
<p>Pausing for a while and spending time investigating on the current best practices and methods of solving the issue we are aiming to solve with our new code may not only save us and our business owners valuable resources, but also increase our knowledge base thanks to that investigation.</p>
</div>
</div>
<div>
<h1>Putting it to practice in the R world</h1>
<p>So we have this brilliant new idea. Instead of starting to write that shiny new code, we can also start with:</p>
<ol>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r904-dont-write-that-code/www.google.com" target="_blank">Google</a> – It is more than likely that someone has already stumbled upon this very same, or a very similar problem. How have they implemented it? What functionality have they used? What are the best practices and approaches to tackling similar issues?</li>
<li><a rel="nofollow" href="https://stackoverflow.com/questions/tagged/r" target="_blank">Stackoverflow</a> and <a rel="nofollow" href="https://rseek.org/" target="_blank">Rseek</a> for R solutions – Can we find solutions to our problem there? Are those solutions good? Can we build upon them?</li>
<li>Evaluate the options – If we have found any, which of them are the most suitable for us? If stability and maintainability is a major concern, can we find a solution with as few dependencies as possible ? If performance is a major concern, are benchmarks available (can we make them)?</li>
<li>Propose a solution – After this research, do we still need to write the new functionality? If so, how much can we build on existing solutions? Are they easy to integrate?</li>
<li>Do we care about dependencies? – The R world is special, one of the reasons for this is <a rel="nofollow" href="https://cran.r-project.org/" target="_blank">CRAN</a>. The number of packages available on CRAN passed 13 000 and it is very convenient to just reach out and grab one more. This approach however has its caveats.</li>
</ol>
</div>
<div>
<h1>A simplest example – learning from my own mistakes</h1>
<div>
<h2>How I did it wrong</h2>
<p>One of the first RStudio addins I have written for my own use was to <a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r101-addin-reproducibility/" target="_blank">run a script open in RStudio with <code>R --vanilla</code> via a keyboard shortcut</a> and open a file with the script’s output in RStudio. If I had to guess, my <em>thought process</em> was likely similar to the following:</p>
<ul>
<li>I <em>will to write a new function</em> to serve as the addin binding</li>
<li>I <em>will to write a new function</em> to serve as command executor for both Unix-like systems via <code>system</code> and Windows via <code>shell</code></li>
<li>I <em>will to write a new function</em> to create the command to be executed by the above</li>
<li>Maybe some utilities, like the ones converting <code>~</code> to a full path, figure out integrating the 4 together, passing arguments, etc.</li>
</ul>
<p>So, there I was, some time and <a rel="nofollow" href="https://gitlab.com/jozefhajnala/jhaddins/blob/71e08c0eca50f6b6ea55bd25ba658113784306a2/R/makeCmd.R" target="_blank">92 lines of code and doc later</a>, with a new useful RStudio addin. Oh and yes, there was also <a rel="nofollow" href="https://gitlab.com/jozefhajnala/jhaddins/blob/6f7564028eee9ea9d6d4d220ee1513b1de18b3f3/tests/testthat/test.makeCmd.R" target="_blank">102 lines of test code</a>, fixed a couple of times, too.</p>
</div>
<div>
<h2>How could I do it better</h2>
<p>After a second look a few months later when actually reviewing this supposedly <em>good</em> functionality, I realized that</p>
<ul>
<li>There is a base function called <code>system2</code>, which seems like a much more user-friendly and easy to use version of <code>system</code> (and <code>shell</code>), with no real need to write system-specific code and even though less configurable than <code>system</code>, still perfectly sufficient for my purpose</li>
<li>I do not actually need to make the command, as extra options can be passed to <code>system2</code> as arguments, including redirecting output</li>
<li>Oh, and I definitely do not need a function to convert <code>~</code> to full path, there is <code>path.expand</code></li>
</ul>
<p>So after a quick rewrite, we end up with a very similar functionality, only we suddenly need <a rel="nofollow" href="https://gitlab.com/jozefhajnala/jhaddins/blob/master/R/makeCmd.R" target="_blank">35 rows of code, doc included</a> and the tests shrink to <a rel="nofollow" href="https://gitlab.com/jozefhajnala/jhaddins/blob/master/tests/testthat/test.makeCmd.R" target="_blank">10 lines</a>, as there is only 1 function to test instead of 4. That is <em>less than a quarter</em> of the original amount of code to be maintained and bug-fixed, with 0 new dependencies added.</p>
<blockquote>
<p>This was of course a very trivial example. Real life problems of real-life projects will be much more difficult to solve. However, as complexity scales, the potential amount of time and resources saved will also scale.</p>
</blockquote>
<p>Good luck resisting that urge the next time it comes <img alt="😉" src="https://s.w.org/images/core/emoji/2.4/72x72/1f609.png" class="wp-smiley"></p>
<h2>Did you find the article helpful or interesting? Help others find it by sharing</h2>
<div></div>
</div>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://jozefhajnala.gitlab.io/r/r904-dont-write-that-code/"> Jozef's Rblog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/An__qKJf7ws" height="1"></a></div>