<?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: Oscar Ablinger</title>
    <description>The latest articles on DEV Community by Oscar Ablinger (@oscarablinger).</description>
    <link>https://dev.to/oscarablinger</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%2F653430%2Fd16b8a0f-d863-4665-9646-93271333b36a.jpeg</url>
      <title>DEV Community: Oscar Ablinger</title>
      <link>https://dev.to/oscarablinger</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/oscarablinger"/>
    <language>en</language>
    <item>
      <title>The Objectively Best Way to Indent your Code</title>
      <dc:creator>Oscar Ablinger</dc:creator>
      <pubDate>Fri, 01 Apr 2022 16:33:35 +0000</pubDate>
      <link>https://dev.to/oscarablinger/the-objectively-best-way-to-indent-your-code-1ece</link>
      <guid>https://dev.to/oscarablinger/the-objectively-best-way-to-indent-your-code-1ece</guid>
      <description>&lt;p&gt;Tabs or spaces?&lt;/p&gt;

&lt;p&gt;Every programmer was at some point confronted by this career-defining choice. But with this post today I'll expand the horizon and finally settle the debate.&lt;/p&gt;

&lt;p&gt;So let's go over the different ways to indent your code and their pros and cons.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;All of the pretty code images here were made using &lt;a href="//carbon.now.sh"&gt;carbon&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Tabs
&lt;/h2&gt;

&lt;p&gt;Indenting as ASCII intended.&lt;br&gt;
The pros of this technique are plenty:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every reader of the code can decide how much space it wants the IDE to attribute to one tab: 1, 5 or even 7 (sadly fractions are not supported by most).&lt;/li&gt;
&lt;li&gt;The resulting file size is smaller, ensuring that you can stay as productive as possible.&lt;/li&gt;
&lt;li&gt;You feel good giving purpose to the 10th ASCII character.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BB8_ETj4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ptntdums7e9w6se34ti.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BB8_ETj4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ptntdums7e9w6se34ti.png" alt="An image of code using tabs" width="880" height="461"&gt;&lt;/a&gt;&lt;em&gt;An image of code with tabs.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Spaces
&lt;/h2&gt;

&lt;p&gt;Ah, the default for most editors.&lt;br&gt;
And also slightly more common than tabs in my objective experience.&lt;/p&gt;

&lt;p&gt;The main advantage of this style is that you can force the reader of your code to use the same length of indentation that you used.&lt;br&gt;
I know I listed the opposite point as an advantage of using tabs, but we all know that in the end you cannot trust users – not even the users of an IDE.&lt;/p&gt;

&lt;p&gt;Developers new to the discussion often bring up that this style requires you to press the space bar repeatedly.&lt;br&gt;
But of course noone actually does that – that's what auto-clickers are for.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--g3cvyXyr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rssglzomsongq4o2by2d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g3cvyXyr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rssglzomsongq4o2by2d.png" alt="An image of code using spaces." width="880" height="461"&gt;&lt;/a&gt;&lt;em&gt;An image of code with spaces.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Mixing tabs and spaces
&lt;/h2&gt;

&lt;p&gt;Okay, hear me out: You can use tabs and spaces for different kinds of indentation.&lt;/p&gt;

&lt;p&gt;For instance you could use tabs to indent blocks and then use spaces when you need something at a specific position, like when aligning the second parameter with the first.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0Gy5Df0e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jbitic51xboh0gcfl7v0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0Gy5Df0e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jbitic51xboh0gcfl7v0.png" alt="An image of code using tabs and spaces." width="880" height="423"&gt;&lt;/a&gt;&lt;em&gt;An image of code with tabs and spaces.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This allows other people to still choose their own length for the tabs, but ensure that elements are still aligned when you want them to be.&lt;/p&gt;

&lt;p&gt;Personally I also advocate to use tabs instead of spaces in between parameters, because that will create more visually pleasing code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rJoEZEWF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zr69rl7lsqe9julwazlk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rJoEZEWF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zr69rl7lsqe9julwazlk.png" alt="An image of code using tabs between parameters." width="880" height="338"&gt;&lt;/a&gt;&lt;em&gt;An image of code with tabs between parameters.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Mixing and mashing
&lt;/h2&gt;

&lt;p&gt;For those that hate to be restricted by style guides and want to rebel against the bourgeoisie of coding, you can just do whatever you want and sometimes use tabs and sometimes use spaces.&lt;/p&gt;

&lt;p&gt;This might seem weird, but if you look at the first projects of software engineers, you'll find that this is pretty common before they learn otherwise.&lt;/p&gt;

&lt;p&gt;So it might just be the most natural of all of the indenting methods.&lt;/p&gt;

