<?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: Gabriel Schade</title>
    <description>The latest articles on DEV Community by Gabriel Schade (@gabrielschade).</description>
    <link>https://dev.to/gabrielschade</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%2F180693%2F1f26cc21-08fc-4354-8c1d-8754ea22d507.jpeg</url>
      <title>DEV Community: Gabriel Schade</title>
      <link>https://dev.to/gabrielschade</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gabrielschade"/>
    <language>en</language>
    <item>
      <title>C# Functional Programming</title>
      <dc:creator>Gabriel Schade</dc:creator>
      <pubDate>Tue, 16 Jul 2019 17:56:03 +0000</pubDate>
      <link>https://dev.to/gabrielschade/c-functional-programming-4f7o</link>
      <guid>https://dev.to/gabrielschade/c-functional-programming-4f7o</guid>
      <description>&lt;p&gt;Let’s start with a question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Is it worth it?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Like almost everything in software development, It depends… I really like the functional programming paradigm, then, probably there’s some bias here. But let’s trying to be reasonable, right?&lt;/p&gt;

&lt;p&gt;The first major point is: why to use C#? — If you want to use functional programming there’s a huge probability that &lt;strong&gt;F#&lt;/strong&gt; fits better than C# in a .NET environment.&lt;/p&gt;

&lt;p&gt;There’s a little problem here, a lot of .NET developers have no idea what is F#, I genuinely think that this is a huge mistake, but it’s topic for another article. So, let’s keep it at C# and this bring us to the next question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Do you like System.Linq?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The answer probably will be: &lt;strong&gt;&lt;em&gt;YES!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Because System.Linq is awesome and guess what, it works with one of the core functional concepts: &lt;em&gt;High Order Functions&lt;/em&gt;. There’s also a lot more things related to System.Linq, like extension methods, IEnumerables and so on.&lt;/p&gt;

&lt;p&gt;Backing to the original question, well, C# contains some native features related to functional paradigm, so, it’s fair to say, that you can do great things by using it.&lt;/p&gt;

&lt;p&gt;Let’s code a simple example, we want to iterate an &lt;code&gt;IEnumerable&lt;/code&gt; to show each one of its elements:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Our core feature was implemented successfully, but is that the best code that we can create? — I don’t think so.&lt;/p&gt;

&lt;p&gt;Let’s remove the &lt;strong&gt;hard coded values&lt;/strong&gt; and make it a parameter, also, we can use &lt;code&gt;IEnumerable&lt;/code&gt; interface with generics, it makes a lot more sense:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The &lt;code&gt;static&lt;/code&gt; modifier it’s just for make it callable by &lt;code&gt;Main&lt;/code&gt; method directly. This new implementation is better than the previous one, but there’s still some hard code here, can you see it?&lt;/p&gt;

&lt;p&gt;It’s called &lt;strong&gt;hard coded behavior&lt;/strong&gt;, as developer I have to say that it’s almost so toxic as &lt;strong&gt;hard coded values&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;But, what it exactly means?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Well, when we think about code, specially object oriented code, variables are understood as values or attributes and methods as behaviors. So, &lt;strong&gt;hard coded behavior&lt;/strong&gt; occurs when you call a method &lt;strong&gt;hard coded&lt;/strong&gt;, like, &lt;code&gt;Console.WriteLine&lt;/code&gt; in our code.&lt;/p&gt;

&lt;p&gt;Okay, but is it possible to pass a function as parameter? — &lt;strong&gt;Sure&lt;/strong&gt;, just like &lt;code&gt;System.Linq&lt;/code&gt; do.&lt;/p&gt;

&lt;p&gt;As I said before, there’s a lot of functional programming support in C# and one of these is the reference type called &lt;code&gt;delegate&lt;/code&gt;. It’s used to encapsulate a function/method, it’s like a variable that can work with functions instead of values.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Delegates are a topic apart, so if you don’t know anything about it, just check &lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/delegate"&gt;this link&lt;/a&gt; out.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Fortunately, there’s two generic delegates built-in C#. They are &lt;code&gt;Action&lt;/code&gt; and &lt;code&gt;Func&lt;/code&gt;, both of them can work with generic types.&lt;/p&gt;

&lt;p&gt;The only difference between them are the fact that &lt;code&gt;Action&lt;/code&gt; is used to encapsulate methods with no return (&lt;code&gt;void&lt;/code&gt; methods), for all other type of methods the &lt;code&gt;Func&lt;/code&gt; delegate is used.&lt;/p&gt;

