<?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: Tamas Fodor</title>
    <description>The latest articles on DEV Community by Tamas Fodor (@ruffle1986).</description>
    <link>https://dev.to/ruffle1986</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%2F100033%2F9cb63755-8a1a-47e9-97b7-ac24c6338b3c.jpg</url>
      <title>DEV Community: Tamas Fodor</title>
      <link>https://dev.to/ruffle1986</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ruffle1986"/>
    <language>en</language>
    <item>
      <title>How to FOMO</title>
      <dc:creator>Tamas Fodor</dc:creator>
      <pubDate>Thu, 14 Mar 2019 18:22:11 +0000</pubDate>
      <link>https://dev.to/ruffle1986/how-to-fomo-3bke</link>
      <guid>https://dev.to/ruffle1986/how-to-fomo-3bke</guid>
      <description>&lt;p&gt;Has it ever happened to you when you tried to be as productive as you can to kick the day in the ass, but rather you ended up constantly checking the latest technology news on whatever platform you're stuck into? At the end of the day, you feel like you're worthless, unproductive, anxious and overwhelmed. &lt;/p&gt;

&lt;p&gt;The good news is that you're not alone. The bad news? You have nothing to do about it. Well, that is just not true but you definitely can't get rid of it entirely. You can just get better control over it. Ladies and gentlemen, behold FOMO, a.k.a. Fear of Missing Out programmed deep inside our genes, Thanks to our prefrontal cortex or whatever part of the brain that is responsible for bringing this bastard into play. &lt;/p&gt;

&lt;p&gt;When you look after FOMO on the Internet, you more likely stumble upon articles about the social aspects. From my perspective, as a developer, it's a little bit different. Just a little bit.&lt;/p&gt;

&lt;h1&gt;
  
  
  Impostor syndrome
&lt;/h1&gt;

&lt;p&gt;I've been working in the Web development industry for over ten years now. I don't have a computer science degree which is a very important component in my case. I don't know much about you but in my country, it's still a thing to rank people based on what paper they have. I still question my value. I still question whether I have the right to work with those smart people around me, whether I'm good enough to accept the challenge and do my best to solve the problem. When it comes to impostor syndrome, FOMO follows. &lt;/p&gt;

&lt;p&gt;So you've just got the opportunity to be the part of a team, full of talented people at a new job, huh? Congratulations, dude! It's your first day and right after the onboarding process, you're looking at the new codebase you're going to work on. Until that point, you might have convinced yourself that you're a decent developer. Your colleagues seemed nice. What could go wrong? And then, all of a sudden something has changed. You're like "Oh my gosh! What the heck is this theoretical physics I'm staring at?". The impostor syndrome is about starting to eat you from the inside. You're suffering from it and you're eventually ended up reconsidering your whole life until that point. This is a very bad feeling and you don't want to feel it again, EVER! So you've decided to take action. The outcome? Endless amount of newsletter subscriptions in your mailbox and the number just goes up every day. You've already ordered 3 bestselling books from Amazon on a particular topic just to feel it more tangible. Constantly seeking articles on the Internet to avoid a tiny bit of a possibility to miss anything you think you should know about. Or even worse, spending tons of money on online courses which are totally irrelevant. &lt;/p&gt;

&lt;p&gt;That is just ridiculous! And you have to stop doing it RIGHT NOW!&lt;/p&gt;

&lt;h1&gt;
  
  
  Stick to the goal
&lt;/h1&gt;

&lt;p&gt;Yeah, I hear you. You've been told by all the gurus and coaches and even by your managers that you have to set your own goals. "What goals are you talking about? My goal is to survive this embarrassing situation and fake it `til I make it." Well, I have to be the bad guy this time. They're right. You can't just go to work and hang around. Completing a task here, completing a task there. No. Life is too short to spend your whole day at your 9-5 job just because they give you money and Netflix doesn't accept sand and mud in return for their services. &lt;br&gt;
There must have been a reason why you chose that job. If the job is to create a single page React application, you don't have to focus on Angular or Vue just because "what if they would fire me tomorrow and my next project will be based on them". If your first task is increasing the test coverage, you should focus on learning about the tools you have to use to write unit tests, learning about testing best practices and so on.  If you're a front end developer, you don't have to learn about Kubernetes just because your colleagues talk about it during lunch time and you don't want to look foolish and stay silent. If it's for your own entertainment and you're really interested in the topic with all your honesty, that's ok of course.&lt;/p&gt;

&lt;p&gt;Staying at the testing example for a moment, suppose your goal is becoming a pro in all the tools you've been using at work to make sure your React app works. The goal itself is too big, you need a plan and break it down in order to accomplish. Forget all the distracting things around you and stick to the plan. Forget Angular, Vue, and Kubernetes. And believe me, it's time to declutter your mailbox.&lt;/p&gt;

&lt;h1&gt;
  
  
  It's ok to say, I don't know
&lt;/h1&gt;

&lt;p&gt;The reason why we tend to FOMO at work is the fear of being involved in an embarrassing situation when you're asked about something you think you're supposed to know but you don't. That's only in your head. It doesn't really exist. If it happens to get the chance to have a nice conversation with your colleagues during the lunch break about something you don't know, it's ok to say "I don't know". Say "That's interesting. Tell me more about it." They will be more than happy to tell you more about the topic. It's a great opportunity to touch something on the surface and not to mention the opportunity to take your social life to the next level at your job.&lt;/p&gt;

&lt;h1&gt;
  
  
  Stop seeking validation
&lt;/h1&gt;

&lt;p&gt;The other reason why we FOMO at work is that we take other's opinion on us too seriously. "What she's gonna think about me if I don't know this?", "What they're gonna think about me if I don't know that". Would you like to know what the truth is? They don't even care. The Universe is not spinning around you.&lt;/p&gt;

&lt;h1&gt;
  
  
  Just ignore things
&lt;/h1&gt;

&lt;p&gt;When you're about to move forward on the path of your journey and you stumble upon something that grabs your attention, just ask yourself the question, "Does it support me to reach my goals?" Doesn't matter if it's the brand new React newsletter, a youtube notification, a tweet by Dan Abramov. Doesn't matter. If it's a mutual fit between you and your goal then go for it. If it doesn't, just simply ignore. Don't underestimate the power of ignorance. The World won't collapse if you miss the hype around a new React feature or something mainstream in the tech industry. You and your goal are more important and the rest can wait. That is the best what you can do for your self-esteem.&lt;/p&gt;

&lt;p&gt;I've been really into Swift development recently so I decided to declutter my mailbox. I unsubscribed from all the newsletters that don't support me on the path of a Swift developer. &lt;/p&gt;