&lt;p&gt;Plus, it turns programming into a small game, when, in every line, you'll wonder whether spaces or tabs were used.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://link.springer.com/article/10.1007/s10648-019-09498-w"&gt;And gamification improves cognitive, motivational and behavioral learning&lt;/a&gt;, so this method will also make you a better programmer in the long run.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--evDRFY66--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/plb74cn0gr7nqr5ifg3n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--evDRFY66--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/plb74cn0gr7nqr5ifg3n.png" alt="An image of code with one line using a tab and the other using spaces. Can you guess which is which?" width="880" height="439"&gt;&lt;/a&gt;&lt;em&gt;An image of code with one line using a tab and the other using spaces. Can you guess which is which?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Lastly, you can actually fit two programs into one: Since the whitespace is ignored by most programing languages, you can use them &lt;a href="https://esolangs.org/wiki/Whitespace"&gt;to implement a separate program&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That makes for a great easter egg!&lt;/p&gt;

&lt;h2&gt;
  
  
  Comments
&lt;/h2&gt;

&lt;p&gt;Instead of adding another program, you could also just try to use the space more efficiently.&lt;br&gt;
Since you'll likely decide to indent anyways, why let that space go to waste?&lt;/p&gt;

&lt;p&gt;You can use block comments to add more useful information about what's happening in each line.&lt;br&gt;
That's especially useful for beginners so you don't forget to comment your code.&lt;/p&gt;

&lt;p&gt;Since you don't want to use up too much space with full text comments all the time, I'd recommend using some well-defined short names.&lt;/p&gt;

&lt;p&gt;Here are some suggestions by me:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Short Name&lt;/th&gt;
&lt;th&gt;What it describes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;=&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Variable assignment&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;^&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Function call&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;↑&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;stdout output&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;⏎&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Return statement&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;End of a block&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;π&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;π&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;∫&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Function definition&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;λ&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Lambda definition&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--u8Rgw1dE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jy9fj4v3ulcq290nswsk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--u8Rgw1dE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jy9fj4v3ulcq290nswsk.png" alt="An image of code with indentation comments." width="880" height="405"&gt;&lt;/a&gt;&lt;em&gt;An image of code with indentation comments.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This also increases searchability of your code since you can just search for "⏎" and find all of the returns in the file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Semicolons
&lt;/h2&gt;

&lt;p&gt;More and more language now move to a cleaner no-semicolons-required style.&lt;/p&gt;

&lt;p&gt;Sadly some languages still haven't caught up.&lt;/p&gt;

&lt;p&gt;But when you're using semicolons as indentation, you'll never have to worry about seeing one at the end of your statement ever again.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZzJN4UkO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/plhn3i66rv3rzgzt2lyv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZzJN4UkO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/plhn3i66rv3rzgzt2lyv.png" alt="An image of code with semicolons as indentation." width="880" height="439"&gt;&lt;/a&gt;&lt;em&gt;An image of code with semicolons as indentation.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Sadly some languages set (in my opinion) unreasonable restrictions on code when using this indentation style.&lt;/p&gt;

&lt;p&gt;Java for instance will only allow you to use one indentation level for the line after a &lt;code&gt;return&lt;/code&gt; statement.&lt;br&gt;
Otherwise it will complain about "unreachable code".&lt;/p&gt;

&lt;p&gt;In those cases I'd recommend using a pre-compiler job that runs all of the files through a simple &lt;code&gt;sed -r "s/;+/;/g"&lt;/code&gt; call, which will fix some of the errors.&lt;/p&gt;

&lt;p&gt;Most languages also won't let you chain calls over multiple lines anymore.&lt;/p&gt;

&lt;p&gt;I do see this as an absolute win, however, since I believe that no method should consist of more than one statement, anyways.&lt;br&gt;
That's usually a code smell.&lt;/p&gt;

&lt;p&gt;Other languages will outright refuse to compile code indented like this completely.&lt;/p&gt;

&lt;p&gt;While I don't believe that languages should unnecessarily restrict programmers, we're sometimes forced to work with such archaic languages.&lt;br&gt;
In that case, you could try to write a simple pre-compiler, publish it and then never maintain it for 3 years while racking up over a hundred thousand weekly downloads on NPM.&lt;/p&gt;

&lt;h2&gt;
  
  
  Right-to-left indentation
&lt;/h2&gt;

&lt;p&gt;With i18n becoming more and more important, more any more programs introduce a RTL mode in which the text changes to be aligned from right-to-left.&lt;/p&gt;

&lt;p&gt;Maybe it's time to also make that move in the code itself and &lt;a href="https://www.reddit.com/r/ProgrammerHumor/comments/svf9qx/okay_lets_end_this_tabs_vs_space_debate_once_and/"&gt;indent to the right side instead&lt;/a&gt;?&lt;/p&gt;

