<?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: Lucas Fonseca Mundim</title>
    <description>The latest articles on Forem by Lucas Fonseca Mundim (@mundim).</description>
    <link>https://forem.com/mundim</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%2F674334%2Fc6ea01f2-4c79-4690-a26c-77cd62b6ced9.jpeg</url>
      <title>Forem: Lucas Fonseca Mundim</title>
      <link>https://forem.com/mundim</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mundim"/>
    <language>en</language>
    <item>
      <title>Regex 101 - Kill the Monster</title>
      <dc:creator>Lucas Fonseca Mundim</dc:creator>
      <pubDate>Thu, 02 Sep 2021 14:14:36 +0000</pubDate>
      <link>https://forem.com/mundim/regex-101-kill-the-monster-2n9n</link>
      <guid>https://forem.com/mundim/regex-101-kill-the-monster-2n9n</guid>
      <description>&lt;p&gt;More frequently than not I see people recommending &lt;code&gt;RegEx&lt;/code&gt; (or &lt;code&gt;RegExp&lt;/code&gt;) to other people to help solve a problem and the reaction being the same: they don't want to use &lt;code&gt;RegEx&lt;/code&gt; because they don't understand or find it very confusing. I myself find that weird, because I never thought of &lt;code&gt;RegEx&lt;/code&gt; to be that awful, at least not on reasonable boundaries. So today I'll try to explain the basis of &lt;code&gt;RegEx&lt;/code&gt; to try to make it less of a monster to you guys.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Disclaimer:&lt;/strong&gt; depending on the programming language you are using the syntax may vary slightly. For reference, I will be using the &lt;code&gt;JS&lt;/code&gt;/&lt;code&gt;C#&lt;/code&gt; syntax. You can try it out on &lt;a href="//www.regex101.com"&gt;Regex101&lt;/a&gt;&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%2Fuploads%2Farticles%2Fpu67dmelk36rrnokk2q5.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%2Fuploads%2Farticles%2Fpu67dmelk36rrnokk2q5.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The basics
&lt;/h2&gt;

&lt;p&gt;I really would like to split this into more topics but they wouldn't make very much sense on their own, so I'll call them as "the basics". This will include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Letters and Numbers&lt;/li&gt;
&lt;li&gt;Basic Symbols&lt;/li&gt;
&lt;li&gt;Groups and Ranges&lt;/li&gt;
&lt;li&gt;Counters&lt;/li&gt;
&lt;li&gt;Tokens&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Letters and Numbers
&lt;/h3&gt;

&lt;p&gt;First of all, let's talk about letters and numbers. They work very much as you would expect: if you write &lt;code&gt;a&lt;/code&gt;, the regex will expect the letter &lt;code&gt;a&lt;/code&gt;, lower case, and so forth. There's really not much to explain here.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic Symbols
&lt;/h3&gt;