&lt;p&gt;At work, it happens very often when people around me start talking about something. I just simply ignore them. If it's important, they'll let me know about it for sure. Otherwise, it's just noise.&lt;/p&gt;

&lt;p&gt;And last but certainly not least, you should really consider removing all the distracting items from your phone or disabling the notifications at least. They take advantage of human nature and push all the things you don't need at all. &lt;br&gt;
Don't let the information be pushed towards you. You'll pull it when you need it.&lt;/p&gt;

&lt;p&gt;Take care!&lt;/p&gt;

</description>
      <category>career</category>
      <category>productivity</category>
      <category>beginners</category>
      <category>motivation</category>
    </item>
    <item>
      <title>Understanding Swift Closures - Part 2</title>
      <dc:creator>Tamas Fodor</dc:creator>
      <pubDate>Thu, 14 Mar 2019 12:07:48 +0000</pubDate>
      <link>https://dev.to/ruffle1986/understanding-swift-closures---part-2-2bmo</link>
      <guid>https://dev.to/ruffle1986/understanding-swift-closures---part-2-2bmo</guid>
      <description>&lt;p&gt;This is the second part of a two-part series on Swift Closures. You can read the previous article here: &lt;a href="https://dev.to/ruffle1986/understanding-swift-closures---part-1-390m"&gt;Understanding Swift Closures - Part 1&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this article, we're going to learn about some interesting Swift features related to functions and more specifically, closures:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Inout parameters&lt;/li&gt;
&lt;li&gt;Escaping closures&lt;/li&gt;
&lt;li&gt;Autoclosures&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's get dive into it.&lt;/p&gt;

&lt;h1&gt;
  
  
  Inout parameters
&lt;/h1&gt;

&lt;p&gt;It's not strictly related to closures but to functions in general. By default, the parameters passed to a function or a closure become constants no matter how you've declared them outside the function. That being said, you cannot modify or so to say, mutate the argument inside the function.&lt;/p&gt;

&lt;p&gt;It doesn't matter if it's an integer, string or an array, Swift won't let you change the state of the parameter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;addItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Cannot use mutating member on immutable value: 'arr' is a 'let' constant&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;addItem&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And there's no difference in case of Structs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;MyStruct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="k"&gt;mutating&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;increase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;increaseStruct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;MyStruct&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;increase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// Cannot use mutating member on immutable value: 'obj' is a 'let' constant&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;myObj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;MyStruct&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nf"&gt;increaseStruct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myObj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;NOTE: However, at first glance, structs are very similar to classes, you can update the state of a class instance inside a function. That's because classes are reference types, not value types. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Mutating an input parameter is not a good idea in my opinion. Modifying the input inside a function means the function has an effect on the surrounding environment. A function should be a well-focused, small block of code that doesn't know much about what's happening in the outside world. When a function has been permitted to modify other parts of the application, it's called a side effect.&lt;/p&gt;

&lt;p&gt;Personally, I prefer pure functions and omit side effects as much as I can. It doesn't just make debugging easier but also easier to write unit tests for pure functions. But that's just me. If you don't agree, Swift allows you to update function parameters, just make sure you use the &lt;code&gt;inout&lt;/code&gt; keyword right before the type annotation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;increase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;inout&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we call the &lt;code&gt;increase&lt;/code&gt; function with an integer, Swift wants us to make it crystal clear on both ends that the function is going to change the value. When we declare the function, and when we call it. In case of inout parameters, we have to mark the given argument with the ampersand symbol at the place of the invocation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="nf"&gt;increase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// -&amp;gt; 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;NOTE: you &lt;strong&gt;&lt;em&gt;can't&lt;/em&gt;&lt;/strong&gt; pass the integer directly to the function and mark it with the ampersand. It's the outer scope's responsibility to register a memory pointer by assigning the value to a variable.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Obviously, if you've declared the argument as a constant, Swift won't let you modify the value regardless of the inout keyword at the function declaration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;increase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;inout&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;increase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Cannot pass immutable value as inout argument: 'n' is a 'let' constant&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want, you can do the same with closures but you have to be explicit regarding the closure parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;increase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;inout&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
    &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;increase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// -&amp;gt; 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you define inout parameters but you don't actually mutate them inside the function, you're a very very bad person. Period.&lt;/p&gt;

&lt;h1&gt;
  
  
  Escaping closures
&lt;/h1&gt;

&lt;p&gt;If you pass a closure to a function and you don't actually call it, guess what? Swift is smart enough to figure and won't let you. It's like "Hey man! I don't know you but if you think you can mess with me, you better wake up earlier." So at this point, you're about to do something weird. But "why would you pass a closure to a function if you don't want to call it?" Simple. I store it for later execution. In that case, it's an escaping closure and you have to mark it with the &lt;code&gt;@escaping&lt;/code&gt; keyword right before the type annotation.&lt;/p&gt;

&lt;p&gt;Suppose we have an event manager class by which we can be informed if a certain event happens. In order to do that, we add our listeners and tell the event manager "Hey buddy, please let me know about it by calling this function to me!"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;Event&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Click"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;EventManager&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;listeners&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="kt"&gt;Event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Void&lt;/span&gt;&lt;span class="p"&gt;]()&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;@escaping&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;listeners&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;trigger&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;listener&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;listeners&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Event&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;let&lt;/span&gt; &lt;span class="nv"&gt;em&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;EventManager&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;em&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&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="nv"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; happened."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;em&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trigger&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// Click happened.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, we have an &lt;code&gt;addEventListener&lt;/code&gt; method inside the EventManager class. It takes a closure as the first parameter but it doesn't call it before the method returns. It stores the closure in the listeners property which is an array of closures that take an Event parameter and returns nothing. It's an array because it lets you add multiple listeners if you will. The click event can happen anytime. It can happen immediately, or five minutes later. But when it does, the trigger method is responsible for calling each listener inside the listeners array. In case of scenarios like this, we have to explicitly mark them as &lt;code&gt;@escaping&lt;/code&gt; closures.&lt;/p&gt;

&lt;h1&gt;
  
  
  Autoclosures
&lt;/h1&gt;

&lt;p&gt;For convenience purposes, if you have a simple statement but you want it to be executed later, you can ask Swift to wrap your statement inside a closure. These are the so-called Autoclosures:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;whatever&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;wrapped&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;@autoclosure&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;wrapped&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;whatever&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;wrapped&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, since &lt;code&gt;wrapped&lt;/code&gt; is marked as &lt;code&gt;@autoclosure&lt;/code&gt;, Swift takes the statement, which is adding 1 and 2, put it inside a closure, do the math and returns the result of the evaluation. Autoclosures don't accept any incoming parameters.&lt;/p&gt;

