<?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: Georg Nikodym</title>
    <description>The latest articles on DEV Community by Georg Nikodym (@mmi).</description>
    <link>https://dev.to/mmi</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%2F326908%2Fefe1cebe-07a7-4515-add7-42d7f576cc3f.jpeg</url>
      <title>DEV Community: Georg Nikodym</title>
      <link>https://dev.to/mmi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mmi"/>
    <language>en</language>
    <item>
      <title>C Functions Calling Other Functions With Side Effects</title>
      <dc:creator>Georg Nikodym</dc:creator>
      <pubDate>Wed, 23 Sep 2020 21:44:59 +0000</pubDate>
      <link>https://dev.to/mmi/c-functions-calling-other-functions-with-side-effects-297g</link>
      <guid>https://dev.to/mmi/c-functions-calling-other-functions-with-side-effects-297g</guid>
      <description>&lt;p&gt;Recently, I've been working on a project and seeing a lot of this pattern:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void func(void) {
  retVal = thing1WithSideEffects();
  if (0 == retVal) thing2WithSideEffects();
  if (0 == retVal) thing3WithSideEffects()
  if (0 == retVal) thing4WithSideEffects();

  if (0 != retVal) return retVal;

  // do other func()y stuff
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;By side effect, I mean a change in the state of the system. The side effect might be a new open file, a memory allocation, the establishment of a TCP connection, whatever.&lt;/p&gt;

&lt;p&gt;First, the style. The tests of retVal are actually checking things from the line above so reading this code is weird.&lt;br&gt;
While I'm nitpicking, I hate "Yoda Conditions".&lt;/p&gt;

&lt;p&gt;Way more importantly, however, is that if any one of the calls fails, the system is left in an indeterminate state. If retVal has a value by line 7, we have no idea what the failure was and we cannot call any cleanup functions to undo any of the side effects.&lt;/p&gt;

&lt;p&gt;The calling program/function, detecting an error with &lt;code&gt;func()&lt;/code&gt; cannot call &lt;code&gt;func()&lt;/code&gt; again unless &lt;em&gt;it&lt;/em&gt; knows how to clean things up.&lt;/p&gt;

&lt;p&gt;Yes, getting this right is hard since you end up with something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void func(void)
{
  if (retVal = thing1WithSideEffects()) return retVal;
  if (retVal = thing2WithSideEffects()) {
    undoThing1();
    return retVal;
  }
  if (retVal = thing3WithSideEffects()) {
    undoThing2();
    undoThing1();
    return retVal;
  }
  // ad nauseum -- you get the idea
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;A little bit gross (clean-up is beyond the scope of what I'm trying to get across) and hard to get-right/test.&lt;/p&gt;

&lt;p&gt;I totally understand not wanting to do all this when you’re just under deadline, trying to get stuff working and out the door but the code we write tends to outlive us.&lt;/p&gt;

&lt;p&gt;But we can and should do better.&lt;/p&gt;

</description>
      <category>c</category>
    </item>
  </channel>
</rss>
