<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Mentoring on MoskitoHero</title><link>/tags/mentoring/</link><description>Recent content in Mentoring on MoskitoHero</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Fri, 12 Jan 2024 00:00:00 +0000</lastBuildDate><atom:link href="/tags/mentoring/index.xml" rel="self" type="application/rss+xml"/><item><title>Ruby Best Practices - Method Arguments</title><link>/post/2024-01-12-ruby-best-practices-method-arguments/</link><pubDate>Fri, 12 Jan 2024 00:00:00 +0000</pubDate><guid>/post/2024-01-12-ruby-best-practices-method-arguments/</guid><description>Simple rules for ordering Ruby method arguments — put optional arguments last, and switch to keyword arguments when you have more than one optional parameter.</description><content:encoded><![CDATA[<div class="paragraph">
<p>To write readable and maintainable code, follow a few simple rules about argument ordering.</p>
</div>
<div class="sect1">
<h2 id="tip-1-optional-arguments-go-last"><a class="anchor" href="#tip-1-optional-arguments-go-last"></a>Tip 1: Optional arguments go last</h2>
<div class="sectionbody">
<div class="paragraph">
<p>When a method takes several arguments, and some of them have default values, make sure to put the optional ones at the end.
Take this method definition:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="s2">&#34;val_b&#34;</span><span class="p">,</span> <span class="n">c</span><span class="p">)</span>
  <span class="c1"># some code...</span>
<span class="k">end</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>Looks fine at first glance. But in practice, it’s impossible to call this method with values for a and c without also specifying b. You end up writing:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="n">foo</span><span class="p">(</span><span class="s2">&#34;val_a&#34;</span><span class="p">,</span> <span class="s2">&#34;val_b&#34;</span><span class="p">,</span> <span class="s2">&#34;val_c&#34;</span><span class="p">)</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>…​which defeats the purpose of setting a default for b if you’re forced to specify it every time. ¯<em>(ツ)</em>/¯</p>
</div>
<div class="sect2">
<h3 id="solution-move-optional-arguments-to-the-end"><a class="anchor" href="#solution-move-optional-arguments-to-the-end"></a>Solution: move optional arguments to the end</h3>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="s2">&#34;val_b&#34;</span><span class="p">)</span>
  <span class="c1"># some code...</span>
<span class="k">end</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>Now you can call:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="n">foo</span><span class="p">(</span><span class="s2">&#34;val_a&#34;</span><span class="p">,</span> <span class="s2">&#34;val_c&#34;</span><span class="p">)</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>No problem 😎.</p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="tip-2-use-keyword-arguments-aka-kwargs"><a class="anchor" href="#tip-2-use-keyword-arguments-aka-kwargs"></a>Tip 2: Use keyword arguments (aka kwargs)</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Tip 1 only gets you so far. What if you have multiple optional arguments and want to override just the last one? You’d still need to specify all the previous ones.
For example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span> <span class="o">=</span> <span class="s2">&#34;val_c&#34;</span><span class="p">,</span> <span class="n">d</span> <span class="o">=</span> <span class="s2">&#34;val_d&#34;</span><span class="p">,</span> <span class="n">e</span> <span class="o">=</span> <span class="s2">&#34;val_e&#34;</span><span class="p">)</span>
  <span class="c1"># some code...</span>
<span class="k">end</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>Want to change only e? Too bad — you still have to write:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="n">foo</span><span class="p">(</span><span class="s2">&#34;val_a&#34;</span><span class="p">,</span> <span class="s2">&#34;val_b&#34;</span><span class="p">,</span> <span class="s2">&#34;val_c&#34;</span><span class="p">,</span> <span class="s2">&#34;val_d&#34;</span><span class="p">,</span> <span class="s2">&#34;my_val_e&#34;</span><span class="p">)</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>Now imagine this in a large codebase:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="n">foo</span><span class="p">(</span><span class="n">user</span><span class="p">.</span><span class="nf">id</span><span class="p">,</span> <span class="s2">&#34;DONE&#34;</span><span class="p">,</span> <span class="kp">true</span><span class="p">,</span> <span class="kp">false</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="s2">&#34;always&#34;</span><span class="p">,</span> <span class="kp">true</span><span class="p">,</span> <span class="n">post</span><span class="p">.</span><span class="nf">id</span><span class="p">)</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>And you’re left thinking: <em>&#34;Wait, what’s that <strong>false</strong> again? What does the <strong>1</strong> do? Why <strong>&#39;always&#39;</strong>?&#34;</em>
Not ideal, especially under pressure in a production bug fix.</p>
</div>
<div class="sect2">
<h3 id="solution-switch-to-keyword-arguments-when-you-hit-2-optional-params"><a class="anchor" href="#solution-switch-to-keyword-arguments-when-you-hit-2-optional-params"></a>Solution: switch to keyword arguments when you hit 2+ optional params</h3>
<div class="paragraph">
<p>Benefits:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Argument order doesn’t matter anymore</p>
</li>
<li>
<p>You can ignore defaults entirely</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The method now becomes:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="ss">c: </span><span class="s2">&#34;val_c&#34;</span><span class="p">,</span> <span class="ss">d: </span><span class="s2">&#34;val_d&#34;</span><span class="p">,</span> <span class="ss">e: </span><span class="s2">&#34;val_e&#34;</span><span class="p">)</span>
  <span class="c1"># some code...</span>
<span class="k">end</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>To change only e, you write:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="n">foo</span><span class="p">(</span><span class="s2">&#34;val_a&#34;</span><span class="p">,</span> <span class="s2">&#34;val_b&#34;</span><span class="p">,</span> <span class="ss">e: </span><span class="s2">&#34;my_val_e&#34;</span><span class="p">)</span></code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="ninja-level-tip-the-clean-coders-take"><a class="anchor" href="#ninja-level-tip-the-clean-coders-take"></a>Ninja-level tip — the clean coder’s take:</h2>
<div class="sectionbody">
<div class="paragraph">
<p><strong>If your method takes more than 1 argument, make everything after the first a keyword argument. Always.</strong></p>
</div>
<div class="sect2">
<h3 id="example"><a class="anchor" href="#example"></a>Example:</h3>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="k">def</span> <span class="nf">send_message_to</span><span class="p">(</span><span class="n">recipient</span><span class="p">,</span> <span class="ss">confirm: </span><span class="kp">true</span><span class="p">,</span> <span class="ss">bcc: </span><span class="s2">&#34;joe@bill.com&#34;</span><span class="p">,</span> <span class="ss">cc: </span><span class="s2">&#34;jane@doe.com&#34;</span><span class="p">)</span>
  <span class="c1"># ruby code</span>
<span class="k">end</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>Here, the first argument is obvious thanks to the method name: you’re sending a message <em>to</em> someone.
Called without options:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="n">send_message_to</span><span class="p">(</span><span class="s2">&#34;sam@sam.com&#34;</span><span class="p">)</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>Clear and concise.
Compare a non-keyword version:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="n">send_message</span><span class="p">(</span><span class="s2">&#34;recipient@mail.com&#34;</span><span class="p">,</span> <span class="kp">false</span><span class="p">,</span> <span class="s2">&#34;joe@bill.com&#34;</span><span class="p">,</span> <span class="s2">&#34;paul@smith.com&#34;</span><span class="p">)</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>…​with the keyword version:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="n">send_message_to</span><span class="p">(</span><span class="s2">&#34;sam@sam.com&#34;</span><span class="p">,</span> <span class="ss">bcc: </span><span class="s2">&#34;paul@smith.com&#34;</span><span class="p">,</span> <span class="ss">confirm: </span><span class="kp">false</span><span class="p">)</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>If you adopt these habits, <a href="&lt;em&gt;GHOST_URL&lt;/em&gt;/writing-code-for-your-future-self">future you</a> will thank you when you revisit the code to debug or tack on yet another parameter.</p>
</div>
</div>
</div>
</div>
]]></content:encoded></item><item><title>Love your error messages as thyself</title><link>/post/2022-05-17-learn-your-error-messages-as-thyself/</link><pubDate>Tue, 17 May 2022 07:44:36 +0000</pubDate><guid>/post/2022-05-17-learn-your-error-messages-as-thyself/</guid><description>Stop ignoring error messages and start reading them — it will make you a better developer and help you understand your language from the inside out.</description><content:encoded><![CDATA[<div class="paragraph">
<p>Learn to hate, then love error messages.</p>
</div>
<div class="sect1">
<h2 id="stack-overflowoverflow"><a class="anchor" href="#stack-overflowoverflow"></a>Stack Overflow overflow</h2>
<div class="sectionbody">
<div class="paragraph">
<p>I have lost so much time on Stack Overflow.</p>
</div>
<div class="paragraph">
<p>I mean Stack Overflow is great, and software development knowledge would not be so advanced and so widespread without its great influence. What I mean is that I have often found myself repeating the endless pattern of <em>&#39;Hey, I have an error…​ Wow, so many words…​ Let’s google this. Stack Overflow has it! Let’s see what they say&#39;</em></p>
</div>
<div class="paragraph">
<p>I am not talking about the infamous Stack Overflow copy-paste syndrome, nor am I dismissing the great answers produced by the great developers out ther, but rather about the missing step in the example above: making sense of the error message myself before actually looking for a ready-made answer.</p>
</div>
<div class="paragraph">
<p>Our brain is so quick at taking shortcuts that it often makes us lazy and skip the seemingly unneeded steps our thinking needs to fully develop. Take the example of walking. Although I don’t exactly remember taking my first step in this world, I can imagine it was a movement that required so much concentration - not to say bravery - and coordination that it must have felt quite awkward at the beginning. Today, I don’t even think about it. My brain just orchestrates everything, allowing me to go for a walk and actually think about something else.</p>
</div>
<div class="paragraph">
<p>Think about the first day you typed your first computer program. Think about the way you were thinking at the time. Maybe you are currently learning computer science and many mechanisms experienced developers don’t even think about are steps you consciously take, one by one, in order to reach code quality - or even to write code that actually works.</p>
</div>
<div class="paragraph">
<p>And while learning, we all encountered bugs. The language we were coding in was talking to us with complicated words, and concepts we sometimes did not even know about. We all googled for solutions. And Stack Overflow is so well conceived and maintained that we most certainly found the answer there. And it happened again. Error messages were so hard to understand, so cryptic, sometimes. So we just kept copy-pasting the error messages, and it just kept throwing correct answers at you, which, with a little adaptation, just worked.</p>
</div>
<div class="paragraph">
<p><em>And then, your brain skipped the error-message-reading step.</em></p>
</div>
<div class="paragraph">
<p>But we don’t want to just be very, very, very talented googlers, do we?</p>
</div>
</div>
</div>
<h1 id="rtfem" class="sect0"><a class="anchor" href="#rtfem"></a>RTFEM!</h1>
<div class="paragraph">
<p>Read the f***ing error messages!</p>
</div>
<div class="paragraph">
<p>So I encourage all of you learners to actually take a long look at the error messages that are thrown right in your face just as you were expecting a successful compilation. Take the time to read them. Take the time to look at those weird symbols that show up here and there. And you know what? You may not get to the point quicker at first, but you will gain a deeper knowledge of how your language is actually built, how it works internally. You might look at your fellow programmers increase their skills faster on the surface, but your understanding of error messages will eventually turn you into a Code Jedi Master.</p>
</div>
<div class="paragraph">
<p>What made me really stop overlooking error messages was reading <em>Ruby Under A Microscope</em> by Pat Shaughnessy, a most excellent book about the way Ruby is actually written in C, the way the language parser works and the way it turns the words you write into machine-understandable concepts.</p>
</div>
<div class="paragraph">
<p>Somewhere in the first part of the book that introduces how the parser works, the author talks about language tokenisation, which is - very basically - how the parser takes words and understand how to match them with a feature. And here they were, in a code example, my long-seen-yet-so-little-known error message friends, namely <code>tSTRING</code>, <code>tCONSTANT</code> and <code>tINTEGER</code>.</p>
</div>
<div class="paragraph">
<p>If only I had known what these correspond to before, <a href="https://makandracards.com/makandra/15611-how-to-fix-unexpected-token-error-for-json-parse">error messages such as these</a> would never have had me google for an answer.</p>
</div>
<div class="paragraph">
<p>But how could I know? When you google for ruby tString, you get all sorts of results, mostly skipping the preceding t. In order to get to the real stuff, I should have taken the time to actually read the error messages and wonder:</p>
</div>
<div class="paragraph">
<p>What is the language trying to tell me?</p>
</div>
<div class="paragraph">
<p>And if that sounds awkward, just remember that your programming language is actually a computer program written by other humans, and ask yourself:</p>
</div>
<div class="paragraph">
<p>What is the author of the language telling me to look at?</p>
</div>
<div class="paragraph">
<p>Chances are he is trying to get you to look at features of the language that may not be plain to see, but a little hidden from sight. And if he is trying to have you look at these, you might just as well do it. And learn.</p>
</div>
<div class="sect1">
<h2 id="learn-to-love-your-errormessages"><a class="anchor" href="#learn-to-love-your-errormessages"></a>Learn to love your error messages</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Not only should you read error messages carefully, but you should also take the time to craft yours with love and care. Like I said in an earlier post, <a href="https://moskitohero.com/development/2021/11/21/writing-code-for-your-future-self.html">you should always try to write code with your future self in mind</a>. And I guess the same applies to error messages.</p>
</div>
<div class="paragraph">
<p>Don’t just raise <code>Error</code>, or <code>StandardError</code> in your lib. Actually write your own <code>OddError</code> class. Give it a message. Make it clear. Make it so your user - and that may be the future you - is eager to look at what you coded and understand how it is architectured.</p>
</div>
<div class="paragraph">
<p>An error message must be clear, so it requires your code to be clear, and therefore it requires your programming mind to be clear about what the code is actually doing. Error messages tell something about your self.</p>
</div>
<div class="paragraph">
<p>Error messages are the conversation between the author of a program and his users. As a programmer, you will be either one or the other, in turn, and often both at the same time.</p>
</div>
<div class="paragraph">
<p>Error messages will make you grow while <em>learning</em> software. They will make you grow while <em>using</em> software. They will make you grow while <em>writing</em> software. Do not just dismiss them and skip to the Stack Overflow step.</p>
</div>
</div>
</div>
]]></content:encoded></item></channel></rss>