&lt;p&gt;Basically, it lets you omit the extra braces when you call &lt;code&gt;whatever&lt;/code&gt;. And that's it. To be honest, I don't really see the benefits of autoclosures. Yeah sure, I don't have to write the braces but for me, it makes the code a lot harder to follow. &lt;/p&gt;

&lt;p&gt;Am I missing something? If you have any experiences with autoclosures and you see the positive or negative parts I might have missed, let me know in the comments section.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>beginners</category>
      <category>ios</category>
      <category>swift</category>
    </item>
    <item>
      <title>Understanding Swift Closures - Part 1</title>
      <dc:creator>Tamas Fodor</dc:creator>
      <pubDate>Mon, 04 Mar 2019 20:26:27 +0000</pubDate>
      <link>https://dev.to/ruffle1986/understanding-swift-closures---part-1-390m</link>
      <guid>https://dev.to/ruffle1986/understanding-swift-closures---part-1-390m</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” &lt;br&gt;
― Martin Fowler&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As I'm a committed participant of the &lt;a href="https://twitter.com/hashtag/100DaysOfSwift"&gt;100DaysOfSwift&lt;/a&gt; challenge by &lt;a href="https://www.hackingwithswift.com/about"&gt;Paul Hudson&lt;/a&gt;, it was inevitable to meet closures at some point. Even though I have a solid understanding of what closures (or lambdas) are from other languages and why they're useful, I realized that closures are one of the main reasons why I'm struggling with reading others' Swift code.&lt;/p&gt;

&lt;p&gt;After day 7, I didn't want to just simply let it go so I decided to dig deeper. The reason why I'm writing about it is that I'd like to collect my findings and solidify the knowledge I've got during the journey. Hopefully, you will find it beneficial and take the value that is relevant to you.&lt;/p&gt;

&lt;p&gt;Alright, let's get started.&lt;/p&gt;

&lt;h1&gt;
  
  
  What are closures?
&lt;/h1&gt;

&lt;p&gt;According to the official documentation, "Closures are self-contained blocks of functionality that can be passed around and used in your code". The same applies to functions so I'm not far if I'm saying they're functions basically but you can write and use them in a few different ways compared to normal functions in the hope of better code quality.&lt;/p&gt;

&lt;h1&gt;
  
  
  First-class objects
&lt;/h1&gt;

&lt;p&gt;In programming, the term "First-class" means that you can assign these so-called first-class objects to variables, pass them as function parameters and return them as the result of function execution. This is not different from functions in Swift.&lt;br&gt;
Before you think you have to use the closure syntax in order to assign a function to a variable or pass it as a function parameter, I have to point it out that it's just not true. It's completely up to you which one you choose, normal functions or closures. In my opinion, one is not better than the other and it depends on what works the best for you. If it's easier for you to understand your code by using normal functions then go for it. But it's needless to say that having a decent knowledge around closures is very important, especially if you're not the only developer who's working on a project or you're about to go through a hiring process at your dream company. &lt;/p&gt;
&lt;h1&gt;
  
  
  The function type annotation
&lt;/h1&gt;

&lt;p&gt;"So I can juggle with functions as I do with Strings, Integers or other types. So far so good, but why would I do that?" Before jumping into the practical parts, let's see how you would write normal functions and closures in Swift.&lt;/p&gt;

&lt;p&gt;Since Swift is a strongly typed language, you have to add type annotations at some certain degree to your variables. Fortunately, Swift is smart enough to figure the type of a variable by simply the value you've assigned to it. This is the so-called Type Inference. But let's just be super explicit for now and optimize it later.&lt;/p&gt;

&lt;p&gt;In the following example, there's a normal function called &lt;code&gt;countLetters&lt;/code&gt; which accepts a String and returns an Integer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;countLetters&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;word&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;stillCountLetters&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;countLetters&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function &lt;code&gt;countLetters&lt;/code&gt; is assigned to a new variable &lt;code&gt;stillCountLetters&lt;/code&gt;. As we agreed, let's be explicit and set the type of &lt;code&gt;stillCountLetters&lt;/code&gt; which is &lt;code&gt;(String) -&amp;gt; Int&lt;/code&gt;. It means "Hey, I can store a pointer to a function that accepts a String &lt;code&gt;(String)&lt;/code&gt; and returns an Integer &lt;code&gt;-&amp;gt; Int&lt;/code&gt;". If the arity of a function is zero, which means it doesn't accept anything, you can write an opening and a closing parenthesis and nothing between the two &lt;code&gt;()&lt;/code&gt;. If your function returns nothing, you can use the &lt;code&gt;Void&lt;/code&gt; type after &lt;code&gt;-&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So in short, when a function accepts nothing and returns nothing, the type of this function looks this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;myFunc&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Void&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At the place of the function declaration, you can omit the type of the return value only if your function returns nothing. That's going to be an implicit &lt;code&gt;Void&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;printMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&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;instead of &lt;code&gt;Void&lt;/code&gt;, you can also use parentheses but for me, that's just too many of them and at the first glance, it confuses me with Tuples which doesn't make sense of course but still:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;myFunc&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;myFunc2&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  The closure syntax
&lt;/h1&gt;

&lt;p&gt;Alright, in the previous section, we created a normal function and assigned it to a variable. It's time to see how we can write closures.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;countLetters&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;word&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, the type is the same as it is for normal functions. Everything inside the curly brackets is the functionality belonging to the closure. Before the &lt;code&gt;in&lt;/code&gt; keyword, you can find the incoming values &lt;code&gt;(word: String)&lt;/code&gt; and the type of the return value &lt;code&gt;-&amp;gt; Int&lt;/code&gt;. Everything after &lt;code&gt;in&lt;/code&gt; is what your function does. What you would normally put between the brackets in case of a normal function.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why closures?
&lt;/h1&gt;

&lt;p&gt;At this point, you might be like "Ok, this is the closure syntax, fine! It's a little bit different from a normal function. Why do you want to confuse me, Apple?". Yeah, I hear you. It's like the "tabs versus spaces" war, isn't it?&lt;br&gt;
Well, it's not that simple and there are obvious reasons why Apple has introduced closures in Swift.&lt;/p&gt;

&lt;p&gt;The main reason behind the idea of closures is convenience. It provides a convenient way to work with functions. It's kind of like following the "Write less, do more" principle. At the beginning when your codebase consists of just a few lines of code it doesn't seem to make any difference. But later on when you're about to scale your application, you add some new features, bugs start to show up and you eventually end up maintaining a huge monster that is not so easy to debug anymore. Not to mention if you have to work with other developers on the project or even worse, you've inherited a big legacy codebase.&lt;/p&gt;

