<?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>Forem: Robert Bornschein</title>
    <description>The latest articles on Forem by Robert Bornschein (@rbrtbrnschn).</description>
    <link>https://forem.com/rbrtbrnschn</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%2F531945%2Fd286534f-304d-465c-b00d-de098afad762.png</url>
      <title>Forem: Robert Bornschein</title>
      <link>https://forem.com/rbrtbrnschn</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rbrtbrnschn"/>
    <language>en</language>
    <item>
      <title>How to get rid of switch statements</title>
      <dc:creator>Robert Bornschein</dc:creator>
      <pubDate>Sat, 06 Feb 2021 07:06:36 +0000</pubDate>
      <link>https://forem.com/rbrtbrnschn/how-to-get-rid-of-switch-statements-3oj1</link>
      <guid>https://forem.com/rbrtbrnschn/how-to-get-rid-of-switch-statements-3oj1</guid>
      <description>&lt;p&gt;Photo by Tim Mossholder from Pexels&lt;/p&gt;

&lt;h2&gt;
  
  
  Why get rid of switch statements, to begin with?
&lt;/h2&gt;

&lt;p&gt;The answer is simple. Having switch statements is fine, but it could be better. &lt;br&gt;
In the world of code, you will find something called the &lt;strong&gt;SOLID&lt;/strong&gt; principles, which help you in writing clearer, cleaner, better code.&lt;/p&gt;

&lt;h2&gt;
  
  
  The switch statment
&lt;/h2&gt;

&lt;p&gt;Imagine having to handle a "keydown" event. You want to handle each valid keycode accordingly. This is what you'd find most often.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fswicmzigckbqtkxu99ym.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fswicmzigckbqtkxu99ym.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Refactoring
&lt;/h2&gt;

&lt;p&gt;How about this, let's create a class for the key press.&lt;br&gt;
We need the keycode, we need the action it's supposed to take and an added bonus of possibly extra validation.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fl9aaxflh1dhn0pp9m8er.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fl9aaxflh1dhn0pp9m8er.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Now what?
&lt;/h3&gt;

&lt;p&gt;Now we instantiate a new Object from our KeyPress class for each keycode you'd need.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvrwyaitlvglnjel829az.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvrwyaitlvglnjel829az.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  And now the fun part!
&lt;/h2&gt;

&lt;p&gt;We still need something to manage when and how each different keypress object is being called. This is where the handler comes into play. It'll need a bind method to add new KeysPress objects and a handle method, which is self-explanatory.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F6w81q244nyniq7cklden.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F6w81q244nyniq7cklden.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The moment you have all been waiting for!
&lt;/h2&gt;

&lt;p&gt;That same function with the previous switch statement.&lt;br&gt;
It's tiny. If you don't need any prior validation, it's basically a one-liner.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F10niht4b7r5rdwov0610.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F10niht4b7r5rdwov0610.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  A few last words
&lt;/h2&gt;

&lt;p&gt;Now why would you want to do this? For the sake of maintainability, decreased complexity.&lt;br&gt;
You have essentially &lt;strong&gt;closed&lt;/strong&gt; the handleKeyDown function for modificaiton. But it's still &lt;strong&gt;open&lt;/strong&gt; for extension.&lt;/p&gt;

&lt;p&gt;You can simply add new KeyPress Objects, add them to the list in the keyhandler and voila. &lt;br&gt;
Your function still stays the same! That's crazy.&lt;/p&gt;

&lt;p&gt;Next time you implement a switch statement like this, simply think of this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Why not try something new this time? &lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>solid</category>
      <category>polymorphism</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Discord Bot - @Mr. Taskman</title>
      <dc:creator>Robert Bornschein</dc:creator>
      <pubDate>Fri, 04 Dec 2020 11:39:31 +0000</pubDate>
      <link>https://forem.com/rbrtbrnschn/discord-bot-mr-taskman-161e</link>
      <guid>https://forem.com/rbrtbrnschn/discord-bot-mr-taskman-161e</guid>
      <description>&lt;p&gt;I do not see how the tag of "help" is valid here, but here goes.&lt;/p&gt;

&lt;p&gt;Currently working on an &lt;strong&gt;open source project&lt;/strong&gt; with &lt;em&gt;2 others&lt;/em&gt;. We are developing &lt;strong&gt;a discord bot&lt;/strong&gt;, that could if all goes well, &lt;strong&gt;render&lt;/strong&gt; other "third party" project management tools like &lt;em&gt;trello, jira, kanban&lt;/em&gt;, and the like &lt;strong&gt;obsolete&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This project is being written in &lt;strong&gt;Typescript&lt;/strong&gt;, a superset of &lt;em&gt;javascript&lt;/em&gt;.&lt;br&gt;
Current structure geared towards "&lt;strong&gt;SOA&lt;/strong&gt;" - service oriented architecture.&lt;/p&gt;

&lt;p&gt;If anyone is interested, feel free to contact me or hit me up on discord:&lt;br&gt;
&lt;a href="https://discord.gg/qhHjuqgJ6Y"&gt;https://discord.gg/qhHjuqgJ6Y&lt;/a&gt;.&lt;br&gt;
Repo: &lt;a href="https://github.com/rbrtbrnschn/mr-taskman"&gt;https://github.com/rbrtbrnschn/mr-taskman&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Disclaimer:&lt;br&gt;
Knowledge of typescript isn't a necessity, it's easier to learn than you think.&lt;br&gt;
I do not know if posting this link is allowed, nor if I used the &lt;em&gt;help&lt;/em&gt; tag properly.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Discord Bot @Mr.Taskman</title>
      <dc:creator>Robert Bornschein</dc:creator>
      <pubDate>Fri, 04 Dec 2020 11:02:49 +0000</pubDate>
      <link>https://forem.com/rbrtbrnschn/discord-bot-mr-taskman-17n5</link>
      <guid>https://forem.com/rbrtbrnschn/discord-bot-mr-taskman-17n5</guid>
      <description>&lt;p&gt;I do not see how the tag of "help" is valid here, but here goes.&lt;/p&gt;

&lt;p&gt;Currently working on an &lt;strong&gt;open source project&lt;/strong&gt; with &lt;em&gt;2 others&lt;/em&gt;. We are developing &lt;strong&gt;a discord bot&lt;/strong&gt;, that could if all goes well, &lt;strong&gt;render&lt;/strong&gt; other "third party" project management tools like &lt;em&gt;trello, jira, kanban&lt;/em&gt;, and the like &lt;strong&gt;obsolete&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This project is being written in &lt;strong&gt;Typescript&lt;/strong&gt;, a superset of &lt;em&gt;javascript&lt;/em&gt;.&lt;br&gt;
Current structure geared towards "&lt;strong&gt;SOA&lt;/strong&gt;" - service oriented architecture.&lt;/p&gt;

&lt;p&gt;If anyone is interested, feel free to contact me or hit me up on discord:&lt;br&gt;
&lt;a href="https://discord.gg/qhHjuqgJ6Y"&gt;https://discord.gg/qhHjuqgJ6Y&lt;/a&gt;.&lt;br&gt;
Repo: &lt;a href="https://github.com/rbrtbrnschn/mr-taskman"&gt;https://github.com/rbrtbrnschn/mr-taskman&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Disclaimer:&lt;br&gt;
Knowledge of typescript isn't a necessity, it's easier to learn than you think.&lt;br&gt;
I do not know if posting this link is allowed, nor if I used the &lt;em&gt;help&lt;/em&gt; tag properly.&lt;/p&gt;

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