<?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: Ski</title>
    <description>The latest articles on DEV Community by Ski (@skyjur).</description>
    <link>https://dev.to/skyjur</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%2F129838%2Fd8e5f01a-debd-4757-9f41-ef7d763d7055.jpeg</url>
      <title>DEV Community: Ski</title>
      <link>https://dev.to/skyjur</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/skyjur"/>
    <language>en</language>
    <item>
      <title>Your code probably does not need TypeScript's Omit&lt;&gt;</title>
      <dc:creator>Ski</dc:creator>
      <pubDate>Tue, 21 Feb 2023 00:06:49 +0000</pubDate>
      <link>https://dev.to/skyjur/your-code-probably-does-not-need-typescripts-omit-55bd</link>
      <guid>https://dev.to/skyjur/your-code-probably-does-not-need-typescripts-omit-55bd</guid>
      <description>&lt;p&gt;What are you optimising your code for? Would it be readability or rather least amount of meaningful words used? If trying to optimize code for later then &lt;code&gt;Omit&amp;lt;&amp;gt;&lt;/code&gt; might be the tool for the job. If however readability is is a concern then &lt;code&gt;Omit&amp;lt;&amp;gt;&lt;/code&gt; might be making your code worse.&lt;/p&gt;

&lt;p&gt;Take this example, a seemingly straight forward case for applying &lt;code&gt;Omit&amp;lt;&amp;gt;&lt;/code&gt;&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;type&lt;/span&gt; &lt;span class="nx"&gt;HigherLevelProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Omit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;LowerLevelProps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;something&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;HigherLevelCode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;HigherLevelProps&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="p"&gt;...&lt;/span&gt;

   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;LowerLevelCode&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
     &lt;span class="na"&gt;something&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;
   &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When I am reading this code trying to understand how it works or what it needs to work, what would help me my understanding? Knowing what properties it needs - would be very helpful. Knowing what properties it doesn't need? Not really helpful at all. I cannot understand this code unless I dig deeper into &lt;code&gt;LowerLevelCode&lt;/code&gt; - digging deeper will occupy more of my short term memory and/or will require me to manage more vertical code split windows before I can begin to understanding what the code is doing and how I could use it.&lt;/p&gt;

&lt;p&gt;Let's check what &lt;a href="http://principles-wiki.net/principles:law_of_demeter"&gt;Law of Demeter&lt;/a&gt; tells us. Principle talks about building code in a way that it can be successfully used and understood with least amount of knowledge. If I would try to apply it to my example above - I should be able to use &lt;code&gt;HigherLevelCode&lt;/code&gt; without having to learn a lot about the &lt;code&gt;LowerLevelCode&lt;/code&gt;. Yet applying &lt;code&gt;Omit&amp;lt;&amp;gt;&lt;/code&gt; and spread operator (&lt;code&gt;...props&lt;/code&gt;) achieves the exact opposite: my code needs to be coupled not just with &lt;code&gt;HigherLevelCode&lt;/code&gt; but also with &lt;code&gt;LowerLevelCode&lt;/code&gt; thus if I ever need to refactor &lt;code&gt;LowerLevelCode&lt;/code&gt; it will not be enough to only change &lt;code&gt;HigherLevelCode&lt;/code&gt; but also all the usage of it.&lt;/p&gt;

&lt;p&gt;Let's try to improve the example to fix the problems we talked about:&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;type&lt;/span&gt; &lt;span class="nx"&gt;HigherLevelProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="na"&gt;propA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;...,&lt;/span&gt;
   &lt;span class="na"&gt;propB&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;HigherLevelCode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;HigherLevelProps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;propA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;propB&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;
   &lt;span class="p"&gt;...&lt;/span&gt;

   &lt;span class="nx"&gt;LowerLevelCode&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;something&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;propA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;propB&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It may not be the shortest possible way to write code. But it results in code that I can read and understand faster as I no longer have to jump into implementation of &lt;code&gt;LowerLevelCode&lt;/code&gt; to learn about necessary properties. It also makes refactoring easier to do in smaller iterative chunks thanks to code being less coupled (code that uses HighLevelCode now knows nothing about LowLevelCode and so each piece can be easily changed independently in smaller pull requests).&lt;/p&gt;