&lt;p&gt;Let’s change our code a little bit, just replace the hard coded call to a generic one:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Congratulations&lt;/strong&gt;! You just did your first &lt;em&gt;High Order Function&lt;/em&gt;!&lt;/p&gt;

&lt;p&gt;This method is a lot more generic than the previous one, now we need to change just the &lt;code&gt;Iterate&lt;/code&gt; call if we want a different operation.&lt;/p&gt;

&lt;p&gt;For instance, if we want to write the value squared instead of the original one:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As you may notice, &lt;code&gt;delegates&lt;/code&gt; work with named and anonymous functions, in the above code, we changed the &lt;code&gt;Iterate&lt;/code&gt; call by passing a lambda expression as parameter.&lt;/p&gt;

&lt;p&gt;The icing on the cake is make it an extension method, so, it’ll works like a Linq method.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you don’t know what an extension method is, check &lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods"&gt;this link&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In order to do that, we need to create a new &lt;code&gt;static&lt;/code&gt; class and use the &lt;code&gt;this&lt;/code&gt; keyword before the first parameter:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now we can use it as an &lt;code&gt;IEnumerable&lt;/code&gt; method:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This is a very simple application of functional programming using C#, but the main point is: knowing functional programming will extends your toolbox, so you probably gonna be a better developer with this skills.&lt;/p&gt;

&lt;p&gt;It is more than another language, it is a lot more related to the way of thinking and solving problems than the programming tools you will use.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;And please, you &lt;strong&gt;shouldn’t&lt;/strong&gt; try to use your functional programming skills in each single piece of code you develop.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Use it to improve your code and your solutions, not to make them more complex.&lt;/p&gt;

&lt;p&gt;And remember:&lt;/p&gt;

&lt;h3&gt;
  
  
  With great power comes great responsibility
&lt;/h3&gt;

&lt;p&gt;See you in the next post!&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>functional</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>How to Create Infinite Collections Using IEnumerable Properly</title>
      <dc:creator>Gabriel Schade</dc:creator>
      <pubDate>Mon, 01 Jul 2019 17:33:06 +0000</pubDate>
      <link>https://dev.to/gabrielschade/how-to-create-infinite-collections-using-ienumerable-properly-3b5j</link>
      <guid>https://dev.to/gabrielschade/how-to-create-infinite-collections-using-ienumerable-properly-3b5j</guid>
      <description>&lt;p&gt;All C# developers faced these interfaces at some point, but how to use them? And better than that, how to use them &lt;strong&gt;properly&lt;/strong&gt;? — Because I’ve seen a lot of mistakes with these guys.&lt;/p&gt;

&lt;p&gt;The main point of writing this article is the fact that I lost the count about how many times I’ve seen mistakes and misunderstoods related to C# collections, so I hope to clarify some points here.&lt;/p&gt;

&lt;p&gt;Before we start, let’s go a little back and see the cover image at the beginning of the text. It’s a visualization about how you may thinking about &lt;code&gt;IEnumerables&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It works like a Turing Machine tape with a cursor that start pointing to &lt;code&gt;null&lt;/code&gt;. This cursor move only straight forward (to right in the image), without index access and besides that, only the necessary information to iterate the collection is provided by an &lt;code&gt;IEnumerable&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Having said that, we can start to &lt;strong&gt;code&lt;/strong&gt;. Let’s check the &lt;code&gt;IEnumerable&lt;/code&gt; implementation (with and without generics):&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And that’s all. There’s no one more line code at &lt;code&gt;IEnumerable&lt;/code&gt; implementation.&lt;/p&gt;

&lt;p&gt;What does this suggest?&lt;/p&gt;

&lt;p&gt;&lt;code&gt;IEnumerable&lt;/code&gt; actually is a &lt;code&gt;IEnumerator&lt;/code&gt; factory. Now we need to check &lt;code&gt;IEnumerator&lt;/code&gt; out. It’s a little bit more complex, but we can handle it.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;There’s two different interfaces, with and without using generics, just like &lt;code&gt;IEnumerable&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;IEnumerator&lt;/code&gt; is the one who provides the information about how to iterate a collection (and &lt;code&gt;IEnumerable&lt;/code&gt; that is the famous one, how unfair is that?).&lt;/p&gt;

&lt;p&gt;In fact, when we use the &lt;code&gt;foreach&lt;/code&gt; loop, C# internally calls the &lt;code&gt;GetEnumerator&lt;/code&gt; of the collection we’ll iterate and at each step it calls the &lt;code&gt;MoveNext&lt;/code&gt; method to move the cursor forward.&lt;/p&gt;