&lt;p&gt;The main advantage of this is that the maximum line width now becomes more than a suggestion and actually has to be respected.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0RGXfuA8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ijjrfqdewoo8v3cawpp3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0RGXfuA8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ijjrfqdewoo8v3cawpp3.png" alt="An image of code with RTL indentation." width="880" height="478"&gt;&lt;/a&gt;&lt;em&gt;An image of code with RTL indentation.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  No Indentation
&lt;/h2&gt;

&lt;p&gt;An often overlooked way of indentation is simply not indenting.&lt;/p&gt;

&lt;p&gt;Has anyone ever checked if indentation actually improves readability? I know I haven't.&lt;/p&gt;

&lt;p&gt;In fact, having no indentation forces you to read every line and ensures that you don't miss anything, reducing errors in the process.&lt;/p&gt;

&lt;p&gt;Of course, we also save precious disk space.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--twxRbigz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lr3h763o98p5wm7s837s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--twxRbigz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lr3h763o98p5wm7s837s.png" alt="An image of code with no indentation." width="880" height="470"&gt;&lt;/a&gt;&lt;em&gt;An image of code with no indentation.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Blank space
&lt;/h2&gt;

&lt;p&gt;What's better than no indentation?&lt;br&gt;
&lt;em&gt;Nothing!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Blank space is the &lt;code&gt;null&lt;/code&gt; to your empty list.&lt;/p&gt;

&lt;p&gt;While most editors don't yet support this style, the up-and-coming OSS editor &lt;a href="https://ms-paint-i.de/"&gt;MEPaintIDE&lt;/a&gt; is pioneering this brand new way of styling your code.&lt;/p&gt;

&lt;p&gt;Instead of doing the cumbersome, arduous, laborious work of inserting characters, you &lt;a href="https://www.youtube.com/watch?v=A_pTGhOyPbw"&gt;should take your code and push it somewhere else&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--W1YrofLt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n6fuehjdbv0vaqtvdont.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W1YrofLt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n6fuehjdbv0vaqtvdont.png" alt="Code indented using blank space." width="533" height="279"&gt;&lt;/a&gt;&lt;em&gt;Code indented using blank space. &lt;a href="https://www.youtube.com/watch?v=e-ORhEE9VVg"&gt;I got a blank space, baby.&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;I think after this in-depth analysis we can agree that Paint is the best tool for beautiful indentation.&lt;/p&gt;

&lt;p&gt;And I hope I could convince you that, at the very least, you shouldn't let people choose their indentation completely freely.&lt;/p&gt;

</description>
      <category>java</category>
      <category>styleguide</category>
      <category>indentation</category>
    </item>
    <item>
      <title>What are some good email providers?</title>
      <dc:creator>Oscar Ablinger</dc:creator>
      <pubDate>Fri, 29 Oct 2021 23:08:27 +0000</pubDate>
      <link>https://dev.to/oscarablinger/what-are-some-good-email-providers-24p4</link>
      <guid>https://dev.to/oscarablinger/what-are-some-good-email-providers-24p4</guid>
      <description>&lt;p&gt;So, I've got &lt;a href="https://oscarablinger.dev"&gt;my own domain&lt;/a&gt; now. Does anyone have a recommendation for some email providers that give me that sweet …&lt;a class="mentioned-user" href="https://dev.to/oscarablinger"&gt;@oscarablinger&lt;/a&gt;.com email address?&lt;/p&gt;

&lt;p&gt;A catch-all email would be pretty important to me – other than that only maybe that I can export my emails easily so I don't loose stuff when I'm not happy with it.&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>You already know Monads</title>
      <dc:creator>Oscar Ablinger</dc:creator>
      <pubDate>Mon, 21 Jun 2021 19:54:06 +0000</pubDate>
      <link>https://dev.to/oscarablinger/you-already-know-monads-4gk5</link>
      <guid>https://dev.to/oscarablinger/you-already-know-monads-4gk5</guid>
      <description>&lt;p&gt;Instead of explaining the abstract theory behind monads, I’ll just show you some Java code that you already understand. And using this practical approach you’ll realize that you’ve in all likelihood already used monads a few times.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/w0fhQCzy2g0?t=1308"&gt;Oh and if you prefer to listen to me instead, this post is based on a lightning talk I gave, where I (try to) explain them in 7 minutes.&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Monads are just classes that implement the following two methods:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#of-T...-"&gt;&lt;code&gt;of&lt;/code&gt;&lt;/a&gt; (in explanations usually called &lt;code&gt;unit&lt;/code&gt; or &lt;code&gt;return&lt;/code&gt;) and&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#flatMap-java.util.function.Function-"&gt;&lt;code&gt;flatMap&lt;/code&gt;&lt;/a&gt; (usually called &lt;code&gt;bind&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;of&lt;/code&gt; should simply take some values and return a wrapper for it (the monad). &lt;code&gt;flatMap&lt;/code&gt; then allows accessing those values by applying a function to all of them, taking the monads returned by those functions and combining (aka “flattening”) them into one again.&lt;/p&gt;