&lt;p&gt;This may look like going against 'DRY' (Do not repeat yourself) concept creating repetition. Yet this is not necessary a situation where some amount of repetition is bad. In a situations where it is hard to maintain consistency between the repeated concepts for example if implementation of an algorithm is repeated in a few places - that's a big problem. However repeating attribute names is not necessary a problem: it helps to communicate meaning as opposed to stuffing type algebra that may be meaningful to TypeScript but fora human it requires additional mental power to process it and to understand it.&lt;/p&gt;

&lt;p&gt;Surely if TypeScript added &lt;code&gt;Omit&amp;lt;&amp;gt;&lt;/code&gt; it must be useful somewhere? Yes in some situations it is useful or even necessary. Typically that would be framework or library level code - code that is designed to be very generic. For example, if I want to implement a function that allows to bind a single property key of a function input parameter, &lt;code&gt;Omit&amp;lt;&amp;gt;&lt;/code&gt; can be helpful to create type declaration for such function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * Usage example:
 *   const xy = ({x, y}) =&amp;gt; { ... } 
 *   const y = bindProp(xy, 'x', 0)
 */&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bindProp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Props&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ReturnType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Key&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;Props&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Props&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;ReturnType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Props&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Key&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="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Omit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Props&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Key&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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;Return&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&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;f&lt;/span&gt;&lt;span class="p"&gt;({...&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;notice how complicated TypeScript type declaration is, compared to the simple javascript function? When implementing libraries or frameworks and the code can simplify business logic in application's code it may be worthwhile to pay the price of complexity implementing very generic types.&lt;br&gt;
If however code is in application level as opposed to a generic library - aiming for simplicity and avoiding cleverness is usually better bet. We can assume that library's code will seldom need to change and we can assume that if it needs to change it would be done by specialists who know TypeScript down to every details. On other hand when writing high level application code we should assume that it may be written and read by developers who are either less experienced (entry level roles) or might be coming from different background (a backend engineer trying to fix a simple UI bug).&lt;/p&gt;

</description>
      <category>typescript</category>
    </item>
    <item>
      <title>I wish I had a lib that...</title>
      <dc:creator>Ski</dc:creator>
      <pubDate>Sat, 18 Feb 2023 17:38:52 +0000</pubDate>
      <link>https://dev.to/skyjur/i-wish-i-had-a-lib-that-p19</link>
      <guid>https://dev.to/skyjur/i-wish-i-had-a-lib-that-p19</guid>
      <description>&lt;p&gt;What kind of api/library you wish that you had that would make your work easier?&lt;/p&gt;

</description>
      <category>api</category>
      <category>library</category>
      <category>programming</category>
      <category>tools</category>
    </item>
    <item>
      <title>Writing fashionable functional code</title>
      <dc:creator>Ski</dc:creator>
      <pubDate>Fri, 18 Feb 2022 16:15:38 +0000</pubDate>
      <link>https://dev.to/skyjur/short-bits-writing-fashionable-functional-code-50eo</link>
      <guid>https://dev.to/skyjur/short-bits-writing-fashionable-functional-code-50eo</guid>
      <description>&lt;p&gt;Old and boring code:&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="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Authenticated&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;isAuthenticated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
  &lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Anonymous&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;isAuthenticated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Authenticated&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Anonymous&lt;/span&gt; 

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&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;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isAuthenticated&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I don't talk to strangers, bye&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
   &lt;span class="p"&gt;}&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 functional, it's not bad, but it lacks in style. We need more swag! Monads are all the rage nowadays. Functional code without monads ain't no functional code. Let's throw in some to make code look more hip&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="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Authenticated&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nl"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
   &lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Anonymous&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;someone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Either&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Authenticated&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Anonymous&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;someone&lt;/span&gt;
       &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mapLeft&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; 
         &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;
               &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getOrElse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
       &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;orElse&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I don't talk to stranges, bye&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How much better is this? Infinitely better!&lt;/p&gt;