&lt;p&gt;Let’s code a super simple code to iterate an IEnumerable of integers using a foreach loop:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;It’s pretty simple, isn’t?&lt;/p&gt;

&lt;p&gt;As I said before, this code is a simple syntax sugar to another code a little bit more complex, but still a simple code snippet:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This code is a lot more uglier but it does the exactly same thing. The first step got the enumerator using &lt;code&gt;GetEnumerator&lt;/code&gt; method, after that, we can use the &lt;code&gt;MoveNext&lt;/code&gt; method to iterate each of &lt;code&gt;IEnumerable&lt;/code&gt; elements.&lt;/p&gt;

&lt;p&gt;It’s interesting to notice that the &lt;code&gt;MoveNext&lt;/code&gt; method is called even before we got the first element. It occurs because the enumerator cursor start pointing to one position before the first element (as shown in the tape image).&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;MoveNext&lt;/code&gt; method have a design that I particularly don’t like. It does two different things.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;One of these things is to move the cursor to the next position, which is the main purpose;&lt;/li&gt;
&lt;li&gt;The secondary thing is what this method returns. It returns &lt;code&gt;true&lt;/code&gt; if the cursor is pointing to a valid position after it was moved or it will return &lt;code&gt;false&lt;/code&gt; if isn’t.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;My problem with the design of this method is the fact that the main operation, move the cursor to the next element, is caused only by a side effect, but this is a topic for another article, let’s move on.&lt;/p&gt;

&lt;p&gt;After call the &lt;code&gt;MoveNext&lt;/code&gt; method the first time, we’ll move the cursor to the very first position of the &lt;code&gt;IEnumerable&lt;/code&gt;, like the image below:&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%2Fl8oap5qmlbanfh12d52k.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%2Fl8oap5qmlbanfh12d52k.png" alt="First Position"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And the property &lt;code&gt;Current&lt;/code&gt; just returns the value at the position that the cursor points to.&lt;/p&gt;

&lt;p&gt;Having said that, how about to create a brand new collection containing &lt;strong&gt;all integer positive numbers&lt;/strong&gt; that exists?&lt;/p&gt;

&lt;p&gt;Yeah, you read it correctly, a collection with all numbers, which means, a &lt;strong&gt;infinity collection&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Here’s a thing before we start to code, the whole point of this implementation is see how &lt;code&gt;IEnumerator&lt;/code&gt; and &lt;code&gt;IEnumerable&lt;/code&gt; works under the hood with a &lt;code&gt;foreach&lt;/code&gt; loop, so, don’t take it as the best implementation possible for a new collection, it’s just an experiment, right?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s start with an &lt;code&gt;IEnumerator&lt;/code&gt;, so, just create a new class that implements &lt;code&gt;IEnumerator&amp;lt;int&amp;gt;&lt;/code&gt;, It will looks like the code below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;So, now we need to create our own stuff now. In order to create an infinite collection, we’ll use the cursor itself as a value.&lt;/p&gt;

&lt;p&gt;Let’s create a new field called &lt;code&gt;_current&lt;/code&gt; which starts at &lt;code&gt;-1&lt;/code&gt; and will be incremented as the &lt;code&gt;MoveNext&lt;/code&gt; method is called (usually the value is stored at the &lt;code&gt;IEnumerable&lt;/code&gt;, but this isn’t a regular example, remember?)&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Ok, but let’s understand why &lt;code&gt;_current&lt;/code&gt; starts with &lt;code&gt;-1&lt;/code&gt; instead of zero. The answer is pretty simple, it’s because the &lt;code&gt;MoveNext&lt;/code&gt; method is called before the iteration.&lt;/p&gt;

&lt;p&gt;Now, let’s go to &lt;code&gt;MoveNext&lt;/code&gt; method, you probably already know what to do here. We need to increase the &lt;code&gt;_current&lt;/code&gt; value and always return true, after all, it’s an infinite collection, so always is possible to move to the next one.&lt;/p&gt;

&lt;p&gt;We don’t need to worry about the &lt;code&gt;Dispose&lt;/code&gt; method now, so let’s implement the &lt;code&gt;Reset&lt;/code&gt; method by reseting the &lt;code&gt;_current&lt;/code&gt; to its initial value.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now, let’s implement our collection itself. It’ll be easy, trust me. All we need is implement the &lt;code&gt;IEnumerable&amp;lt;int&amp;gt;&lt;/code&gt; interface and return an instance of our &lt;code&gt;IEnumerator&lt;/code&gt;.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Maybe for some reason, you may need return new instances any time you call &lt;code&gt;GetEnumerator&lt;/code&gt;, but for our example, only one instance is enough.&lt;/p&gt;