&lt;p&gt;Symbols on the other hand &lt;em&gt;can&lt;/em&gt; be a little bit confusing. Some symbols are &lt;strong&gt;reserved&lt;/strong&gt; by RegEx to do some &lt;em&gt;special&lt;/em&gt; stuff. They all have the possibility of escaping by using a &lt;code&gt;backslash \&lt;/code&gt;, which brings us to the first special symbol:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;\&lt;/code&gt;: escapes any character that would be a special one to mean literally that character&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;()&lt;/code&gt;: group delimiter, we'll dive in deeper on that later&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[]&lt;/code&gt;: range delimiter, we'll dive in deeper on that later&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{}&lt;/code&gt;: counter delimiter, we'll dive in deeper on that later&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;^&lt;/code&gt;: when outside delimiters, it means the start of a string. When within delimiters, it means &lt;code&gt;not&lt;/code&gt; (the same as the good old &lt;code&gt;!&lt;/code&gt; on programming)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$&lt;/code&gt;: end of a string&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.&lt;/code&gt;: anything. The &lt;code&gt;.&lt;/code&gt; means that the character there can be absolutely any &lt;em&gt;single&lt;/em&gt; character. Also known as wildcard&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;|&lt;/code&gt;: our good old boolean operator &lt;code&gt;or&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Groups and Ranges
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Groups&lt;/strong&gt;, delimited by &lt;code&gt;()&lt;/code&gt;, have more or less the same idea as the symbols in maths or any programming: they group operations together to make something valid for the entire group (e.g. a counter)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ranges&lt;/strong&gt;, delimited by &lt;code&gt;[]&lt;/code&gt;, are a little bit more complex, but not so much. They mean that any character within its range is valid. Note that it can be mixed and matched, and even improved:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[abc]&lt;/code&gt; means any character from &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt; or &lt;code&gt;c&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[^abc]&lt;/code&gt; means any character &lt;strong&gt;except&lt;/strong&gt; &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt; or &lt;code&gt;c&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[a-z]&lt;/code&gt; means any character from &lt;code&gt;a&lt;/code&gt; to &lt;code&gt;z&lt;/code&gt;, in the alphabetical order (so &lt;code&gt;[a-c]&lt;/code&gt; would be the same as &lt;code&gt;[abc]&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[a-zA-Z]&lt;/code&gt; means the same as the above, but &lt;strong&gt;case insensitive&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[0-9]&lt;/code&gt; means any digit&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Counters
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Counters&lt;/strong&gt; make it easier to delimit &lt;em&gt;how many&lt;/em&gt; from a given character (or rule) you expect.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;*&lt;/code&gt; means &lt;em&gt;any&lt;/em&gt; number, or from &lt;code&gt;0&lt;/code&gt; to ∞, also known as &lt;strong&gt;zero or more&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;+&lt;/code&gt; means from &lt;code&gt;1&lt;/code&gt; to ∞, aka &lt;strong&gt;one or more&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;?&lt;/code&gt; means from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;1&lt;/code&gt;, aka &lt;strong&gt;zero or one&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{3}&lt;/code&gt; means &lt;strong&gt;exactly&lt;/strong&gt; 3&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{3,}&lt;/code&gt; means &lt;strong&gt;3 or more&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{3,6}&lt;/code&gt; means &lt;strong&gt;from 3 to 6&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Tokens
&lt;/h3&gt;

&lt;p&gt;Just as we have &lt;code&gt;\n&lt;/code&gt; on programming as a token for &lt;code&gt;new line&lt;/code&gt;, &lt;code&gt;RegEx&lt;/code&gt; has its own tokens as well.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;\s&lt;/code&gt; means any &lt;em&gt;whitespace&lt;/em&gt; character (space, tab, new line)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\S&lt;/code&gt; means any &lt;em&gt;non-whitespace&lt;/em&gt; character&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\d&lt;/code&gt; means any &lt;em&gt;digit&lt;/em&gt;, the same as &lt;code&gt;[0-9]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\D&lt;/code&gt; means any &lt;em&gt;non-digit&lt;/em&gt;, the same as &lt;code&gt;[^0-9]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\w&lt;/code&gt; means any &lt;em&gt;word&lt;/em&gt;, or any letter, digit or underscore&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\W&lt;/code&gt; means any &lt;em&gt;non-word&lt;/em&gt;, or anything besides letters, digits or underscores&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\b&lt;/code&gt; means &lt;em&gt;word boundary&lt;/em&gt;, or the character immediately matched by &lt;code&gt;\w&lt;/code&gt; and a character not matched by &lt;code&gt;\w&lt;/code&gt;, in either order&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Join all that together and...
&lt;/h3&gt;

&lt;p&gt;By joining all those definitions, we can start writing &lt;code&gt;RegEx&lt;/code&gt;es. Let's see some samples&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Match a 🇧🇷BR ZIP Code: Brazilian ZIP Codes are 5 digits, followed by a dash, followed by 3 more digits. Or, in &lt;code&gt;RegEx&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[0-9]{5}-[0-9]{3}&lt;/code&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%2Fuploads%2Farticles%2Ffxedrdr6kupc729je1oy.png" alt="image"&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\d{5}-\d{3}&lt;/code&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%2Fuploads%2Farticles%2Fba1j0b1izxhwa95rw3x8.png" alt="image"&gt;
&lt;/li&gt;
&lt;li&gt;Some people might not type in the &lt;code&gt;-&lt;/code&gt;: &lt;code&gt;\d{8}&lt;/code&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%2Fuploads%2Farticles%2Feyzdno48ldi5vmqhmgp4.png" alt="image"&gt;
&lt;/li&gt;
&lt;li&gt;Furthermore:
&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%2Fuploads%2Farticles%2Fkvo0lkvdswxx62arwgrg.png" alt="image"&gt; &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Match a &lt;code&gt;DD/MM/YYYY&lt;/code&gt; or &lt;code&gt;DD/MM/YY&lt;/code&gt; date: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;\d{2}/\d{2}/(\d{4}|\d{2})&lt;/code&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%2Fuploads%2Farticles%2Fqm9m1wyjcc7t7ei92php.png" alt="image"&gt;
&lt;/li&gt;
&lt;li&gt;Note that &lt;code&gt;|&lt;/code&gt; evaluation is &lt;strong&gt;lazy&lt;/strong&gt; &lt;code&gt;\d{2}/\d{2}/(\d{4}|\d{2})&lt;/code&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%2Fuploads%2Farticles%2Fc93zbjlh0r3lfd5xvw76.png" alt="image"&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&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%2Fuploads%2Farticles%2F2oiaxtcxf5pz3h7vbtxm.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%2Fuploads%2Farticles%2F2oiaxtcxf5pz3h7vbtxm.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Naming groups
&lt;/h2&gt;

&lt;p&gt;Naming groups should be available on most programming languages, but how it works may vary. It is very useful for readability purposes and should always be used in production environments or serious work &lt;em&gt;should &lt;code&gt;RegEx&lt;/code&gt; make it that far&lt;/em&gt;. The symbol for grouping is &lt;code&gt;?&amp;lt;&amp;gt;&lt;/code&gt; (or &lt;code&gt;?P&amp;lt;&amp;gt;&lt;/code&gt; for &lt;code&gt;Python&lt;/code&gt; in the example).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;((?P&amp;lt;ZIPCode&amp;gt;\d{5}-?\d{3})|(?P&amp;lt;Date&amp;gt;\d{2}\/\d{2}\/(\d{4}|\d{2})))&lt;/code&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%2Fuploads%2Farticles%2F6mxwc39p4d3f6umd9c83.png" alt="image"&gt;

&lt;ul&gt;
&lt;li&gt;Yes, in &lt;code&gt;Python&lt;/code&gt; it is very ugly, but it is language dependent. &lt;a href="https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.getgroupnames?view=net-5.0" rel="noopener noreferrer"&gt;In &lt;code&gt;C#&lt;/code&gt; it is &lt;em&gt;much&lt;/em&gt; better&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&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%2Fuploads%2Farticles%2F2jeup6rnunwaixlrxov9.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%2Fuploads%2Farticles%2F2jeup6rnunwaixlrxov9.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrap up
&lt;/h2&gt;

&lt;p&gt;This article was aimed to just "kill the monster" that people consider Regular Expressions to be, and show that it is not &lt;em&gt;that&lt;/em&gt; scary for simple work. Of course it gets harder and harder the more complex your matching needs are (e.g. find an email), but there &lt;em&gt;usually&lt;/em&gt; are better ways of doing complex tasks.&lt;/p&gt;

&lt;p&gt;If you want or need to dive deeper into Regular Expressions, consider studying the theory behind it (from &lt;a href="https://en.wikipedia.org/wiki/Formal_language" rel="noopener noreferrer"&gt;Formal Languages&lt;/a&gt; and read/play around &lt;a href="//www.regex101.com"&gt;Regex101&lt;/a&gt;, but beware: it gets really deep, but it's very interesting!&lt;/p&gt;

</description>
      <category>regex</category>
      <category>programming</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Writing your F.I.R.S.T. Unit Tests</title>
      <dc:creator>Lucas Fonseca Mundim</dc:creator>
      <pubDate>Fri, 27 Aug 2021 11:53:43 +0000</pubDate>
      <link>https://forem.com/mundim/writing-your-f-i-r-s-t-unit-tests-1iop</link>
      <guid>https://forem.com/mundim/writing-your-f-i-r-s-t-unit-tests-1iop</guid>
      <description>&lt;h2&gt;
  
  
  What are Unit Tests?
&lt;/h2&gt;

&lt;p&gt;By design, &lt;strong&gt;Unit Tests&lt;/strong&gt; are automated tests of &lt;em&gt;small&lt;/em&gt; units of code that are tested in an &lt;strong&gt;isolated&lt;/strong&gt; fashion. Essentially, an unit test is a program that calls the (&lt;code&gt;public&lt;/code&gt;) methods of a class and checks if the results are as expected&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%2Fuploads%2Farticles%2Fhfgkf5icaiwuczgiwvb7.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%2Fuploads%2Farticles%2Fhfgkf5icaiwuczgiwvb7.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits to Unit Testing
&lt;/h2&gt;

&lt;p&gt;If you wrote your unit tests correctly, there are several benefits they will bring to your maintenance process:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Unit Tests will find bugs while you're still developing:&lt;/strong&gt; if you are refactoring a section of code, or even including a new one, and your past tests start to &lt;em&gt;fail&lt;/em&gt;, it means that you changed the behaviour of that method and that might imply on a bug!&lt;/li&gt;
&lt;li&gt;Any correction cost regarding the newly found bugs is considerably &lt;strong&gt;lower&lt;/strong&gt; than if they were found while in production&lt;/li&gt;
&lt;li&gt;In other words, Unit Tests protect your code against &lt;strong&gt;Regression&lt;/strong&gt;: system changes that create bugs&lt;/li&gt;
&lt;li&gt;After changing your code, you &lt;strong&gt;must&lt;/strong&gt; run the test suite! That way you'll find if there was any regression (if any test fails)&lt;/li&gt;
&lt;/ul&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%2Fuploads%2Farticles%2Fvbl8bsv1v70dp35ee2jx.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%2Fuploads%2Farticles%2Fvbl8bsv1v70dp35ee2jx.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  F.I.R.S.T. Principles
&lt;/h2&gt;

&lt;p&gt;Just as we have our S.O.L.I.D. principles for programming in general, there are a couple of principles for general good practice in unit testing: the &lt;strong&gt;F.I.R.S.T.&lt;/strong&gt; principles. Let's go over them.&lt;/p&gt;

&lt;h3&gt;
  
  
  [F]ast
&lt;/h3&gt;

&lt;p&gt;The developer &lt;strong&gt;should not&lt;/strong&gt; hesitate on running the test suite at any point of the development cycle, even if there are thousands of unit tests. They should run in a matter of seconds. If a test takes too long to run, it probably is doing more than it should — and, therefore, is not an &lt;strong&gt;unit&lt;/strong&gt; test!&lt;/p&gt;

&lt;h3&gt;
  
  
  [I]ndependent/[I]solated
&lt;/h3&gt;

&lt;p&gt;For any given unit test, it should be &lt;strong&gt;independent&lt;/strong&gt; of everything else so that its results are &lt;strong&gt;not&lt;/strong&gt; influenced by any other factor. With that definition, they should usually follow the “3 A’s of testing”: &lt;strong&gt;Arrange, Act, Assert&lt;/strong&gt; (also known as “Given-When-Then”).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Arrange:&lt;/strong&gt; All needed data should be provided to the test when you are about to run, and it should &lt;strong&gt;not&lt;/strong&gt; depend on your environment.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Act:&lt;/strong&gt; The actual method you are testing should be run.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Assert:&lt;/strong&gt; Unit Tests should only test &lt;strong&gt;one&lt;/strong&gt; outcome, meaning each test should assert that &lt;strong&gt;one&lt;/strong&gt; state of an object should be tested. Note that this does not mean that you should only check &lt;strong&gt;one&lt;/strong&gt; variable of a class response, but that &lt;strong&gt;all&lt;/strong&gt; variables tested should be related to the method you ran&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  [R]epeatable
&lt;/h3&gt;

&lt;p&gt;Tests should be repeatable and deterministic: every single time you run a test their values &lt;strong&gt;cannot&lt;/strong&gt; change, no matter the environment.&lt;/p&gt;

&lt;h3&gt;
  
  
  [S]elf-Validating
&lt;/h3&gt;

&lt;p&gt;The test itself should tell you if it passed. There should not be any need of manually checking the values. Most assertion libraries (such as &lt;code&gt;Shouldly&lt;/code&gt;) work in favour of that principle.&lt;/p&gt;

&lt;h3&gt;
  
  
  [T]horough
&lt;/h3&gt;

&lt;p&gt;Your test should cover all &lt;em&gt;“happy paths”&lt;/em&gt; of a method (&lt;code&gt;xUnit&lt;/code&gt; makes that possible with &lt;code&gt;Theory&lt;/code&gt; tests), all edge cases (where you think the test might fail), illegal arguments, security flaws, large values… in sum, every possible use case scenario should be tested, not only enough to have (near) 100% Code Coverage&lt;/p&gt;

&lt;h3&gt;
  
  
  The extra T: Timely
&lt;/h3&gt;

&lt;p&gt;Following &lt;a href="https://en.wikipedia.org/wiki/Test-driven_development" rel="noopener noreferrer"&gt;TDD&lt;/a&gt;, Unit Tests should be written before the production code that will be tested. This can be done by writing the abstraction (Interface) and then the test based on the method signature and spec alone. After the test is written, your code can be produced and tested.&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%2Fuploads%2Farticles%2Fb54ir52c6zm8rsf98zcc.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%2Fuploads%2Farticles%2Fb54ir52c6zm8rsf98zcc.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  A few tools
&lt;/h2&gt;

&lt;p&gt;For &lt;code&gt;C#&lt;/code&gt;/&lt;code&gt;.NET&lt;/code&gt; development, a few tools/frameworks are available by default, and a lot more available as &lt;strong&gt;NuGet packages&lt;/strong&gt;. Here are some &lt;strong&gt;highly recommended&lt;/strong&gt; options&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Base Framework: &lt;a href="https://github.com/xunit/xunit" rel="noopener noreferrer"&gt;&lt;code&gt;xUnit&lt;/code&gt;&lt;/a&gt; is a powerful and complete testing framework for .NET that is usually bundled with &lt;strong&gt;VisualStudio&lt;/strong&gt; alongside &lt;code&gt;NUnit&lt;/code&gt;. Overall both are good frameworks, but &lt;code&gt;xUnit&lt;/code&gt; is more readable and intuitive&lt;/li&gt;
&lt;li&gt;Assertion Tool: &lt;a href="https://github.com/shouldly/shouldly" rel="noopener noreferrer"&gt;&lt;code&gt;Shouldly&lt;/code&gt;&lt;/a&gt; is a much more intuitive way of asserting your test results than the native Assert class. The github repo gives plenty of examples but just one very simple sample should be enough to show how readable and simple &lt;code&gt;Shouldly&lt;/code&gt; is:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// using Assert&lt;/span&gt;
&lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;IsTrue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;booleanVariable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// using Shouldly&lt;/span&gt;
&lt;span class="n"&gt;booleanVariable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ShouldBeTrue&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Mocking Tool: &lt;a href="https://github.com/nsubstitute/NSubstitute" rel="noopener noreferrer"&gt;&lt;code&gt;NSubstitute&lt;/code&gt;&lt;/a&gt; is a powerful tool that allows you to create mocks for any given class or interface so that you can easily test any method that would, in a regular case, use external environment such as 3rd Party APIs or DataBases. It has a simple learning curve and is &lt;a href="https://nsubstitute.github.io" rel="noopener noreferrer"&gt;well documented&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;This article is a compilation made from different source materials cited below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;General “Software Maintenance and Evolution” and “Software Testing” theory&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dzone.com/articles/writing-your-first-unit-tests" rel="noopener noreferrer"&gt;https://dzone.com/articles/writing-your-first-unit-tests&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/ghsukumar/SFDC_Best_Practices/wiki/F.I.R.S.T-Principles-of-Unit-Testing" rel="noopener noreferrer"&gt;https://github.com/ghsukumar/SFDC_Best_Practices/wiki/F.I.R.S.T-Principles-of-Unit-Testing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/@tasdikrahman/f-i-r-s-t-principles-of-testing-1a497acda8d6" rel="noopener noreferrer"&gt;https://medium.com/@tasdikrahman/f-i-r-s-t-principles-of-testing-1a497acda8d6&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>unittests</category>
      <category>programming</category>
      <category>codequality</category>
      <category>testing</category>
    </item>
    <item>
      <title>Basic Productivity Software Setup</title>
      <dc:creator>Lucas Fonseca Mundim</dc:creator>
      <pubDate>Tue, 24 Aug 2021 19:35:15 +0000</pubDate>
      <link>https://forem.com/mundim/basic-mac-productivity-software-setup-nd6</link>
      <guid>https://forem.com/mundim/basic-mac-productivity-software-setup-nd6</guid>
      <description>&lt;p&gt;With the pandemic, the most fortunate of us were moved to working from home, and that often meant using our personal computers to do our jobs (often due to choice), but that meant installing "work stuff" into our machines. How can you keep stuff separated and get a more productive environment, while also maintaining stuff clean for your free time?&lt;/p&gt;

&lt;p&gt;Small disclaimer: some of these tips only apply to MacOS users, but most of them are perfectly compatible with any Unix environment 🐧&lt;/p&gt;




&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rG3yDzHJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hpdod5azqfcufybfalcf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rG3yDzHJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hpdod5azqfcufybfalcf.png" alt="image" width="800" height="457"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  1. Use different browsers 🕸
&lt;/h2&gt;

&lt;p&gt;Keeping a browser for your personal, free time and a separate one for your job is crucial: more often than not I find myself having dozens of tabs related to some code I'm working on open, and if I had to juggle that with my personal tabs it would be an immense memory hog, while also a terrible chore. Keeping a different browser just for your work eliminates that problem.&lt;/p&gt;

&lt;p&gt;You can even double down on that and use browsers that sync to make it possible to work on multiple machines! For example, I use Safari as my personal browser so it syncs with my iPhone and iPad, and to work I use Google Chrome, which in turn syncs up with my Windows' Chrome should I ever need to use it, or even my Ubuntu VM! It's always helpful 👹&lt;/p&gt;




&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OF_gjgv4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/02hkwf45tzs835vgjjsa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OF_gjgv4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/02hkwf45tzs835vgjjsa.png" alt="image" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Use a browser manager 🤵‍♀️🤵‍♂️
&lt;/h2&gt;

&lt;p&gt;Good, you've decided to keep a separate browser for each environment. But that means you're logged off of your, say, Atlassian account on your personal browser, which is set as your primary browser. Now you have to copy every Jira ticket or BitBucket Pull Request link sent on slack and paste it on your work browser. That works... but that's not that efficient is it?&lt;/p&gt;

&lt;p&gt;Some more eager people have already had this issue and decided to make it easier on themselves, and they were kind enough to share it with the world! Enter &lt;a href="https://github.com/johnste/finicky"&gt;Finicky&lt;/a&gt;, a free, open source, browser manager.&lt;/p&gt;

&lt;p&gt;What Finicky does in essence is really simple: it allows you to code rules for every URL you click to open up on a given browser. You can set URL's that contain a certain substring to open on Firefox, URL's from Slack on Chrome and everything else on Safari, for example! It is really handy, the &lt;a href="https://github.com/johnste/finicky#installation"&gt;installation&lt;/a&gt; is very straightforward using &lt;code&gt;brew&lt;/code&gt; and there are &lt;a href="https://github.com/johnste/finicky/wiki/Configuration"&gt;many possibilities&lt;/a&gt; to configure at your heart's content!&lt;/p&gt;

&lt;p&gt;Here's my &lt;em&gt;very&lt;/em&gt; small and simple configuration:&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="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;defaultBrowser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Safari&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Hide the finicky icon from the top bar&lt;/span&gt;
    &lt;span class="na"&gt;hideIcon&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="na"&gt;handlers&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="c1"&gt;// Open any link clicked in Slack in Chrome&lt;/span&gt;
      &lt;span class="na"&gt;match&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;opener&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;opener&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bundleId&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;com.tinyspeck.slackmacgap&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;browser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Google Chrome&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="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Should you want anything with a GUI, the folks at Finicky themselves recommend &lt;a href="https://github.com/will-stone/browserosaurus"&gt;Browserosaurus&lt;/a&gt; so go ahead and take a look!&lt;/p&gt;




&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tWyUvSji--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vhqblgqtcazyz617kvy2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tWyUvSji--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vhqblgqtcazyz617kvy2.png" alt="image" width="600" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Have a single messenger app for your personal stuff 💬
&lt;/h2&gt;

&lt;p&gt;Many of us use more than one messaging app, after all there are tons of them nowadays: Facebook Messenger, Telegram, WhatsApp, Discord... and having to juggle all that open along with the job's ones like Slack and Teams is a hassle.&lt;/p&gt;

&lt;p&gt;Some might already be familiar with &lt;a href="https://meetfranz.com"&gt;Franz&lt;/a&gt;, a very popular program that unifies all those messaging apps into a single one. Franz is a very capable and very practical solution and if you have an Intel Mac or any other &lt;code&gt;x86_64&lt;/code&gt; machine it should be plenty! But &lt;a href="https://github.com/meetfranz/franz/issues/1946"&gt;looks like it does not play well with M1 Macs yet&lt;/a&gt;. For that then I decided to look elsewhere and found it's "offspring" &lt;a href="https://getferdi.com"&gt;Ferdi&lt;/a&gt;, which has most (if not all) of it's features -- it is actually &lt;em&gt;based off of Franz&lt;/em&gt; -- and works very well on Apple Silicon, so be sure to give it a shot!&lt;/p&gt;




&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jL5_g6pE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7sd6y1bcmje2008cvtpr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jL5_g6pE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7sd6y1bcmje2008cvtpr.png" alt="image" width="800" height="476"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  4. Try out the new Focus mode on MacOS Monterey 🖥
&lt;/h2&gt;

&lt;p&gt;This is still on Beta at the time of writing the article, but the new Focus Mode introduced in WWDC2021 for &lt;a href="https://www.apple.com/macos/monterey-preview/"&gt;MacOS Monterey&lt;/a&gt; looks like a very handy tool to not only tell your pesky iMessage contacts (and maybe, in the future, any app that feeds off of it's API?) that you're focused working but also hides any notification you do not want in that profile -- and it also syncs up through your apple devices! Be sure to take a look!&lt;/p&gt;




&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sTu-oPl1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iq7m6xqggub938sz74j9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sTu-oPl1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iq7m6xqggub938sz74j9.png" alt="image" width="800" height="312"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Use a better Terminal emulator 💻
&lt;/h2&gt;

&lt;p&gt;Yes, MacOS' good ol' terminal gets the job done alright, but it only does an 'OK Job' at that, just like Ubuntu's base terminal emulator or WSL's standard emulator. But what if you want &lt;em&gt;more&lt;/em&gt;?&lt;/p&gt;

&lt;p&gt;But &lt;em&gt;what could be &lt;strong&gt;more&lt;/strong&gt;&lt;/em&gt;? Well, for starters, opening more than one tab. While you wait your pesky &lt;code&gt;make&lt;/code&gt; command run for a couple minutes, why should you need an entire new &lt;em&gt;window&lt;/em&gt; to keep working on your terminal? That's where &lt;a href="https://iterm2.com"&gt;iTerm2&lt;/a&gt; comes to the rescue.&lt;/p&gt;

&lt;p&gt;It is very easy to install, has &lt;a href="https://iterm2.com/features.html"&gt;a ton of features&lt;/a&gt; and, to those who care (like me), it &lt;em&gt;is&lt;/em&gt; Apple Silicon ready!&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7Cv26Jl_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x0owt8sreafihj50k4nl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7Cv26Jl_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x0owt8sreafihj50k4nl.png" alt="image" width="492" height="356"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sHUzsKt7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hmtqz8u72wr63u6xdkhu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sHUzsKt7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hmtqz8u72wr63u6xdkhu.png" alt="image" width="643" height="402"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  6. Get SiliconInfo if you're on Apple Silicon 
&lt;/h2&gt;

&lt;p&gt;Many of us are currently running on the new (at the time of writing) M1 Macs and we often need (or want) to know if our current application is running on old &lt;code&gt;x86&lt;/code&gt; architecture or the current &lt;code&gt;arm64v8&lt;/code&gt; one. Well, &lt;a href="https://apps.apple.com/us/app/silicon-info/id1542271266?mt=12"&gt;Silicon Info&lt;/a&gt; is a pretty straightforward Mac app that adds an icon to your menu bar that can tell you with a glance if you opened the right version of an app! It is pretty handy, yet pretty simple.&lt;/p&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_w99GS0e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0gsvja6us63bj3vtnwc0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_w99GS0e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0gsvja6us63bj3vtnwc0.png" alt="image" width="800" height="382"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  7. Have &lt;code&gt;x86&lt;/code&gt; versions of compatible apps available ⚙️
&lt;/h2&gt;

&lt;p&gt;For some reason, you might need to run the &lt;code&gt;x86&lt;/code&gt; version of an app on your Apple Silicon Mac (compatibility check?), but by default your app opens up on Apple Silicon mode. Well, you can &lt;em&gt;force it&lt;/em&gt; to run under the new &lt;strong&gt;Rosetta2&lt;/strong&gt; environment with a few clicks!&lt;/p&gt;

&lt;p&gt;Open &lt;strong&gt;Finder&lt;/strong&gt; and go to your &lt;strong&gt;Applications&lt;/strong&gt; folder. Duplicate the app you want, for example the previously mentioned &lt;strong&gt;iTerm2&lt;/strong&gt;, and rename it to something like &lt;strong&gt;Rosetta-iTerm2&lt;/strong&gt;. Open up it's info panel and select &lt;strong&gt;Open using Rosetta&lt;/strong&gt;, and that is it!&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IiGUQ_8G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w436nma78ify1bjr6tm6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IiGUQ_8G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w436nma78ify1bjr6tm6.png" alt="image" width="754" height="1788"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now whenever you open that copy of the app, it will be running on &lt;code&gt;x86&lt;/code&gt; architecture!&lt;/p&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PPzi51q5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7n6xcfjh2oa8mb4qyadn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PPzi51q5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7n6xcfjh2oa8mb4qyadn.png" alt="image" width="337" height="208"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  8. Get a better &lt;code&gt;shell&lt;/code&gt; 🐚
&lt;/h2&gt;

&lt;p&gt;Yes, the good ol' &lt;code&gt;bash&lt;/code&gt; shell is fine, but from Catalina onwards Apple replaced it with &lt;code&gt;zsh&lt;/code&gt; with no further explanation, but one would assume its because &lt;code&gt;bash&lt;/code&gt; is aging and &lt;code&gt;zsh&lt;/code&gt; is the new hot thing. Well, along with it you can install &lt;a href="http://ohmyz.sh"&gt;&lt;code&gt;oh-my-zsh&lt;/code&gt;&lt;/a&gt; and customize even further your terminal, not only to make it cooler and more interesting but also more &lt;em&gt;informative&lt;/em&gt;, enhancing your productivity. All of that with some minor tweaks.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DbQ5kuNs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a08yt3l4uv4wbevr49oz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DbQ5kuNs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a08yt3l4uv4wbevr49oz.png" alt="image" width="800" height="478"&gt;&lt;/a&gt;&lt;br&gt;
With a glance I can see what folder I'm in (as usual), what git branch I'm on, and if that branch is dirty or not! Pretty handy right? &lt;/p&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wBdDWNsM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/onsp83h392l6ki11dip7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wBdDWNsM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/onsp83h392l6ki11dip7.png" alt="image" width="300" height="300"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  9. Start using &lt;code&gt;alias&lt;/code&gt;-es! 🕵️‍♀️🕵️‍♂️
&lt;/h2&gt;

&lt;p&gt;Let's face it, there are probably at least a few commands you use very regularly that are long and boring or easy to mistype. Why should you type it all the time?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uaTkonPL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5agsemgsozzfzxtxxd66.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uaTkonPL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5agsemgsozzfzxtxxd66.png" alt="image" width="800" height="342"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Most (maybe all?) shells allow you to create aliases for those commands, so take advantage of that! Open your &lt;code&gt;.zshrc&lt;/code&gt; or &lt;code&gt;.bashrc&lt;/code&gt; file and add a new one with a simple line!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;alias &lt;/span&gt;&lt;span class="nv"&gt;shortcut&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'long command'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then boom! You're all set!&lt;/p&gt;




&lt;p&gt;For now, that is it! A very brief productivity setup to help you in your work from home life. Whenever I add something to my own setup I'll be sure to add it here as well!&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>developer</category>
      <category>work</category>
      <category>macos</category>
    </item>
    <item>
      <title>🛠Refactoring: Replacing Conditional with Polymorphism</title>
      <dc:creator>Lucas Fonseca Mundim</dc:creator>
      <pubDate>Mon, 26 Jul 2021 14:18:23 +0000</pubDate>
      <link>https://forem.com/mundim/refactoring-replacing-conditional-with-polymorphism-2ob6</link>
      <guid>https://forem.com/mundim/refactoring-replacing-conditional-with-polymorphism-2ob6</guid>
      <description>&lt;h4&gt;
  
  
  &lt;code&gt;Switch&lt;/code&gt; statements are often considered code smells and should be avoided
&lt;/h4&gt;




&lt;h2&gt;
  
  
  The Problem
&lt;/h2&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%2Fuploads%2Farticles%2Feh382rmot1e2oung00kb.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%2Fuploads%2Farticles%2Feh382rmot1e2oung00kb.png" alt="image"&gt;&lt;/a&gt;&lt;br&gt;
Suppose you have a class with various subtypes defined by a property such as &lt;code&gt;Bird&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// whatever properties do birds have&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;BirdType&lt;/span&gt; &lt;span class="n"&gt;Type&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&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="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;BirdType&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;African&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;American&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;European&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And then you need to retrieve, lets say, the speed of a given bird. Thing is: the speed of the bird depends of it's type. Some logic is required, but it sounds easy enough right? You create a class method:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// whatever properties do birds have&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;BirdType&lt;/span&gt; &lt;span class="n"&gt;Type&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;GetSpeed&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;switch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;African&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="c1"&gt;// logic for African birds&lt;/span&gt;
            &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;American&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="c1"&gt;// logic for American birds&lt;/span&gt;
            &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;European&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="c1"&gt;// logic for European birds&lt;/span&gt;
            &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="c1"&gt;// default logic&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;A little bit ugly, but it gets the job done right? But here's the catch: after some time in production, you need to add a &lt;em&gt;new&lt;/em&gt; type of bird, lets say Asian&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;BirdType&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;African&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;American&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;European&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Asian&lt;/span&gt; &lt;span class="c1"&gt;// new type&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And now you have to calculate it's speed as well. &lt;em&gt;"Just add another case to the &lt;code&gt;switch&lt;/code&gt; statement"&lt;/em&gt; you might say. You could do that, yes, but bear in mind that this will violate &lt;strong&gt;SOLID's Open-Closed Principle&lt;/strong&gt;! And we don't want that, do we?&lt;/p&gt;




&lt;h2&gt;
  
  
  The Solution: Polymorphism
&lt;/h2&gt;

&lt;p&gt;With Object-Oriented Programming we have the concept of inheritance and, with that, we can implement polymorphism.&lt;/p&gt;

&lt;p&gt;To the uninitiated, polymorphism is the ability to process objects differently depending on their class. Sounds familiar?&lt;/p&gt;

&lt;p&gt;How do we approach the above problem with polymorphism in mind? We start very much the same: defining the &lt;code&gt;Bird&lt;/code&gt; class, but with one change:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;GetSpeed&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;Now we have added the &lt;code&gt;abstract&lt;/code&gt; modifier to the &lt;code&gt;GetSpeed&lt;/code&gt; method and the &lt;code&gt;Bird&lt;/code&gt; class itself. What that means is that the &lt;code&gt;Bird&lt;/code&gt; class itself has no implementation for that method, nor can it be directly instantiated, and any subclasses might implement it themselves. Now all we gotta do is implement the subclasses!&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AfricanBird&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Bird&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;GetSpeed&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// logic for African birds&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AmericanBird&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Bird&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;GetSpeed&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// logic for American birds&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EuropeanBird&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Bird&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;GetSpeed&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// logic for European birds&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;Let's see how that plays on a running code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// Ran on C# Interactive via VisualStudio&lt;/span&gt;
&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Bird&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="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;GetSpeed&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="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AfricanBird&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Bird&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="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;GetSpeed&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="p"&gt;.&lt;/span&gt;         &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="m"&gt;1&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="p"&gt;.&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AmericanBird&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Bird&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="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;GetSpeed&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="p"&gt;.&lt;/span&gt;         &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="m"&gt;0.7&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="p"&gt;.&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Bird&lt;/span&gt; &lt;span class="n"&gt;africanBird&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;AfricanBird&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Bird&lt;/span&gt; &lt;span class="n"&gt;americanBird&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;AmericanBird&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;africanBird&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetSpeed&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="m"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;americanBird&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetSpeed&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="m"&gt;0.7&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Note that as the method we used is declared by &lt;code&gt;Bird&lt;/code&gt;, we could define the variables as the base class and still use it, and we got our desired result 😉&lt;/p&gt;

&lt;p&gt;And what if we need to add a new type of bird? We just add a new subclass! No need to modify any existing class! We have respected the Open-Closed Principle!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Note:&lt;/strong&gt; This refactor that was done is called &lt;strong&gt;Replace conditional with Polymorphism&lt;/strong&gt; (pretty easy to remember the name huh?)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Benefits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The forementioned compliance with the &lt;strong&gt;Open-Closed Principle&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Gets rid of duplicate code, as you can get rid of many conditionals and/or switch statements&lt;/li&gt;
&lt;li&gt;Adheres to the &lt;strong&gt;Tell, Don't Ask principle&lt;/strong&gt;: the object itself is &lt;em&gt;telling&lt;/em&gt; you what you want to know, instead of you having to "ask" (via conditionals) for the information&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Reference
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://martinfowler.com/books/refactoring.html" rel="noopener noreferrer"&gt;Martin Fowler's "Refactoring" book&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://refactoring.guru/replace-conditional-with-polymorphism" rel="noopener noreferrer"&gt;Refactoring.guru: Replace conditional with Polymorphism&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://daedtech.com/switch-statements-are-like-ants/" rel="noopener noreferrer"&gt;Switch Statements are like Ants&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.webopedia.com/definitions/polymorphism/" rel="noopener noreferrer"&gt;Polymorphism definition&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>polymorphism</category>
      <category>objectoriented</category>
      <category>goodpractice</category>
    </item>
  </channel>
</rss>