&lt;p&gt;And did you also noticed, we didn't just add swag, we removed the unnecessary boolean isAuthorized! We can identify users from anonymous by their political leaning left or right*. Less business specific jargon, more generalizations, better code!&lt;/p&gt;

&lt;p&gt;Which style do you prefer more? Leave comment.&lt;/p&gt;

&lt;p&gt;*Monad inspired APIs, for example fp-ts, frequently refer to one of two possible types of values either as 'left' or 'right'&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Classes are still useful even in largely functional style code</title>
      <dc:creator>Ski</dc:creator>
      <pubDate>Tue, 15 Feb 2022 12:14:36 +0000</pubDate>
      <link>https://dev.to/skyjur/classes-are-still-useful-even-in-largely-functional-style-code-1ng4</link>
      <guid>https://dev.to/skyjur/classes-are-still-useful-even-in-largely-functional-style-code-1ng4</guid>
      <description>&lt;p&gt;It’s became a fashion to rid code of classes even when it doesn’t help produce better code. The popular web UI framework React is likely a culprit — so many developers are familiar with React and so any trends started by React developers inevitably spread even if outside React same concepts don’t work at all.&lt;/p&gt;

&lt;p&gt;While classes are mostly associated with object oriented programming they can also be just as useful tool in functional programming as they are in OOP. Functional programming while containing useful ideas and goals badly lacks down to earth vocabulary.&lt;/p&gt;

&lt;p&gt;The use case that classes solve is same that of “Higher-order functions”. This functional term is a bit broad — it’s good to describe a general approach but not good to name the individual bits in code. It’s not immediately clear if higher-order function refers to function that returns a function, or a function that takes a function as a parameter or a function that has been returned by a function and in this sentence the word function was used so many times that it should be pretty clear that we need better vocabulary.&lt;/p&gt;

&lt;p&gt;Classes provide more structure and very useful vocabulary:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Class&lt;/li&gt;
&lt;li&gt;Constructor&lt;/li&gt;
&lt;li&gt;Instance&lt;/li&gt;
&lt;li&gt;Property&lt;/li&gt;
&lt;li&gt;Method&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now it’s easy to identify what is what — instead of “function that returns a function” — we got a “class" and "constructor”. Instead of “function that has been created inside a function” — we have a method. And it has became explicit that methods don’t exist without instances, just like returned functions don’t exist without hidden closures attached to them.&lt;/p&gt;

&lt;p&gt;What also very helpful is that it's more natural to name classes using nouns. This makes code look more like a human language - we typically don't use verbs to describe gifts that we get on birthdays. Meanwhile typically verbs are frequently used to name functions/methods thus it often becomes awkward to give a name to variables that hold on to a function that's bound to closure. On one hand it makes sense that function is verb on other hand when function is passed around like a gift it's awkward if it's name is a verb.&lt;/p&gt;

&lt;p&gt;What classes help to achieve — a code written in such a manner that all functions are always verbs, and all things that can be passed around are always nouns. It helps to achieve consistency. It helps to talk about code. And yes it’s sometimes takes more time to give meaningful naming — but this is a good problem to have — after it’s solved it creates a code that is more easy to understand and has more clearly defined structural shape.&lt;/p&gt;

&lt;p&gt;It doesn’t matter if you’re fan of functional programming or not — if your programming language has classes — use them instead of just plain functions — it’s good for your code.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>functional</category>
      <category>react</category>
    </item>
  </channel>
</rss>