&lt;p&gt;Now we can use our own class inside of a &lt;code&gt;foreach&lt;/code&gt; loop:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And we can get the next, the next, and the next and keep doing that &lt;strong&gt;forever&lt;/strong&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%2Fmzjd97w02yvjkyqfx0k6.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%2Fmzjd97w02yvjkyqfx0k6.png" alt="Infinite Collection"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How cool is that?&lt;/p&gt;

&lt;p&gt;So, everything was implemented correctly, right? — Not exactly.&lt;/p&gt;

&lt;p&gt;Let’s force an interruption by using &lt;code&gt;break&lt;/code&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;&lt;em&gt;BANG!&lt;/em&gt;&lt;/strong&gt; — An exception was thrown.&lt;/p&gt;

&lt;p&gt;Why this exception was thrown? — Simple, the &lt;code&gt;Dispose&lt;/code&gt; method wasn’t implemented yet.&lt;/p&gt;

&lt;p&gt;Right, but when it’s called?&lt;/p&gt;

&lt;p&gt;Just after the loop! In order to remember, let’s reimplement it without &lt;code&gt;foreach&lt;/code&gt; sugar syntax, in other words, by using &lt;code&gt;while&lt;/code&gt; loop:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now the things are more clear, aren’t they?&lt;/p&gt;

&lt;p&gt;When we iterate across an &lt;code&gt;IEnumerable&lt;/code&gt; we use the &lt;code&gt;using&lt;/code&gt; keyword, and as you probably already know, the &lt;code&gt;Dispose&lt;/code&gt; method is called at the end of the using scope.&lt;/p&gt;

&lt;p&gt;The reason why the &lt;code&gt;Dispose&lt;/code&gt; method is called is a lot more clear with this syntax, even though it looks worse. We can fix it just by calling &lt;code&gt;Reset&lt;/code&gt; method at &lt;code&gt;Dispose&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Warning&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This &lt;strong&gt;isn’t&lt;/strong&gt; the best way to implement the &lt;code&gt;Dispose&lt;/code&gt; method whatsoever, this is just an experiment, ok?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, you have your own infinite collection, it’s time to snapping your fingers (Like Thanos did)!&lt;/p&gt;

&lt;p&gt;See you in the next post!&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Recursion without Stack Overflows</title>
      <dc:creator>Gabriel Schade</dc:creator>
      <pubDate>Wed, 26 Jun 2019 16:06:49 +0000</pubDate>
      <link>https://dev.to/gabrielschade/recursion-without-stack-overflows-3c62</link>
      <guid>https://dev.to/gabrielschade/recursion-without-stack-overflows-3c62</guid>
      <description>&lt;p&gt;This can look like a hard achievement, but actually it isn’t. We’ll use F# language and a technique called &lt;strong&gt;Tail Recursion&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Recursion itself is a little bit scary for a lot of developers, but there’s no reason to that, it’s just a repetition loop with another outfit.&lt;/p&gt;

&lt;p&gt;Eventually you will need it, so, it’s better to be prepared.&lt;/p&gt;

&lt;p&gt;As I said before, recursion is a repetition loop technique, so, it’ll help you to solve problems that can be decomposed into smaller problems.&lt;/p&gt;

&lt;p&gt;However, there are some issues around using recursion. In general, a recursive loop is slower than a regular loop. In addition to that, there’s a major limitation called &lt;strong&gt;call stack&lt;/strong&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  The call stack
&lt;/h1&gt;

&lt;p&gt;Before we start to code, let’s understanding what the call stack is. Well, this data structure is used by our program to store a pointer to an execution line.&lt;/p&gt;

&lt;p&gt;This storage procedure occurs every time your program call a function (any function, not just the recursive ones), this way, the program will be able to return to the correct execution line after function execution.&lt;/p&gt;