&lt;p&gt;When it comes to creating and maintaining a scalable and testable application which is &lt;a href="https://medium.freecodecamp.org/what-does-it-mean-when-code-is-easy-to-reason-about-4e6f63eb386f"&gt;easy to reason about&lt;/a&gt;, it's very common lately to follow the Functional Programming Paradigm in order to write code that meets the aforementioned requirements. Long story short, if FP is your thing and you heavily rely on functions, Swift provides a kind of like a more user-friendly way to deal with them. Period.&lt;/p&gt;

&lt;p&gt;In the following sections, I'll go through the cases when closures might come in handy in the future in general or if you decide to get on the Functional Programming bandwagon.&lt;/p&gt;
&lt;h1&gt;
  
  
  Omitting labels
&lt;/h1&gt;

&lt;p&gt;One of my favorite things is that it's not required to use labels when you invoke a closure with parameters. Moreover, it doesn't even allow you to use them at all. The following code will fail at compile time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;countLetters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;word&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; 
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;countLetters&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;word&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Raptors"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// error: extraneous argument label 'word:' in call ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You cannot override the labels for external usage either:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;countLetters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="nv"&gt;word&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; 
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;countLetters&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;of&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Raptors"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// error: closure cannot have keyword arguments&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't get me wrong. I see and understand the benefits of labels and after reading a few articles by &lt;a href="https://www.goodreads.com/book/show/3735293-clean-code"&gt;Robert Martin&lt;/a&gt;, I'd probably feel like I want to use them everywhere. But I'm not yet used to labels and I always tend to forget writing them when I call functions and they're not optional.&lt;/p&gt;

&lt;h1&gt;
  
  
  Write less, do more
&lt;/h1&gt;

&lt;p&gt;This section describes a few features available in Swift that help you write shorter code for the sake of readability but it can be dangerous too if you use them inappropriately.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Creating closures in place&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As I said earlier, functions are first-class citizens which means you can pass them as function parameters among others. And it's affordable with both normal functions and closures. Suppose you have a function to &lt;code&gt;add&lt;/code&gt; two numbers and another function called &lt;code&gt;combine&lt;/code&gt; that takes a function with which you can combine that two numbers in a certain way. For now, don't try to understand what the point is of passing functions to other functions. I'll describe it later.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;by&lt;/span&gt; &lt;span class="nv"&gt;function&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;by&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's totally fine to do this. We're extremely explicit here but it's still valid Swift.&lt;/p&gt;

&lt;p&gt;With closures, you can shorten the code above a bit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;by&lt;/span&gt; &lt;span class="nv"&gt;function&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;by&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="nv"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; 
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You cannot create normal functions at the place of the invocation. By using closures in our example, we could save 3 lines of code. It's not much but imagine a bigger module with hundreds of lines of code. By specifying how our &lt;code&gt;combine&lt;/code&gt; function should do the math, we improve readability so the developer doesn't have to jump to another place in the codebase to see what &lt;code&gt;add&lt;/code&gt; does when she's investigating our code. By doing this we can avoid "Change blindness":&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Change blindness is a perceptual phenomenon that occurs when a change in a visual stimulus is introduced and the observer does not notice it. For example, observers often fail to notice major differences introduced into an image while it flickers off and on again. People's poor ability to detect changes has been argued to reflect fundamental limitations of human attention.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;small&gt;from &lt;a href="https://en.wikipedia.org/wiki/Change_blindness"&gt;Wikipedia&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;And we can keep staying in the flow state:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In positive psychology, a flow state, also known colloquially as being in the zone, is the mental state of operation in which a person performing an activity is fully immersed in a feeling of energized focus, full involvement, and enjoyment in the process of the activity.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;small&gt;from &lt;a href="https://en.wikipedia.org/wiki/Flow_(psychology)"&gt;Wikipedia&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The power of Type Inference&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As I referred to it previously, If you write your code consistently, Swift is smart enough to figure the types automagically due to Type Inference. Having this in mind, we can do further enhancements.&lt;/p&gt;

&lt;p&gt;Since it's stated already in the &lt;code&gt;combine&lt;/code&gt; function that the return type of the &lt;code&gt;function&lt;/code&gt; parameter must be an Integer, we can omit the type of the return value of the closure when we pass it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;by&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="nv"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; 
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same applies to arguments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;by&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="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; 
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you really want, the code is short enough to write it in one line. But personally, I prefer spreading vertically rather than horizontally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;by&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="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Implicit returns&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;However we could reduce the size of our code to 1 line and 64 characters long (The recommended maximum length is 80 before putting the next line), it's still not the best in terms of readability. We can go further by taking advantage of implicit returns inside closures. If the body of your closure contains only one statement and you want to return the result of it, you can omit the &lt;code&gt;return&lt;/code&gt; keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;by&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="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Shorthand arguments&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Swift does even let you omit the arguments and use the shorthand version of them. The shorthand syntax is pointing to an item by using its index in the argument list starting from zero. You must prefix the index with a dollar sign. So in our case, the shorthand version of &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; is &lt;code&gt;$0&lt;/code&gt; and &lt;code&gt;$1&lt;/code&gt;. By using shorthand arguments you can also omit the &lt;code&gt;in&lt;/code&gt; keyword:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;by&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;$0&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alright, I think the time has come to point it out that however, it's a very fancy way of solving the problem, it really hurts readability. If we're just experimenting or just hacking something, it's fine. To be fair, even if I'm a less experienced Swift developer, I can presume that this closure adds two numbers. But don't forget that we can concatenate two strings together with the &lt;code&gt;+&lt;/code&gt; operator. It's not necessarily clear at first glance what the types are of the two parameters. If XCode is your preferred editor to write Swift, tooltips can come in handy to figure it out. When it comes to making a decision whether you should use shorthand arguments, especially if it's a very complex functionality, think twice, think of your co-workers and your future self.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Operator methods&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Still not enough?! Ah, you're killing me! Alright! If there's only one statement inside your closure and it's a basic arithmetic operation or comparing a value to another, it's enough to only write the operator and Swift does the rest for you:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;by&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;BONUS:&lt;/p&gt;

