<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Benji Stephenson</title>
    <description>The latest articles on DEV Community by Benji Stephenson (@benjistephenson).</description>
    <link>https://dev.to/benjistephenson</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F613443%2F1559366e-3fdc-4156-8061-243198826517.jpeg</url>
      <title>DEV Community: Benji Stephenson</title>
      <link>https://dev.to/benjistephenson</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/benjistephenson"/>
    <language>en</language>
    <item>
      <title>Make Expressions, not Statements</title>
      <dc:creator>Benji Stephenson</dc:creator>
      <pubDate>Thu, 17 Aug 2023 23:42:49 +0000</pubDate>
      <link>https://dev.to/benjistephenson/make-expressions-not-statements-5dg9</link>
      <guid>https://dev.to/benjistephenson/make-expressions-not-statements-5dg9</guid>
      <description>&lt;p&gt;I was chatting to a colleague about Haskell the other day and some of the qualities it has, such as everything being an expression and how that can encourage the use of smaller functions.&lt;br&gt;
The drive to use expressions over statements is something that I've advocated for for quite a while at work as I think it has a strong set of benefits over merely making statements.  Although not all languages make this a first class citizen, it's still usually possible to do.&lt;/p&gt;

&lt;p&gt;TL;DR at the bottom&lt;/p&gt;

&lt;p&gt;I'll use a couple of languages, but let's start with Typescript.  Say we want to get the age bracket of an individual, based on their age range.  We might do something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;ageBracket&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;

&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;ageBracket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;junior&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;ageBracket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;teen&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nx"&gt;ageBracket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;adult&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So what's could we say is a problem with that?  We could wrap the logic up in a function and avoid the mutability, but I think the aspect that we're still dealing with statements remains.  A statement is a bit of code that does not return a value and so can't be chained very easily.  An expression on the other hand returns some value and can easily be built into a larger expression to achieve something more complex.&lt;br&gt;
Scala is a more expression based language and so allows us to do things like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;ageBracket&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="s"&gt;"junior"&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="s"&gt;"teen"&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s"&gt;"adult"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One mode of thought is that statements describe &lt;em&gt;how&lt;/em&gt; to do things, expressions state &lt;em&gt;what&lt;/em&gt; to do.  Here's another example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's quite low level in that is describing how to loop through an array of elements and then compute a running total.  The &lt;code&gt;for&lt;/code&gt; statement also does not return anything, if we wanted to do something afterwards, we must do so with a new statement.  Compare with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Scala we get something ever more pleasant:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="py"&gt;sum&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="py"&gt;fold&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)(&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great, so what?  Let's add a couple more example steps, again in Typescript, well filter for even numbers, double them and then get the word for the number (2 =&amp;gt; "two"):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...]&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;odds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;odds&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;doubles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;odds&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;doubles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;odds&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;doubles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;names&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;getNumberWord&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;doubles&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We end up with quite a lot of intermediary variables.  If we change the functionality we need to keep track of whether we should get rid of them or not, IDEs help with this of course, but it can still be pretty easy to end up with unused code lying around.  Compare with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...]&lt;/span&gt;
   &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;getNumberWord&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I find it to be much more &lt;em&gt;descriptive and declarative&lt;/em&gt;, we talk about what it is we want the code to do and don't have to be concerned with the how.&lt;/p&gt;