&lt;p&gt;Let’s see how it works by create a simple hello function:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You should set a breakpoint at line 2 and open the &lt;strong&gt;Call Stack Window&lt;/strong&gt;, this way, you’ll be able to check the call stack, as you can see below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxulruy254vbfn2jcxdx0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxulruy254vbfn2jcxdx0.png" alt="Call Stack Window" width="667" height="525"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The call stack is a very useful structure for our programs, however, it may be the reason for a crash in our application. In fact, there’s an extremely popular exception raised by the call stack: &lt;strong&gt;StackOverflow&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Have you ever seen that name? — You probably already have seen it at stackoverflow forum, it’s just a joke, because as I said before, it’s a extremely popular exception, mainly when you are learning about recursion.&lt;/p&gt;

&lt;p&gt;This exception occurs because there’s a fixed memory dedicated to the call stack, it can be different depending on the language, but usually it’s around 1MB.&lt;/p&gt;

&lt;p&gt;Then, if we call functions enough to overflow it, &lt;strong&gt;&lt;em&gt;bang&lt;/em&gt;&lt;/strong&gt;! We got a &lt;code&gt;StackOverflow&lt;/code&gt; exception.&lt;/p&gt;

&lt;h1&gt;
  
  
  A recursive sum
&lt;/h1&gt;

&lt;p&gt;Let’s create a recursive function that sums all elements of a given list:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This is a simple pattern matching that returns zero if the list is empty &lt;code&gt;[]&lt;/code&gt; , or otherwise it will return the sum of the &lt;code&gt;head&lt;/code&gt; element (first element of the list) and the sum of the &lt;code&gt;tail&lt;/code&gt; (all other elements of the list).&lt;/p&gt;

&lt;p&gt;Let’s create a 1000 elements list and call this function:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Yay, success!&lt;/p&gt;

&lt;p&gt;Now, let’s increase the list size to 10000:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjhel5a6ld333pe388y0i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjhel5a6ld333pe388y0i.png" alt="StackOverflow" width="753" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ooops, it doesn’t looks like a success right now. We just got a &lt;code&gt;StackOverflow&lt;/code&gt; exception.&lt;/p&gt;

&lt;p&gt;It happens because we aren’t using tail recursion yet. Ok, I got it, but after all, what tail recursion is?&lt;/p&gt;

&lt;h1&gt;
  
  
  Tail Recursion and the Tail Call
&lt;/h1&gt;

&lt;p&gt;Tail recursion is a specific type of recursion implemented by some languages (mainly the functional programming languages). This concept is directly related to what we call as tail call.&lt;/p&gt;

&lt;p&gt;A tail call is when a function call occurs with the guarantee that there’s &lt;strong&gt;no instructions&lt;/strong&gt; to be executed after it. It should be the last instruction at all.&lt;/p&gt;

&lt;p&gt;So, because the call is the last thing to happen, is pretty fair to say that the instruction line to return after this new call is the same instruction line to the previous call, because there’s no code to be executed after this tail call.&lt;/p&gt;

&lt;p&gt;Because of that, the program doesn’t need to store a pointer to each call, because it’s all the same. Internally the compiler will replace this recursive tail call with a JUMP instruction, it’ll avoid the overflow exception and improve performance as well, it’s a win win deal.&lt;/p&gt;

&lt;p&gt;Now, we need to understanding why our code isn’t using a tail call. Well, a tail call only happens when the function call is at a tail position. Tail position is just the name of the last instruction of a function, it may be a lot of different things, let’s see some examples:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Hold on a second, the &lt;code&gt;function4&lt;/code&gt; isn’t the same structure of our previous code? — No, it isn’t.&lt;/p&gt;

&lt;p&gt;It’s almost the same, in fact each branch of an pattern matching can be a tail position, however, there’s an extra code on our pattern branch.&lt;/p&gt;

&lt;p&gt;We aren’t simply return a value, we are making a sum between a value and the result of a function call. As you probably know, the function call occurs before the sum itself, so, the call isn’t the last instruction.&lt;/p&gt;

&lt;p&gt;Are you already figured out how to solve it?&lt;/p&gt;

&lt;p&gt;It’s pretty simple, instead to accumulate the value with returns, we’ll accumulate the value as a parameter. So, our function now contains two different parameters: the list and the accumulator.&lt;/p&gt;

&lt;p&gt;To prevent the function consumer to give a wrong value as accumulator, we gonna make this new function a nested function, and call it internally always passing zero as accumulator initial value.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Maybe this code looks a little more unfamiliar to you, but it’s ok, tail recursion is very important (and useful) when we are talking about functional programming, as recursive functions is preferred over regular loops.&lt;/p&gt;