&lt;p&gt;Did you know that you don't even have to write anything at all??? Swift can read your mind, so the following code does work like a charm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;by&lt;/span&gt;&lt;span class="p"&gt;:)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just kidding. It doesn't. Sorry for my awkward sense of humor. :(&lt;/p&gt;

&lt;h1&gt;
  
  
  Trailing closures
&lt;/h1&gt;

&lt;p&gt;Hurray, we've finally arrived at the practical parts. So why would you use closures? Well, that's a very good question but better to ask yourself why would you use Functional Programming. Even if you're not even aware of that, probably you do FP pretty often if you're a full-time Swift developer.&lt;/p&gt;

&lt;p&gt;Suppose we have the following view. When the view appears on the screen, it shows a popup window with a greeting. Let's take a closer look inside the &lt;code&gt;viewDidAppear&lt;/code&gt; function where the popup window is created and told to be displayed. It creates an alert view and an action, adds the action to the alert and then opens the popup.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;UIKit&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;ViewController&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIViewController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;viewDidLoad&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;viewDidLoad&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;alertActionHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;action&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIAlertAction&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Great!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;alertCompletionHandler&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Completion feels good."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;viewDidAppear&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;animated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;alert&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIAlertController&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Greeting"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Hello, World!"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;preferredStyle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;action&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIAlertAction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"OK"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;alertActionHandler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addAction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;present&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;animated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;alertCompletionHandler&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;The third argument of the &lt;code&gt;UIAlertAction&lt;/code&gt; class accepts a closure called &lt;code&gt;handler&lt;/code&gt;. It's like "Alright, action! I'm giving you this function. Would you please so kind, and call it for me when the user hits the action button?".&lt;br&gt;
I'm 100% sure, you've already met these kinds of things and yes, it's Functional Programming. When you pass a function to another function and wait until a certain event happens (User clicks on the OK button in our case), this is the so-called callback design pattern in other languages like NodeJS. It may have a different name in Swift but the concept is the same. It's pretty powerful if you don't want your application to stop until the click event.&lt;/p&gt;

&lt;p&gt;As I said earlier, using closures is optional. It's just a convenient way to work with callbacks. If you don't like the idea, you can stay with normal functions. In the example above, I declared the &lt;code&gt;alertActionHandler&lt;/code&gt; callback as a member of the &lt;code&gt;ViewController&lt;/code&gt; class and passed it to &lt;code&gt;UIAlertAction&lt;/code&gt;. Just a quick side note, you can also declare the function inside &lt;code&gt;viewDidAppear&lt;/code&gt; method as a nested function. It's pretty powerful if you want to reach variables from the parent context but more on this later.&lt;/p&gt;

&lt;p&gt;This is a very good example to demonstrate the power of closures, more precisely the trailing closures. Let's refactor this code and see how it feels like after replacing the class methods with closures. Let's just not use a trailing closure fist.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;UIKit&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;ViewController&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIViewController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;viewDidLoad&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;viewDidLoad&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;alertCompletionHandler&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Completion feels good."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;viewDidAppear&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;animated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;alert&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIAlertController&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Greeting"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Hello, World!"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;preferredStyle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;action&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIAlertAction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"OK"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Great!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;

        &lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addAction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;present&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;animated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;alertCompletionHandler&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;In my opinion, this is much better because it doesn't grab the user out from the flow since we've declared the callback in place so she doesn't have to scroll up to the class method. The promise of the trailing closure syntax is easier readability so let's see how it looks like. To help you focus on the relevant parts, I've removed the rest of the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;action&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIAlertAction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"OK"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Great!"&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;Here's the thing. If and only if the last parameter of a given function is a closure, you're allowed to use the trailing closure syntax. As you can see, by using it, you can omit the label.&lt;br&gt;
I let you decide if it's easier to read compared to the previous example. If you prefer being more explicit and using labels, it's completely up to you.&lt;/p&gt;

&lt;p&gt;You might have noticed that there was another place in the code where I applied the callback pattern. The class's &lt;code&gt;present&lt;/code&gt; method accepts a closure as the last parameter so we can take advantage of the trailing closure syntax again to make the code a bit shorter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;UIKit&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;ViewController&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIViewController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;viewDidLoad&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;viewDidLoad&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;viewDidAppear&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;animated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;alert&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIAlertController&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Greeting"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Hello, World!"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;preferredStyle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;action&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIAlertAction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"OK"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Great!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addAction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;present&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;animated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Completion feels good."&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Capturing values
&lt;/h1&gt;

&lt;p&gt;You can also return a function from other functions. Remember? When you declare a function inside of another, it's a nested function. It can be very powerful if you want to store some state in the parent context (which is the function in which the nested function was created) based on certain inputs. The nested function has access to the variables in the parent context. When you call the function that returns a function, it's like "Ok I did what you wanted me to do, here's a function if you want to go further and do other stuff later (based on the stored state)."&lt;/p&gt;

&lt;p&gt;In the following code, there's an &lt;code&gt;addUser&lt;/code&gt; function with which you can add a user to the database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;addUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;removeUser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;removeUser&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;removeJohn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;addUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// ["John"]&lt;/span&gt;

&lt;span class="nf"&gt;removeJohn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// []&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's a bit silly example but straightforward enough to get the idea. &lt;code&gt;addUser&lt;/code&gt; is responsible for adding the given user name to the &lt;code&gt;users&lt;/code&gt; Set. It also returns a function with which you can remove the user you just added previously. You don't need to remember how the &lt;code&gt;addUser&lt;/code&gt; does its job. It doesn't matter what Collection type it uses behind the scene. If you want to add John, just call the function with the input "John", store the returned remove function which belongs to a parent context where John has been stored as a new user.&lt;/p&gt;

&lt;p&gt;We can say that &lt;code&gt;addUser&lt;/code&gt; is a nested function inside the global context. So even though I declared the &lt;code&gt;users&lt;/code&gt; Set outside from the function, it can "capture" that variable.&lt;/p&gt;

&lt;p&gt;Let's see how it looks like with closures:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;addUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&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;let&lt;/span&gt; &lt;span class="nv"&gt;removeJohn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;addUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;removeJohn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alright, that's all for now. I hope you have a better understanding of closures and find them useful. If not, that's fine too. At the end of the day, what matters is whether or not your App works. Next time I'm going to show you other dirty secrets of closures in Swift. If I missed something important or I was wrong, let me know in the comments section.&lt;/p&gt;

&lt;p&gt;Take care!&lt;/p&gt;

</description>
      <category>swift</category>
      <category>beginners</category>
      <category>challenge</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Atomic habits: Book review and cheat sheet</title>
      <dc:creator>Tamas Fodor</dc:creator>
      <pubDate>Mon, 25 Feb 2019 19:15:32 +0000</pubDate>
      <link>https://dev.to/ruffle1986/atomic-habits-book-review-and-cheat-sheet-1bh4</link>
      <guid>https://dev.to/ruffle1986/atomic-habits-book-review-and-cheat-sheet-1bh4</guid>
      <description>&lt;p&gt;If you read my &lt;a href="https://dev.to/ruffle1986/the-power-of-mini-habits-4ibp"&gt;previous post about mini habits&lt;/a&gt;, you know that I'm a firm believer in the benefits of habit development.&lt;/p&gt;

&lt;p&gt;One of my goals for this year is reading two books every month. So I developed a few mini habits to manage this task. If you're interested in the books I've read already this year, you can find the list &lt;a href="https://github.com/ruffle1986/books"&gt;here&lt;/a&gt;. I haven't decided on all the 24 books I'm planning to read so it's a constantly growing list. When I finish a book, I put it on the list.&lt;/p&gt;

&lt;p&gt;In the previous post, I gave a brief overview of why mini habits can change our lives significantly and now I want to take it to the next level. Well, it has turned out that there's an entire book on the topic out there. &lt;br&gt;
Ladies and gentlemen! Behold &lt;a href="https://amzn.to/2Th6Wwm"&gt;Atomic habits by James Clear&lt;/a&gt;, a number one bestselling book on Amazon about how tiny changes can have a huge effect on our every day lives especially if we'd like to achieve success.&lt;/p&gt;

&lt;p&gt;Dare I say, it's a great book and a must have on your bookshelf. Especially if you need a step by step guide to change your behaviors in order to build good habits and get rid of bad ones. The book also includes many scientific facts and stories from great habit builders in history which I've found very entertaining. The book touches many different scientific aspects including biology, neuroscience, and psychology. It's like Christmas for me.&lt;/p&gt;

&lt;p&gt;As Dan Lok, the famous entrepreneur and businessman once said, "Don't read books, use them!". Yes, reading is a very important part of this concept but you get the idea. So this time I've decided to provide a more useful post rather than a motivational speech. This article is about my thoughts on the book and also provides a cheat sheet and examples from my everyday life to make it easier for you to practice the ideas.&lt;/p&gt;
&lt;h1&gt;
  
  
  The 4 laws of behavior change
&lt;/h1&gt;

&lt;p&gt;The book is built around the four stages of the habit building process that you should follow. The author refers to it as the Four Laws of Behavior Change:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Make it obvious &lt;/li&gt;
&lt;li&gt;Make it attractive &lt;/li&gt;
&lt;li&gt;Make it easy &lt;/li&gt;
&lt;li&gt;Make it satisfying&lt;/li&gt;
&lt;/ol&gt;
&lt;h1&gt;
  
  
  Inversion
&lt;/h1&gt;

&lt;p&gt;Speaking of good habits, you might be satisfied with your life. Maybe, you just have some bad habits you want to get rid of like smoking or desperately checking social media to compare yourself to others. The inversion of the four laws comes to the rescue. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Make it invisible &lt;/li&gt;
&lt;li&gt;Make it unattractive &lt;/li&gt;
&lt;li&gt;Make it difficult &lt;/li&gt;
&lt;li&gt;Make it unsatisfying. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I really liked the idea of taking advantage of human nature and instead of focusing on putting a huge amount of energy to become a new next-level superhuman and get rid of what we are in our genes, make it serve us. Human nature comes with a lot of things that stop us from breaking bad habits. Our brain wants instant gratification and doesn't accept the delayed reward. The book lists many good techniques to turn these obstacles to our advantage.&lt;/p&gt;
&lt;h1&gt;
  
  
  Identity
&lt;/h1&gt;

&lt;p&gt;According to the book, the first step is coming up with the idea of the person who we want to become. When we’re aware of the identity of this person, it’s much easier to collect the habits which support us on the path of becoming that particular person. Otherwise, we're just hanging around probably. Just write them down on a paper or somewhere else. It’s easier to feel like you're committed.&lt;/p&gt;

&lt;p&gt;Here’s my list:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I’m a writer&lt;/li&gt;
&lt;li&gt;I’m a conference speaker&lt;/li&gt;
&lt;li&gt;I’m a teacher&lt;/li&gt;
&lt;li&gt;I’m a swift developer&lt;/li&gt;
&lt;li&gt;I’m a minimalist&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  Habits scorecard
&lt;/h1&gt;

&lt;p&gt;The idea of the habits scorecard is about collecting your current habits. The habits you do on a regular basis without failing. It’s very important to keep in mind that it’s a list of all of your habits, not just the good ones. It’s ok if you have some bad habits. Let's just accept it for now. &lt;br&gt;
Once you are done with the list, you should mark each habit based on whether it helps you to build your new identity, prevents you or just simply neutral. The book uses "+" for supportive habits, "-" for unsupportive habits and "=" for neutral habits. If you’re anything like me, you would make it look dope with emojis. &lt;/p&gt;

&lt;p&gt;✅ - supportive&lt;br&gt;
❌ - unsupportive&lt;br&gt;
🐒 - neutral&lt;/p&gt;

&lt;p&gt;Let me share my list with you. These are my daily habits. The habits I do every day.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;My daily morning routine:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Wake up 🐒&lt;br&gt;
Check emails and social media ❌&lt;br&gt;
Check my todos ✅&lt;br&gt;
Make my bed ✅&lt;br&gt;
Go to the kitchen 🐒&lt;br&gt;
Drink a big cup of tap water ✅&lt;br&gt;
Drink a cup of coffee ❌&lt;br&gt;
Go to the bathroom 🐒&lt;br&gt;
Turn on music ❌&lt;br&gt;
Brush my teeth ✅&lt;br&gt;
Wash my face ✅&lt;br&gt;
Get dressed 🐒&lt;br&gt;
Clean the surfaces from water and dust ✅&lt;br&gt;
Leave the bathroom 🐒&lt;br&gt;
Go to the living room 🐒&lt;br&gt;
Learn Swift for an hour ✅&lt;br&gt;
Take on my shoes 🐒&lt;br&gt;
Take on my jacket 🐒&lt;br&gt;
Go to the gym ✅&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Work (when I arrive):&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Make a cup of coffee ❌&lt;br&gt;
Have a breakfast ❌&lt;br&gt;
Tweet about my Swift learning progress ✅&lt;br&gt;
Watch a maximum 10-minute motivational video ✅&lt;br&gt;
Check work-related emails 🐒&lt;br&gt;
Check my sprint progress in a PM tool ✅&lt;br&gt;
Check social media ❌&lt;/p&gt;

&lt;p&gt;&lt;em&gt;My daily evening routine:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Go to the bathroom 🐒&lt;br&gt;
Turn on music ❌&lt;br&gt;
Have a shower ✅&lt;br&gt;
Wash my face ✅&lt;br&gt;
Brush my teeth ✅&lt;br&gt;
Put on deodorant ✅&lt;br&gt;
Take on my pajamas 🐒&lt;br&gt;
Leave the bathroom 🐒&lt;br&gt;
Pack my gym bag ✅&lt;br&gt;
Go to bed 🐒&lt;br&gt;
Check social media ❌&lt;br&gt;
Read a chapter ✅&lt;/p&gt;
&lt;h1&gt;
  
  
  Implementation Intention
&lt;/h1&gt;

&lt;p&gt;Based on several studies, we more likely stick with a habit if we attach it to a certain time and location. This is the so-called implementation intention. There’re a lot of other components of successfully sticking with a habit of course and it may not be the best for you. I know from experience that it’s very difficult to be on schedule if it’s not clarified. Use the formula and write it down.&lt;/p&gt;

&lt;p&gt;The formula is the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;I will [BEHAVIOR] at [TIME] in [LOCATION]

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

&lt;/div&gt;



&lt;p&gt;Here’s my list:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I will exercise for one hour at 6 a.m. in the gym&lt;/li&gt;
&lt;li&gt;I will learn Swift for one hour every day at 8 a.m. at my desk&lt;/li&gt;
&lt;li&gt;I will write an article every Sunday at 6 a.m. at my desk&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Habit stacking
&lt;/h1&gt;

&lt;p&gt;Another great way to form a new habit is by connecting it to an already existing one. If implementation intention is not your thing, this might work for you. You can also use the two together of course. &lt;br&gt;
Since we already have a list of our habits, we can easily find a good place for a new one. It’s better to connect the new habit to a good habit obviously because you probably want to get rid of the bad ones. It’s just a matter of time.&lt;/p&gt;

&lt;p&gt;Formula:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;After [CURRENT HABIT], I will [NEW HABIT]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Environment
&lt;/h1&gt;

&lt;p&gt;It happens very often that where we live or work prevents us from forming habits. It's not about the place but rather how we organize the things around us. Make your room your friend instead of your enemy. If you want to read every day, put a book on your pillow every morning. If you want to play on the guitar, put it in the middle of your living room, and so on.&lt;/p&gt;

&lt;p&gt;I think I’m mostly fine with this part but there are always rooms to improve. For example, I work on my sleep quality and I don’t have a television in my bedroom but I read books on my phone before sleep time. I use the Twilight app to filter the blue light but it’s not just about that. I tend to check social media between chapters. I have a Kindle but I don’t have a reading lamp near my bed so I think it’s time to buy one. :)&lt;/p&gt;