&lt;p&gt;Related to the intermediary variables point, expressions also allow us to more tightly group related bits of functionality or logic together.  To go back to the &lt;code&gt;sum&lt;/code&gt; example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the initial value for total and indeed the logic to do the addition is free to be moved around, away from the loop implementation.  Not implicitly a bad thing but it does make code harder to reason about when it's spread apart.  What if we also forgot to initialise &lt;code&gt;total&lt;/code&gt;? Depending on the language, we could get an exception or we could get behaviour we didn't expect.  Javascript isn't as well behaved, but in the Scala version&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="py"&gt;sum&lt;/span&gt;
&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="py"&gt;fold&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)(&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;it's impossible to forget the initial &lt;code&gt;0&lt;/code&gt; value for the &lt;code&gt;fold&lt;/code&gt; as the compiler would yell at us and in the &lt;code&gt;sum&lt;/code&gt; version it's implicit.&lt;/p&gt;

&lt;p&gt;Obviously these are really contrived examples but generally I think they illustrate the point.  When we have statement orientated code we usually end up with less of a set of , declarative building blocks and more of a longer set of lower level instructions that are much harder to compose.&lt;/p&gt;

&lt;p&gt;There's a great &lt;a href="https://www.youtube.com/watch?v=xwDhKV7CqAY&amp;amp;t=3320s" rel="noopener noreferrer"&gt;talk&lt;/a&gt; by Paul Louth, the writer of the C# functional programming lib &lt;a href="https://github.com/louthy/language-ext" rel="noopener noreferrer"&gt;language-ext&lt;/a&gt; where he talks about statements vs expressions that's well worth a watch., he talks about much more than I'll go into here.  I love his examples (around 25:30) of how statements are much more open the insertion of side-effecting changes in a more uncoordinated way, whereas the expression based approach forces you to be more structured and deliberate.&lt;/p&gt;

&lt;p&gt;I'm a big fan of FP and FP languages, but lots of the patterns and practices that I think make them great can be applied to other languages too and this is definitely one of them, I hope you give it a try and see how it feels.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In my experience, expression leaning code makes for a much nicer experience in writing and reading code.  I find it helps to create more maintainable and composable solutions to problems.&lt;/p&gt;

&lt;p&gt;Statements &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;describe how to do it&lt;/li&gt;
&lt;li&gt;leave the code open to modification&lt;/li&gt;
&lt;li&gt;intermediate variables/state&lt;/li&gt;
&lt;li&gt;tend towards more mutability&lt;/li&gt;
&lt;li&gt;easier to rot&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Expressions&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;return values&lt;/li&gt;
&lt;li&gt;describe what to do&lt;/li&gt;
&lt;li&gt;keeps related logic/functionality contained and together&lt;/li&gt;
&lt;li&gt;leave the code open for extension&lt;/li&gt;
&lt;li&gt;composable&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>practices</category>
      <category>functional</category>
    </item>
    <item>
      <title>Week-note; starting 07 Aug 2023</title>
      <dc:creator>Benji Stephenson</dc:creator>
      <pubDate>Sun, 13 Aug 2023 05:38:24 +0000</pubDate>
      <link>https://dev.to/benjistephenson/week-note-starting-07-aug-2023-5g62</link>
      <guid>https://dev.to/benjistephenson/week-note-starting-07-aug-2023-5g62</guid>
      <description>&lt;p&gt;Historically, I've been pretty consistently inconsistent with practices like these and having missed the last two week notes I'm reminded how difficult it is to form good habits.&lt;/p&gt;

&lt;p&gt;It's been a rather busy couple of work weeks though, despite not having an active client to work with.  Participating in the flow framework workshop that we ran with a client went really well and I got some great experience in facilitating larger groups, so looking forward to the next one.  Also has the pleasure of co-running a SAFe advanced Scrum coach training with a stellar facilitator that I learned a lot from.&lt;/p&gt;

&lt;p&gt;The Agile Coaching Lab sessions continue to be great and the timing is exactly what I need as we're covering a lot of facilitation techniques now.  I can't wait to put some of it to use in my next client role which I think will take on more of an engineering coaching shape.  It's good challenging my idea of what I think my ideal career path looks like, but I'm still pretty sure it would contain some amount of code.  A change a perspective is always good though and I got some of that from attending a portfolio management mini-conference so see what technology looks and feels like from a business lens.&lt;/p&gt;

&lt;p&gt;I'm keeping it short this week, ease back into it and all that.&lt;/p&gt;

</description>
      <category>weeknote</category>
    </item>
    <item>
      <title>Week-note; starting 17 July 2023</title>
      <dc:creator>Benji Stephenson</dc:creator>
      <pubDate>Fri, 21 Jul 2023 06:43:20 +0000</pubDate>
      <link>https://dev.to/benjistephenson/week-note-starting-17-july-2023-4ga</link>
      <guid>https://dev.to/benjistephenson/week-note-starting-17-july-2023-4ga</guid>
      <description>&lt;p&gt;Another week, and even more coursework content has gone by.  Alongside combing up to the 1/2 way point for the  Agile Coaching Lab course I'm also about 1/2 way though the Flow Framework Advisor certification which has been pretty interesting.&lt;/p&gt;

&lt;p&gt;ACL-wise, we've been covering the Clean Language practice and more generally the idea of powerful questions; how do we ask and prompt in a way that juices as much info out of our clients as possible while also ensuring they self reflect and feel held.  It reminds me of the "are we in solution mode or listening mode" view that I've experienced before.  We've got a short workshop to run next week on Concrete Practice which I'm looking forward to co-running,&lt;br&gt;
It's actually been quite nice to have a course on the go alongside the ACL, some of the content we've covered on mentoring skills and teaching techniques have prompted me to think about ways to convey some of the Flow concepts to clients that links to what they already know about their organisation and current practices.&lt;/p&gt;

&lt;p&gt;Also combined with existing work commitments has been a real test of my time management skills, which are feeling a bit bruised.&lt;br&gt;
Work-wise I'm shadowing and helping prep for a Flow Framework workshop for a pretty sizeable group of 30 people.  I enjoy being challenged to facilitate back things that I've learned and it also keeps me honest in making sure I really understand stuff and have'n't just gamed the course to pass the exam!&lt;/p&gt;

&lt;p&gt;Other work bits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hacking away at automating some of our annual leave process via a Typescript AWS lambda; yes, node_modules is the heaviest object in the universe.&lt;/li&gt;
&lt;li&gt;Nice Friday Zoom drinks session that's prompted me to try writing a little more&lt;/li&gt;
&lt;li&gt;Looking forward to our monthly get together next week&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Away from work but at the same keyboard:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Like every Vim user in existence, the next config will be the best one; I'm rewriting my Fennel nvim setup and it's taking approx. 400% longer than expected&lt;/li&gt;
&lt;li&gt;Back to Tai Chi practice for a new term and a new form&lt;/li&gt;
&lt;li&gt;Will I ever be done with visa applications?  Who knows; it's currently been "in transit" from NZ to Aus for 3 days, which is amazing given it's a 3 hour flight.&lt;/li&gt;
&lt;li&gt;Booked up for Taste Martinborough 🍷&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Look after yourselves.&lt;/p&gt;

</description>
      <category>weeknote</category>
    </item>
    <item>
      <title>Week note; 10 July 2023</title>
      <dc:creator>Benji Stephenson</dc:creator>
      <pubDate>Thu, 13 Jul 2023 02:08:57 +0000</pubDate>
      <link>https://dev.to/benjistephenson/week-note-09-july-2023-45kk</link>
      <guid>https://dev.to/benjistephenson/week-note-09-july-2023-45kk</guid>
      <description>&lt;p&gt;This week has been mostly a blur of training videos, articles and a couple of practice exams.  Trying to juggle my time between things has been a challenge in and of itself, never mind the actual work involved.  On the go this week has been; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Coaching lab week 2 activities&lt;/li&gt;
&lt;li&gt;Flow Framework training classes&lt;/li&gt;
&lt;li&gt;Code and deploy the internal leave automation service&lt;/li&gt;
&lt;li&gt;Aussie visa paperwork&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The coaching lab activities were all based around aspects of the agile coach role, how to orient yourself in a new system/team/org and then opportunities to give each other feedback on some slides pitching the coach role.  I generally consider myself a pretty introspective person but a lot of the course activities have been a nudge to to do it more and in finer detail, which surely can only be a good thing!  Providing it reveals all roses, I suppose...&lt;/p&gt;

&lt;p&gt;I've actually enjoyed working on things that are outside of my usual comfort zone as an engineer/aspiring architect more that I thought I would but the bonus is that they all are related in the general digital profession sense.&lt;/p&gt;

&lt;p&gt;Ticking off the achievement list:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Submit Aus visa (so much paperwork.... Wonder if they want to talk to someone about their digital services.)&lt;/li&gt;
&lt;li&gt;Complete ACL week 2 homework&lt;/li&gt;
&lt;li&gt;Learned that mocha pot coffee is, in fact, the best home brew&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And all that with my half finished FFXVI game staring at me from across the room!&lt;/p&gt;

&lt;p&gt;It's a long weekend ahead, so hopefully my keyboard time will be low and I'll get out on a nice walk.&lt;/p&gt;

&lt;p&gt;Mānawaita a Matariki 👋🏻&lt;/p&gt;

</description>
      <category>weeknote</category>
    </item>
    <item>
      <title>10 Things I Learned About Code</title>
      <dc:creator>Benji Stephenson</dc:creator>
      <pubDate>Mon, 10 Jul 2023 01:20:15 +0000</pubDate>
      <link>https://dev.to/benjistephenson/10-things-i-learned-about-code-5c0g</link>
      <guid>https://dev.to/benjistephenson/10-things-i-learned-about-code-5c0g</guid>
      <description>&lt;p&gt;Or, more accurately, 10 things I learned about &lt;em&gt;software&lt;/em&gt; that I wish I knew when I started.  But that's not as catchy and didn't fit on the film poster so you'll have to indulge me and my love for cheesy films.&lt;/p&gt;

&lt;p&gt;I've been in the software game for about 12 years by now, maybe a few more years if you count the early teen years of playing with Borland Delphi (which is rather strange now that I think of it) and even slightly longer if you want to count all the time having to write BASIC on my green screen Amstrad when I was an actual bairn.  So by all accounts, I'm still rather early in my career and still have a million and one things left to learn and probably even more mistakes to make and pick myself up from.  But I'm mid-30s now, my knees hurt and now I need to be in bed by 10 pm so I feel long enough in the tooth to offer some unsolicited advice to the internet.&lt;/p&gt;

&lt;p&gt;I've had a varied time in the workplace; from one of the big consultancies to start-ups, government to games, junior to senior (whatever that means). These are ten of the things about working in tech that I've been gifted by people much wiser than me, or found out for myself through my journey that I wish I knew at the start.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Software is mostly about people
&lt;/h3&gt;

&lt;p&gt;Machines run our code, but that's mostly beside the point; machines can't get annoyed with us and they certainly don't pay our wages, so it's people that we need to foster good relationships with.&lt;br&gt;
The stereotypical engineer likes basements, coffee, arguing over Emacs vs Vim (that's old people VSCode for those of you under 30) and not talking to other humans.  But, other humans are where we get our problem statements from, who we're delivering solutions for, who we craft the code with, etc.  Making time in our day to forge and maintain relationships with colleagues and peers gives us a network of support and knowledge.  We do our best work in teams, after all.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Converse more than you code
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.azquotes.com/quote/811850" rel="noopener noreferrer"&gt;&lt;em&gt;Insert overused Einstein quote here.&lt;/em&gt;&lt;/a&gt;&lt;br&gt;
We're coders, so therefore we like to and are drawn to writing code.  I think that one of the differentiators between an ok engineer and a great engineer is how they talk through and define the problem they're trying to solve.  Talk to more people about it and you'll get more detail, more perspectives, more possible approaches and you'll cultivate a wider, shared understanding of the problem and solutions.  The better you can talk about and define the problem, the easier and quicker it'll be to solve through code (or maybe you'll discover you don't need to write any!).&lt;/p&gt;

&lt;h3&gt;
  
  
  3. The job's not done just because "it works"
&lt;/h3&gt;

&lt;p&gt;Getting something working is great; writing up a solution to a problem is so satisfying and probably the thing that most of us enjoy the most about the job.  But it doesn't all stop there.  Earlier on in my career, I missed out on the refactoring opportunities, learning how to tidy up the points where my code integrates with other peoples' or systems, all the different ways to approach testing etc.  Even when all your tests pass, there might still be a few tweaks to your design you can make.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Life's full of contradictions; most of the time "it works" is good enough
&lt;/h3&gt;

&lt;p&gt;Don't let perfect be the enemy of good; there absolutely comes a point where we should just move on to the next job/feature/story and sometimes it's easier to tell when that is than others.  In the past I've spent far too long trying to get things slightly better where it wouldn't make much difference to the value to code was offering, it was only for myself.  If there's time spare, this is a nice self indulgence but often we could have moved on to something else.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Be bold and break things
&lt;/h3&gt;

&lt;p&gt;One way I learn is by breaking something and then having to fix it again.  The sign-in flow, the schema migration, loads of stuff has been borked and deborked over the years.  Hopefully, most breakages are caught by tests but those that aren't still teach you something about your test approach or the way code interacts with other code, and really, as long as they're all broken and fixed again in a local/dev environment; who cares?  I'm not advocating for being a bit fast and loose with code and deployments, but if we're too scared about breaking things then we might not want to take a chance on a new library or pattern.&lt;br&gt;
I remember getting lambasted in my first job when I did my first dev build and broke it three times in a row; all I learned there was to not offer to do the build again.&lt;br&gt;
I still break builds from time to time, I used to care and now I don't.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Reading is good, but doing is better
&lt;/h3&gt;

&lt;p&gt;I'm a serial course taker, blog and dev.to reader.  I love reading about new patterns and tools I could use in my day to day.  The bit I used to get stuck at was actually trying them out in projects, mostly because I wasn't sure the coding style would be cohesive or if it was really worth adding in &lt;em&gt;another&lt;/em&gt; library dependency.  But I can't overstate the value from just giving things a go and seeing how they work in your code context.&lt;br&gt;
Try things out, see where the seams are and if it doesn't work out, the best thing about code is that you can rewrite it.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. To hell with hierarchy
&lt;/h3&gt;

&lt;p&gt;Larger companies tend to put more emphasis on team hierarchy than smaller ones, and a lot of the titles are sort of meaningless or at the very least only give a vague impression of differentiation.  Senior engineer here might equate to an intermediate over there, for example.  But increasingly, I really believe the titles are less important than having a variety of perspectives and experiences and a richness of knowledge follows from that.&lt;br&gt;
Junior, intermediate, senior - who cares? Simply make friends with people you want to learn from&lt;/p&gt;

&lt;h3&gt;
  
  
  8. Assert(true).is(true); fake it till you make it
&lt;/h3&gt;

&lt;p&gt;No one knows it all; we're just doing the best with what we know now and filling in the gaps, even the lead engineer who built the whole platform on their own over the long weekend.  I'm not entirely convinced that the imposter syndrome a lot of us feel ever completely goes away, so I've certainly always felt like I'm faking it all to some degree but over the years you'll realise that the bits you fake get fewer and the bits you know multiply.  So give yourself some space; playing pretend is fun, anyway!&lt;/p&gt;

&lt;h3&gt;
  
  
  9. Balance complexity with simplicity
&lt;/h3&gt;

&lt;p&gt;I love fancy code, who doesn't like feeling a bit clever when they've written something smart looking?  Would I still understand it if I came back a month later?  If not, then it probably needs a rewrite, or I'll get annoyed at myself for writing something so incomprehensible.  And my peers might not be best pleased either.&lt;br&gt;
But everything is a bit complicated the first time you see it, don't let the desire for simplicity stop you from learning a helpful new abstraction that you might have to then teach others.  Code simplicity doesn't mean reducing everything to if statements and for loops just because they're the language primitives.&lt;/p&gt;

&lt;h3&gt;
  
  
  10. Don't believe the marketecture
&lt;/h3&gt;

&lt;p&gt;There's always going to be a time when you need to use a commercial tool or two and unavoidably you'll have to pick one amongst their competitors.  Marketing material is easier to get to than technical documentation, especially for closed-source products.  Invariably it will tell you a shiny and wonderful tale of how this particular product will solve all of your woes, is absolutely config free and has frictionless upgrades.  Sounds great, but does it all stack up in reality?&lt;br&gt;
Off-the-shelf tools can be great and can be the only practical solution to what you're working on, but do read around first.  Community reviews or support boards are a good way to get a feel for the tool and how people end up using it to meet their needs and can offer invaluably candid opinions on things.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