&lt;p&gt;So, let’s check our code again:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw9s9txrooyb814yc54pr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw9s9txrooyb814yc54pr.png" alt="No Stackoverflow" width="598" height="365"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;No &lt;code&gt;StackOverflow&lt;/code&gt; at all!&lt;/p&gt;

&lt;p&gt;In fact, we can set a breakpoint to check the call stack:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhqlfbq0do2krcsnj5brz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhqlfbq0do2krcsnj5brz.png" alt="Call Stack" width="800" height="250"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see, there’s no new pointers added in each call! How cool is that?&lt;/p&gt;

&lt;p&gt;Now we implemented a tail recursion properly, but how can I check if my new random function was implemented correctly?&lt;/p&gt;

&lt;p&gt;Well, the simplest test ever is just execute your function with a very large input, another simple test is check the call stack. But of course, there’s a more accurated test.&lt;/p&gt;

&lt;h1&gt;
  
  
  IL Disassembler
&lt;/h1&gt;

&lt;p&gt;You can use the &lt;strong&gt;IL Disassembler&lt;/strong&gt; in order to make a more deep analysis.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you don’t know IL at all, I strongly recomend you to check &lt;a href="https://docs.microsoft.com/en-us/dotnet/standard/managed-code#intermediate-language--execution"&gt;this link&lt;/a&gt;. Here’s a quick overview: IL is the intermediate language generated by .NET compiler. When you compile your .NET code this is the result, which’ll be compiled again in execution time (JIT).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The IL Disassembler can be open by Visual Studio Developer Command Prompt, you can type “developer command” at Windows search box and it’ll be listed (it looks like the regular command prompt).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9jnw4ue8ang5xql4ubsc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9jnw4ue8ang5xql4ubsc.png" alt="IL CMD" width="656" height="256"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can navigate to your project folder and execute the command &lt;code&gt;ildasm project-name.dll&lt;/code&gt;, it’ll open the IL Disassembler, as shown in the following image:&lt;/p&gt;

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

&lt;p&gt;Now, let’s check the first sum function, which &lt;strong&gt;raised&lt;/strong&gt; the &lt;code&gt;StackOverflow&lt;/code&gt; exception.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Maybe the IL language looks weird to you, but this isn’t a problem, we don’t want to understanding the language in a comprehensive way right now. Let’s focus on call instruction, it happens in four different moments:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;IL 003 — call to get_TailOrNull() function;&lt;/li&gt;
&lt;li&gt;IL 011 — call to get_TailOrNull() function;&lt;/li&gt;
&lt;li&gt;IL 018 — call to get_HeadOrDefault() function;&lt;/li&gt;
&lt;li&gt;IL 020 — call to sum function;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The last call is the most important one, because it shows that a new call really occurs here.&lt;/p&gt;

&lt;p&gt;Now, let’s check the &lt;code&gt;tailRecursionSum&lt;/code&gt; function:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Oops, something is wrong, there’s also a call instruction here. Well, actually to check our recursive function we need to open the nested function.&lt;/p&gt;

&lt;p&gt;In order to open this IL code, we need to navigate at program tree and open the &lt;code&gt;Invoke&lt;/code&gt; file:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa20rt4840s4ijvzvd966.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa20rt4840s4ijvzvd966.png" alt="ILdasm Invoke Function" width="380" height="342"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let’s see the code:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;There’s also three &lt;strong&gt;call&lt;/strong&gt; instructions here. But if we look carefully we gonna see that all calls are related to &lt;code&gt;head&lt;/code&gt; and &lt;code&gt;tail&lt;/code&gt; functions.&lt;/p&gt;

&lt;p&gt;Instead our last recursive call we can see a &lt;code&gt;br.s&lt;/code&gt; instruction, which works like a JUMP to another instruction. In this specific case, a JUMP to the first instruction (&lt;code&gt;IL_0000&lt;/code&gt; ).&lt;/p&gt;

&lt;p&gt;Just before this &lt;code&gt;br.s&lt;/code&gt; we can see two &lt;code&gt;starg.s&lt;/code&gt; instructions, this is a short for store argument, in other words, our IL code is updating the value of the arguments (acc and list), and after that, it’ll back to the first instruction with updated values, like an regular loop.&lt;/p&gt;

&lt;p&gt;Because of this JUMP structure, there’s no &lt;code&gt;Stackoverflow&lt;/code&gt; and there’s a performance improvement, it’s really nice to see how the things works under the hood.&lt;/p&gt;

</description>
      <category>functional</category>
      <category>fsharp</category>
      <category>dotnet</category>
    </item>
  </channel>
</rss>