&lt;h1&gt;
  
  
  The Two-minute rule
&lt;/h1&gt;

&lt;p&gt;It's about procrastination. As I pointed it out earlier many times and basically this is the whole idea behind atomic habits, we tend to give up because the expectations are too high. Go small! At first, create habits that require only &lt;strong&gt;&lt;em&gt;two minutes&lt;/em&gt;&lt;/strong&gt; to get them done.&lt;/p&gt;

&lt;p&gt;If it's easier for you to use some tools to figure your own plans, you can find many resources and templates on the official website of the book.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://s3.amazonaws.com/jamesclear/Atomic+Habits/Habits+Cheat+Sheet.pdf"&gt;The Habits Cheat Sheet&lt;/a&gt;&lt;br&gt;
&lt;a href="https://s3.amazonaws.com/jamesclear/Atomic+Habits/The+Habits+Scorecard.pdf"&gt;Habits Scorecard Template&lt;/a&gt;&lt;br&gt;
&lt;a href="https://s3.amazonaws.com/jamesclear/Atomic+Habits/Implementation+Intentions.pdf"&gt;Implementation Intentions Template&lt;/a&gt;&lt;br&gt;
&lt;a href="https://s3.amazonaws.com/jamesclear/Atomic+Habits/Habit+Tracker.pdf"&gt;Habit Stacking Template&lt;/a&gt;&lt;br&gt;
&lt;a href="https://s3.amazonaws.com/jamesclear/Atomic+Habits/Habit+Tracker.pdf"&gt;Habit Tracker template&lt;/a&gt;&lt;br&gt;
&lt;a href="https://s3.amazonaws.com/jamesclear/Atomic+Habits/Habit+Contract.pdf"&gt;Habit Contract template and example&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I highly recommend you to get this book. For me, it was worth every penny and I'm 100 % sure that I'll read it again to get some motivation on those sad rainy days when I feel stuck. &lt;/p&gt;