&lt;p&gt;Using that, the simplest possible monad is the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Monad&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;Monad&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Monad&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Monad&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;R&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Monad&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;R&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;flatMap&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Monad&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;R&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  So that’s it?
&lt;/h2&gt;

&lt;p&gt;Mostly, but obviously you can’t just name the methods a certain way and have a monad. There are rules for how these two methods need to work together.&lt;/p&gt;

&lt;p&gt;The good news is that if you implement those methods as described above (and as they are commonly understood), you’ll already fulfill all of them. But here they are anyways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The of method is a left-identity for &lt;code&gt;flatMap&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;the of method is a right-identity for &lt;code&gt;flatMap&lt;/code&gt; and&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;flatMap&lt;/code&gt; method is associative.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Or, in code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// using&lt;/span&gt;
&lt;span class="nc"&gt;Monad&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;V&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="no"&gt;V&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;V&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Monad&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;V&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;V&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Monad&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;V&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// rule 1: of is left-identity of flatMap&lt;/span&gt;
&lt;span class="n"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;flatMap&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;

&lt;span class="c1"&gt;// rule 2&lt;/span&gt;
&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;flatMap&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;Monad:&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;of&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// rule 3&lt;/span&gt;
&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;flatMap&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;flatMap&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;flatMap&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;flatmap&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="o"&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Why should I care?
&lt;/h1&gt;

&lt;p&gt;Now you know what monads are and how you can create one, but why is there even a name for it?&lt;/p&gt;

&lt;p&gt;If you were using a heavily functional language, then this wouldn’t even really be a question since the tools and problems you’ll have when programming functionally will likely naturally result in you creating some monads¹. That’s where monads are from originally.&lt;/p&gt;

&lt;p&gt;That being said, they can also be tremendously useful outside of those languages and in fact many new APIs feature monads.&lt;/p&gt;

&lt;h2&gt;
  
  
  Include (error) states in your domain
&lt;/h2&gt;

&lt;p&gt;The best APIs try to stop you from making mistakes. One of the ways that this is done is explicitly including the error state in its return values. When multiple function calls in a row could return an error, this also has the added bonus of making the resulting code prettier.&lt;/p&gt;

&lt;p&gt;Let’s look at an example using Optional:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Stream&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;findFirst&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;flatMap&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;integerHalf&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;flatMap&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;integerHalf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;empty&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we not only had the option to return an “error” at any time, but afterwards we don’t immediately have to check for it. Instead this can be done at a later point.&lt;/p&gt;

&lt;p&gt;Monads can also be used to take care of non-error states. This is often used in collections or asynchronous programming (see &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html"&gt;CompletableFuture&lt;/a&gt;), where it also prevents callback-hell.&lt;/p&gt;

&lt;h2&gt;
  
  
  Allow the use of more generic functions
&lt;/h2&gt;

&lt;p&gt;Take the above function integerHalf for example: We can use this on plain integers and also on any monad like Optional or Stream using the flatMap method.&lt;/p&gt;

&lt;p&gt;This ease of access to the underlying information while not leaving the domain of the Monad can create amazing APIs that prevent many errors.&lt;/p&gt;

&lt;h1&gt;
  
  
  Do you know some monads?
&lt;/h1&gt;

&lt;p&gt;I’ve already mentioned the three most common monads in Java:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Optional&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Stream&lt;/code&gt; and&lt;/li&gt;
&lt;li&gt;&lt;code&gt;CompletableFuture&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;but I’m sure you’ve encountered a few more already. Leave a comment if you know another good example.&lt;/p&gt;

&lt;h1&gt;
  
  
  Further links
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html"&gt;Maybe the best explanation of the theory behind monads (and functors and applicatives) — with pictures!&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;¹&lt;a href="https://stackoverflow.com/questions/28139259/why-do-we-need-monads/28139260#28139260"&gt;A great explanation of why monads naturally arise when programming functionally&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=w0fhQCzy2g0&amp;amp;t=1308s"&gt;My 7-minute lightning talk about this topic&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;P.s.: The title image is a photo by &lt;a href="https://burst.shopify.com/@ndekhors"&gt;Nicole De Khors&lt;/a&gt; and was chosen because monads are often described as boxes since they simply wrap some stuff.&lt;/p&gt;

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