&lt;p&gt;Remember! Don't just read it. Use it!&lt;/p&gt;

</description>
      <category>motivation</category>
      <category>career</category>
      <category>productivity</category>
      <category>habits</category>
    </item>
    <item>
      <title>The power of mini habits</title>
      <dc:creator>Tamas Fodor</dc:creator>
      <pubDate>Sun, 17 Feb 2019 17:23:35 +0000</pubDate>
      <link>https://dev.to/ruffle1986/the-power-of-mini-habits-4ibp</link>
      <guid>https://dev.to/ruffle1986/the-power-of-mini-habits-4ibp</guid>
      <description>&lt;p&gt;Hi! I'm Tamas. 32 years old web developer located in Budapest, Hungary. Let me have the chance to use this channel and introduce myself to the dev community.&lt;br&gt;
I'm a procrastinator. A very kind way of calling myself a loser. And definitely not a finisher. You may find it inappropriate or offensive. But that's a fact and here I'm to confess.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why am I calling myself a loser?
&lt;/h1&gt;

&lt;p&gt;I have dreams to pursue. We all do. And when it comes to pursuing our dreams we all have excuses to convince ourselves why it's ok not taking an action to get closer, why it's ok to do it next day instead of immediately. "I don't have time." "I have a family to take care of". "I have a full-time job, I'm very tired." You know all the forms of these bastards I'm sure.&lt;br&gt;
I don't know you but if you're fine with that, that's ok. But I'm sick and tired of it. So I've eventually decided to take the next step to accomplish my goals.&lt;/p&gt;

&lt;p&gt;I've always wanted to write. To spread my ideas with the World. I've been planning to finally start it for years. Many years, almost a decade. Geez!&lt;br&gt;
But why is that? If this is one of my biggest dreams, why is it so hard to do? If I want to do that, it must be easy, joyful and super cool, right?&lt;br&gt;
Well, it's not that easy and that's ok. &lt;br&gt;
"You're a coder. You're just not good enough at writing" I told myself. Actually, I was pretty good at writing in my student years according to the grades I got from my teachers. so nice try, Tamas, but it wouldn't work this time. "Alright, but I'd like to write in English to target a bigger audience and English is not my primary language". Hmm, getting better. &lt;/p&gt;

&lt;p&gt;See? I'm the ultimate master of finding a good excuse to not do anything to reach my goals.&lt;/p&gt;

&lt;p&gt;It must be the willpower I thought. Well, yes and no. It's a more complex problem including many components. Partially it's the willpower, yes. But it's a huge topic. And in order to deal with big issues effectively, we have to split it down to smaller chunks, right?&lt;br&gt;
Before diving deep into this, let me talk about how I get here and realize the source of all evil.&lt;/p&gt;

&lt;p&gt;I've always been a book guy and as many of us who want to become a better person, I've read a shit load of books on self-discipline, forming good habits and such things. To be honest, at first, I desperately tried to find a secret formula, a secret mantra or pill with which I could immediately get where I wanted to be. Yeah, right. Guess what? There weren't any but I didn't give up reading and many books later I finally got to a certain point where everything started to become clearer and made more sense.&lt;/p&gt;

&lt;p&gt;I'm not a big fan of new year's resolutions because I think it's a huge lie and just another excuse to procrastinate. &lt;br&gt;
"Alright, This is the last chocolate cake and it's over. From January, I won't eat any of this shit I swear. My grandma put a lot of effort to bake it to me anyways." Familiar? But for some reasons, it gives people a huge amount of motivation to achieve something great if they can attach the starting point to a certain event. To start it at least. Doing it consistently? Well, that's another question we all know it. It was the same with me of course but it happened to finally press the red button a few days later after New year's eve. So basically I took an action immediately right? &lt;/p&gt;

&lt;h1&gt;
  
  
  The power of habits
&lt;/h1&gt;

&lt;p&gt;I'm sorry for the emotional start but now let's start talking about the solutions. So as a good first step I googled for the best possible books on willpower out there. I found a few promising books but also, a lot of books about habits came along the way. I remembered that I had heard about the positive effects of building good habits but I had never really dived deep into the topic. "The seven habits of highly successful people". "You have to wake up 4 am every single day, or even better 3 or 2 am" and stuff like that. I stopped being sarcastic immediately, kept focusing and pick a book.&lt;/p&gt;

&lt;p&gt;The first book of my choice was &lt;a href="https://amzn.to/2EfkReb"&gt;Habit stacking by S. J. Scott&lt;/a&gt;. It's not the most popular book on habits but after reading it I realized that it borrowed lots of great ideas from other books like the famous Power of habits by Charles Duhigg and many others. So essentially it was a good start to warm up. &lt;/p&gt;

&lt;p&gt;In my opinion, it's a really good book and had a huge effect on me. It tells you the basics of habit building and also comes with a huge list of habits to start with in case you're not that creative to come up with your own. &lt;/p&gt;

&lt;p&gt;And at this point, I'd like to grab the chance to highlight the most powerful idea included by the book. Mini habits. It was the one that gave me the fuel to take the necessary actions immediately because this fundamental made everything so easy and straightforward.&lt;/p&gt;

&lt;p&gt;As a programmer, I like keeping things super simple so I needed something to make it easier because it's obvious that building good habits makes you a better person. But it's very hard to build them. This is how our brain works. It's very difficult to be consistent and follow our new routines every single day until it really becomes our own and we do it without being aware of it. And if you're like me, you want to grab the best habits all at once to take it to the next level immediately right? Well... Not so fast.&lt;/p&gt;

&lt;h1&gt;
  
  
  Mini habits
&lt;/h1&gt;

&lt;p&gt;It won't work so you better forget it. But that is exactly the point of mini habits. To make it super simple. I love them! Suppose we have a big goal like losing weight,  learning a new language or writing blog posts on a regular basis. These are the goals. In order to reach them, we have to build habits to move forward because it's impossible to accomplish them all of a sudden.&lt;br&gt;
But we tend to go big, especially when we're full of motivation at the early stage of our journey. Let's settle at the losing weight analogy. However, writing is my biggest goals for the year, I wanted to start with something easier. I love sports but I tend to do it inappropriately so I decided to practice habit stacking connected to working out.  &lt;/p&gt;

&lt;p&gt;So we go to the gym every single day of the week and do exercises for hours. We got all the good advice about weight lifting, cardio, and stretching. We want to do them all at once. What could go wrong?  The bigger the effort and the more the work we do, the sooner we get there, right?&lt;br&gt;
This is the main reason why we give up after a few weeks or a month. The expectations are so big, we feel overwhelmed and lose all the motivation. Eventually, we stop doing the hard work no matter how much effort we put in. &lt;/p&gt;

&lt;p&gt;Stop focusing on the results. Focus on the process which is habit building. Who told us we had to train like Arnold Schwarzenegger. We can of course but it's stupid to start like that. Want to get that beach body? Alright! Let's add those mini habits. Here's mine. &lt;/p&gt;

&lt;p&gt;I decided to go to the gym in the morning. Dare I say it's more difficult. So I told myself. Ok, Tamas. Just wake up! Go to the bathroom. Wash your teeth! Wash your face! Take your clothes on! Grab your bag and get out of the house. I have to mention another mini support habit here I've found super useful. Usually, I pack my bag the night before so I don't have to mess with that in the morning. &lt;br&gt;
So the first day I went to the gym. Ran 10 for minutes on the treadmill. Did a few pushups and got familiar with the machines and the tools. Especially with those tools, I use for leg exercises because I had planned a leg day for the next time. And that is all! The next day I did exactly the same. Went to the gym, ran 10 minutes on the treadmill and did a few squats. &lt;br&gt;
See? It's that simple. The point wasn't training like a spartan warrior. The point was taking my ass to the gym and start "something" at 8 am on a consistent basis. &lt;br&gt;
It was very difficult at the beginning but it was getting easier and easier every day. Next week, I started to change the habits a little bit. I woke up a bit earlier and increase the number of exercises after the 10-minute treadmill. &lt;br&gt;
The result? Priceless. It's not necessary to mention but I got to the point when it was super easy and I could not even imagine my mornings without the training. It's even better when you start to see the results. Now I do this morning routine in autopilot mode. When I have to skip, it hurts.&lt;/p&gt;

&lt;p&gt;The same with writing. I don't have to write a book with 200,000 words in one day. First, just decide what you want to write about. Give it a title. Pick a day and write your thoughts down. Don't give up half-way! Finish it. Once it's done, it's great. Do the same a week later. Try to write a blog post every week. Just one! It's going to be easier. You're going to be better at writing. When you get bored and it's not a challenge anymore, increase the number of posts and write two each week instead of one.&lt;/p&gt;

&lt;p&gt;I'm hoping you got the point of mini habits. Take these tiny steel lego blocks and start putting together your routines. Remember! Take it easy and once it's super-solid, add more or take the existing ones to the next level. Use them mindfully and be consistent! These are the foundation of your future greatness.&lt;/p&gt;

&lt;p&gt;Happy habit stacking!&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>habits</category>
      <category>career</category>
      <category>motivation</category>
    </item>
  </channel>
</rss>
