<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>code Archives | Clever Cloud</title>
	<atom:link href="https://www.clever.cloud/blog/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.clever.cloud/blog/tag/code/</link>
	<description>From Code to Product</description>
	<lastBuildDate>Wed, 12 Oct 2016 16:56:00 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://cdn.clever-cloud.com/uploads/2023/03/cropped-cropped-favicon-32x32.png</url>
	<title>code Archives | Clever Cloud</title>
	<link>https://www.clever.cloud/blog/tag/code/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Intro to Clever Cloud-Driven Development</title>
		<link>https://www.clever.cloud/blog/engineering/2016/10/12/clever-cloud-driven-developement/</link>
		
		<dc:creator><![CDATA[Clément Nivolle]]></dc:creator>
		<pubDate>Wed, 12 Oct 2016 16:56:00 +0000</pubDate>
				<category><![CDATA[Engineering]]></category>
		<category><![CDATA[application]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Developers]]></category>
		<category><![CDATA[management]]></category>
		<category><![CDATA[productivity]]></category>
		<category><![CDATA[service]]></category>
		<category><![CDATA[workflow]]></category>
		<guid isPermaLink="false">https://www2.cleverapps.io/wp/blog/technology/2016/10/12/clever-cloud-driven-developement/</guid>

					<description><![CDATA[<p><img width="1400" height="540" src="https://cdn.clever-cloud.com/uploads/2021/08/ccdd.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="ccdd" decoding="async" fetchpriority="high" srcset="https://cdn.clever-cloud.com/uploads/2021/08/ccdd.png 1400w, https://cdn.clever-cloud.com/uploads/2021/08/ccdd-300x116.png 300w, https://cdn.clever-cloud.com/uploads/2021/08/ccdd-1024x395.png 1024w, https://cdn.clever-cloud.com/uploads/2021/08/ccdd-768x296.png 768w, https://cdn.clever-cloud.com/uploads/2021/08/ccdd-1368x528.png 1368w" sizes="(max-width: 1400px) 100vw, 1400px" /></p><p>In ancient times, developers weren&#39;t able to be just developers. They had to be experts in computer networking and development platforms as well.</p>
<span id="more-2807"></span>

<p>Great ideas had to sit on the sidelines and wait. You had to build out a sandbox to play in before you started coding. If you wanted to use Ruby (on or off Rails), you had to start with something like Ruby Version Manager (RVM) to set up the server space you needed. If you decided that Node.JS was a better fit for your next application, you would have to tear it all down and go get Node Version Manager (NVM) from GitHub. Obviously, people couldn&#39;t live that way for long.</p>
<h2 id="the-dawn-of-reason">The Dawn of Reason</h2>
<p>Platform as a Service (PaaS) providers started popping up about a decade ago and civilization took off from there. Now developers worry about development and the host manages the OS, the software stack, the runtime, the servers, virtualization and storage. That problem then became that PaaS providers were too narrow in their focus.</p>
<p>That&#39;s when Clever Cloud Driven Development (CCDD) was born. It evolved the concept of PaaS and made it polyglot. That means that now you have the freedom to work in any of your preferred runtimes (Docker, PHP, Scala, etc.) and have access to your favorite services (MongoDB, PostgreSQL, Redis, etc.). Now, instant access to those capabilities can be deployed with a simple git push.</p>
<h2 id="tell-me-how">Tell Me How</h2>
<p>One of the tricks that do the magic is the <a href="https://github.com/CleverCloud/clever-tools">Clever Tools™</a>, the official and open-source CLI. Installing it allows the rest of us to use Clever Cloud deploying without leaving the terminal. And that&#39;s pretty awesome.</p>
<blockquote>
<p>…deploying without leaving the Terminal.</p>
</blockquote>
<p>Here the quickstart to install and use it without leaving this page: open your term and paste this:</p>
<pre><code class="language-bash"># Do this once, and forget about it
npm install -g clever-tools &amp;&amp; install-clever-completion &amp;&amp; clever login

# Go to your workDir
# For the Terminalesque ones, create your app without touching a mouse
clever create -t node &quot;My Great Application&quot;
clever deploy
## PROFITT
clever open
## Grep logs as in ye olden days
clever logs --before 2016-10-13 | grep &quot;undefined is not a function&quot;
</code></pre>
<h2 id="what-to-do-with-all-the-extra-time">What to Do With All the Extra Time</h2>
<p>CCDD is not merely a convenience. It actually <em>makes the app world a better place</em>™. It allows developers to do a better job at continuous delivery. When you don&#39;t have to set up and maintain your local environment, your time is freed up to create the additional features that customers want or concentrate on performance tuning.</p>
<p>You don&#39;t need to manage your sandboxes for dev and production. CCDD can update your apps in production with no downtime and assure that they will automatically self-heal and redeploy after a crash.</p>
<p>For lean startups and small developer teams, CCDD means scalability from day one. You don&#39;t need to invest in more servers to handle spikes in traffic if you suddenly someone says something nice about you on Hacker News (one can only hope). CCDD scales horizontally and vertically as needed in an instant.</p>
<p>Every developer deserves top-of-the-line security and reactive monitoring. CCDD is built to handle the infrastructure while you handle the accolades.</p>
]]></description>
										<content:encoded><![CDATA[<p><img width="1400" height="540" src="https://cdn.clever-cloud.com/uploads/2021/08/ccdd.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="ccdd" decoding="async" srcset="https://cdn.clever-cloud.com/uploads/2021/08/ccdd.png 1400w, https://cdn.clever-cloud.com/uploads/2021/08/ccdd-300x116.png 300w, https://cdn.clever-cloud.com/uploads/2021/08/ccdd-1024x395.png 1024w, https://cdn.clever-cloud.com/uploads/2021/08/ccdd-768x296.png 768w, https://cdn.clever-cloud.com/uploads/2021/08/ccdd-1368x528.png 1368w" sizes="(max-width: 1400px) 100vw, 1400px" /></p><p>In ancient times, developers weren&#39;t able to be just developers. They had to be experts in computer networking and development platforms as well.</p>
<span id="more-2807"></span>

<p>Great ideas had to sit on the sidelines and wait. You had to build out a sandbox to play in before you started coding. If you wanted to use Ruby (on or off Rails), you had to start with something like Ruby Version Manager (RVM) to set up the server space you needed. If you decided that Node.JS was a better fit for your next application, you would have to tear it all down and go get Node Version Manager (NVM) from GitHub. Obviously, people couldn&#39;t live that way for long.</p>
<h2 id="the-dawn-of-reason">The Dawn of Reason</h2>
<p>Platform as a Service (PaaS) providers started popping up about a decade ago and civilization took off from there. Now developers worry about development and the host manages the OS, the software stack, the runtime, the servers, virtualization and storage. That problem then became that PaaS providers were too narrow in their focus.</p>
<p>That&#39;s when Clever Cloud Driven Development (CCDD) was born. It evolved the concept of PaaS and made it polyglot. That means that now you have the freedom to work in any of your preferred runtimes (Docker, PHP, Scala, etc.) and have access to your favorite services (MongoDB, PostgreSQL, Redis, etc.). Now, instant access to those capabilities can be deployed with a simple git push.</p>
<h2 id="tell-me-how">Tell Me How</h2>
<p>One of the tricks that do the magic is the <a href="https://github.com/CleverCloud/clever-tools">Clever Tools™</a>, the official and open-source CLI. Installing it allows the rest of us to use Clever Cloud deploying without leaving the terminal. And that&#39;s pretty awesome.</p>
<blockquote>
<p>…deploying without leaving the Terminal.</p>
</blockquote>
<p>Here the quickstart to install and use it without leaving this page: open your term and paste this:</p>
<pre><code class="language-bash"># Do this once, and forget about it
npm install -g clever-tools &amp;&amp; install-clever-completion &amp;&amp; clever login

# Go to your workDir
# For the Terminalesque ones, create your app without touching a mouse
clever create -t node &quot;My Great Application&quot;
clever deploy
## PROFITT
clever open
## Grep logs as in ye olden days
clever logs --before 2016-10-13 | grep &quot;undefined is not a function&quot;
</code></pre>
<h2 id="what-to-do-with-all-the-extra-time">What to Do With All the Extra Time</h2>
<p>CCDD is not merely a convenience. It actually <em>makes the app world a better place</em>™. It allows developers to do a better job at continuous delivery. When you don&#39;t have to set up and maintain your local environment, your time is freed up to create the additional features that customers want or concentrate on performance tuning.</p>
<p>You don&#39;t need to manage your sandboxes for dev and production. CCDD can update your apps in production with no downtime and assure that they will automatically self-heal and redeploy after a crash.</p>
<p>For lean startups and small developer teams, CCDD means scalability from day one. You don&#39;t need to invest in more servers to handle spikes in traffic if you suddenly someone says something nice about you on Hacker News (one can only hope). CCDD scales horizontally and vertically as needed in an instant.</p>
<p>Every developer deserves top-of-the-line security and reactive monitoring. CCDD is built to handle the infrastructure while you handle the accolades.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>null is not the issue</title>
		<link>https://www.clever.cloud/blog/engineering/2016/07/21/null-is-not-the-issue/</link>
		
		<dc:creator><![CDATA[Clement Delafargue]]></dc:creator>
		<pubDate>Thu, 21 Jul 2016 14:56:00 +0000</pubDate>
				<category><![CDATA[Engineering]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[thoughts]]></category>
		<category><![CDATA[workflow]]></category>
		<guid isPermaLink="false">https://www2.cleverapps.io/wp/blog/technology/2016/07/21/null-is-not-the-issue/</guid>

					<description><![CDATA[<p><img width="1400" height="540" src="https://cdn.clever-cloud.com/uploads/2021/08/null-issue-1.jpg" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="null issue 1" decoding="async" srcset="https://cdn.clever-cloud.com/uploads/2021/08/null-issue-1.jpg 1400w, https://cdn.clever-cloud.com/uploads/2021/08/null-issue-1-300x116.jpg 300w, https://cdn.clever-cloud.com/uploads/2021/08/null-issue-1-1024x395.jpg 1024w, https://cdn.clever-cloud.com/uploads/2021/08/null-issue-1-768x296.jpg 768w, https://cdn.clever-cloud.com/uploads/2021/08/null-issue-1-1368x528.jpg 1368w" sizes="(max-width: 1400px) 100vw, 1400px" /></p><p>Nowadays, we know that <code>null</code> is to be avoided. It&#39;s been dubbed the <em>billion dollar mistake</em> by its own creator, and the dreaded <code>NullPointerException</code> everyone knows about. Yet, when it comes to getting rid of <code>null</code>s, nobody agrees.</p>
<span id="more-2806"></span>

<h2 id="null-is-bad-mkay"><code>null</code> is bad, m&#39;kay</h2>
<ul>
<li>NPEs</li>
<li>hard to read (is this value always defined?)</li>
<li>handling possibly undefined values is tedious</li>
</ul>
<p>The issue is that we focus on <code>null</code>, and not on the actual problems it causes. If your only goal is to get rid of NPEs at all costs, then you will pay those costs. Dearly.</p>
<p>The memory safety issues caused by <code>null</code> are solved on most platforms. In many languages, <code>null</code> is a pointer to a specific value with specific properties, so not literally a null pointer anymore, and on modern OSs, the memory protection system will prevent your code from directly accessing the address <code>0x00</code>.</p>
<p>So the big risk is having your program blow up (<code>NPE</code>, <code>segfault</code>, <code>panic</code>, …) because of an unexpected missing value. The core issue is that you were unable to express that the value was possibly missing; the crash is a consequence of this issue. Yet many people try to solve the issue by preventing the crash (null check, <a href="https://en.wikipedia.org/wiki/Null_Object_pattern">null object pattern</a>, …) instead of handling the core issue: a value could be missing and you weren&#39;t able to express it.</p>
<h2 id="stop-chasing-npes-and-fix-your-domain-model">Stop chasing NPEs and fix your domain model</h2>
<p>If you&#39;re trying to get rid of NPEs by removing the difference between &quot;there is a meaningful value&quot; and &quot;there is no value&quot;, then not only you&#39;re not solving your issue, but you&#39;re making it far, far worse.</p>
<p>Your domain model didn&#39;t let you express statically the possible absence of a value, but the presence of null at least gave you a stuctural difference at runtime. That&#39;s why putting default values or worse, following the null object pattern, is terrible. You had structural information, but couldn&#39;t use it in a rigorous way, so you&#39;re just throwing it all away because of an implementation detail.</p>
<p>If you&#39;re searching for a replacement to <code>null</code> to denote missing values, it has to be structurally different from a regular value (it cannot belong to the domain of the value you may have). So if you&#39;re in a typed system, then a value that may be not there has to have a different type from a value that&#39;s definitely there. In an untyped language, you can&#39;t have a static difference, so using <code>null</code> is kind of OK (though there are better solutions).</p>
<p>For example, in java you can do:</p>
<pre><code class="language-java">// Bad
String notThere = null;
String there = &quot;my string&quot;;
// Turbo-bad
String notThere = &quot;&quot;; // or &quot;N/A&quot;, etc
String there = &quot;my string&quot;;
// Good
Optional&lt;String&gt; notThere = Optional.empty();
Optional&lt;String&gt; there = Optional.of(&quot;my string&quot;);
</code></pre>
<p>With optional, not only you have a structural difference between defined and undefined strings (instead of having to check if the string is equal to <code>&quot;&quot;</code> or <code>&quot;N/A&quot;</code>, without any guarantee that it&#39;s not the actual value), but it&#39;s clearly documented in the type.</p>
<p><code>Optional</code> is available in Java 8 (and in Guava if you&#39;re not using java 8 yet). In scala, rust, ocaml and many other languages, it&#39;s called <code>Option</code>, in Haskell it&#39;s called <code>Maybe</code>.</p>
<h2 id="this-is-why-you-can-have-nice-things">This is why you <em>can</em> have nice things</h2>
<p>Ok so now you have a proper representation for your optional values. In a typed language it means that you&#39;re forced to check if the value is already there before using it.</p>
<p>For instance, in java:</p>
<pre><code class="language-java">Optional&lt;Integer&gt; parseOptionalString(Optional&lt;String&gt; myOptionalString) {
    if(myOptionalString.isPresent()) {
        Optional&lt;Integer&gt; result = parseInt(myOptionalString.get());
        return result;
    } else {
        return Optional.empty();
    }
}
</code></pre>
<p>This works perfectly well, but is still as verbose as using explicit <code>null</code> checks. So if you&#39;re only concerned about code terseness (you really shouldn&#39;t), it can feel like a marginal improvement (it&#39;s way better than that).</p>
<p>Thankfully that&#39;s not the idiomatic way of handling <code>Optional</code> values: now that we can denote the abstract concept of <em>being possibly not there</em> (in this case, <code>Optional&lt;_&gt;</code>), then we can do useful stuff about it:</p>
<ul>
<li>transforming it only if it&#39;s defined (with <code>map</code>)</li>
<li>eliminate the option by providing a default value when we don&#39;t need the
information anymore (with <code>orElseGet</code>)</li>
<li>sequencing several operations returning options (with <code>flatMap</code>)</li>
</ul>
<p>All of this comes for free because we were able to clearly define our domain model. Some languages like Kotlin provide approaching solutions with things like the Safe Call Operator for chaining operations (instead of using <code>map</code> and <code>flatMap</code>) and the Elvis Operator for providing default values. This, however, is less extensible and less composable than having proper options (<code>map</code> and <code>flatMap</code> are not specific to <code>Optional</code>).</p>
<pre><code class="language-java">Optional&lt;Integer&gt; parseOptionalString(Optional&lt;String&gt; myOptionalString) {
    return myOptionalString.flatMap(parseInt);
}
</code></pre>
<p>Once you have properly defined your domain model, then you can have cleaner code thanks to the information encoded in your types. Aiming at terser code without proper abstraction will lead you to perlish nightmares.</p>
<h2 id="to-sum-up">To sum up</h2>
<p>Using <code>null</code> causes problems. Resorting to solutions that erase information (setting a default value too early or worse, the null object pattern) will cause graver and subtler problems.</p>
<p>In order of decreasing importance, the problems with <code>null</code> are:</p>
<ol>
<li>it&#39;s not clear for people if a value can be missing</li>
<li>the program can blow up if somebody forgot to handle a missing value</li>
<li>it&#39;s tedious to sequence operations returning null</li>
</ol>
<p>Don&#39;t settle for solutions that only address #2 and #3.</p>
]]></description>
										<content:encoded><![CDATA[<p><img width="1400" height="540" src="https://cdn.clever-cloud.com/uploads/2021/08/null-issue-1.jpg" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="null issue 1" decoding="async" loading="lazy" srcset="https://cdn.clever-cloud.com/uploads/2021/08/null-issue-1.jpg 1400w, https://cdn.clever-cloud.com/uploads/2021/08/null-issue-1-300x116.jpg 300w, https://cdn.clever-cloud.com/uploads/2021/08/null-issue-1-1024x395.jpg 1024w, https://cdn.clever-cloud.com/uploads/2021/08/null-issue-1-768x296.jpg 768w, https://cdn.clever-cloud.com/uploads/2021/08/null-issue-1-1368x528.jpg 1368w" sizes="auto, (max-width: 1400px) 100vw, 1400px" /></p><p>Nowadays, we know that <code>null</code> is to be avoided. It&#39;s been dubbed the <em>billion dollar mistake</em> by its own creator, and the dreaded <code>NullPointerException</code> everyone knows about. Yet, when it comes to getting rid of <code>null</code>s, nobody agrees.</p>
<span id="more-2806"></span>

<h2 id="null-is-bad-mkay"><code>null</code> is bad, m&#39;kay</h2>
<ul>
<li>NPEs</li>
<li>hard to read (is this value always defined?)</li>
<li>handling possibly undefined values is tedious</li>
</ul>
<p>The issue is that we focus on <code>null</code>, and not on the actual problems it causes. If your only goal is to get rid of NPEs at all costs, then you will pay those costs. Dearly.</p>
<p>The memory safety issues caused by <code>null</code> are solved on most platforms. In many languages, <code>null</code> is a pointer to a specific value with specific properties, so not literally a null pointer anymore, and on modern OSs, the memory protection system will prevent your code from directly accessing the address <code>0x00</code>.</p>
<p>So the big risk is having your program blow up (<code>NPE</code>, <code>segfault</code>, <code>panic</code>, …) because of an unexpected missing value. The core issue is that you were unable to express that the value was possibly missing; the crash is a consequence of this issue. Yet many people try to solve the issue by preventing the crash (null check, <a href="https://en.wikipedia.org/wiki/Null_Object_pattern">null object pattern</a>, …) instead of handling the core issue: a value could be missing and you weren&#39;t able to express it.</p>
<h2 id="stop-chasing-npes-and-fix-your-domain-model">Stop chasing NPEs and fix your domain model</h2>
<p>If you&#39;re trying to get rid of NPEs by removing the difference between &quot;there is a meaningful value&quot; and &quot;there is no value&quot;, then not only you&#39;re not solving your issue, but you&#39;re making it far, far worse.</p>
<p>Your domain model didn&#39;t let you express statically the possible absence of a value, but the presence of null at least gave you a stuctural difference at runtime. That&#39;s why putting default values or worse, following the null object pattern, is terrible. You had structural information, but couldn&#39;t use it in a rigorous way, so you&#39;re just throwing it all away because of an implementation detail.</p>
<p>If you&#39;re searching for a replacement to <code>null</code> to denote missing values, it has to be structurally different from a regular value (it cannot belong to the domain of the value you may have). So if you&#39;re in a typed system, then a value that may be not there has to have a different type from a value that&#39;s definitely there. In an untyped language, you can&#39;t have a static difference, so using <code>null</code> is kind of OK (though there are better solutions).</p>
<p>For example, in java you can do:</p>
<pre><code class="language-java">// Bad
String notThere = null;
String there = &quot;my string&quot;;
// Turbo-bad
String notThere = &quot;&quot;; // or &quot;N/A&quot;, etc
String there = &quot;my string&quot;;
// Good
Optional&lt;String&gt; notThere = Optional.empty();
Optional&lt;String&gt; there = Optional.of(&quot;my string&quot;);
</code></pre>
<p>With optional, not only you have a structural difference between defined and undefined strings (instead of having to check if the string is equal to <code>&quot;&quot;</code> or <code>&quot;N/A&quot;</code>, without any guarantee that it&#39;s not the actual value), but it&#39;s clearly documented in the type.</p>
<p><code>Optional</code> is available in Java 8 (and in Guava if you&#39;re not using java 8 yet). In scala, rust, ocaml and many other languages, it&#39;s called <code>Option</code>, in Haskell it&#39;s called <code>Maybe</code>.</p>
<h2 id="this-is-why-you-can-have-nice-things">This is why you <em>can</em> have nice things</h2>
<p>Ok so now you have a proper representation for your optional values. In a typed language it means that you&#39;re forced to check if the value is already there before using it.</p>
<p>For instance, in java:</p>
<pre><code class="language-java">Optional&lt;Integer&gt; parseOptionalString(Optional&lt;String&gt; myOptionalString) {
    if(myOptionalString.isPresent()) {
        Optional&lt;Integer&gt; result = parseInt(myOptionalString.get());
        return result;
    } else {
        return Optional.empty();
    }
}
</code></pre>
<p>This works perfectly well, but is still as verbose as using explicit <code>null</code> checks. So if you&#39;re only concerned about code terseness (you really shouldn&#39;t), it can feel like a marginal improvement (it&#39;s way better than that).</p>
<p>Thankfully that&#39;s not the idiomatic way of handling <code>Optional</code> values: now that we can denote the abstract concept of <em>being possibly not there</em> (in this case, <code>Optional&lt;_&gt;</code>), then we can do useful stuff about it:</p>
<ul>
<li>transforming it only if it&#39;s defined (with <code>map</code>)</li>
<li>eliminate the option by providing a default value when we don&#39;t need the
information anymore (with <code>orElseGet</code>)</li>
<li>sequencing several operations returning options (with <code>flatMap</code>)</li>
</ul>
<p>All of this comes for free because we were able to clearly define our domain model. Some languages like Kotlin provide approaching solutions with things like the Safe Call Operator for chaining operations (instead of using <code>map</code> and <code>flatMap</code>) and the Elvis Operator for providing default values. This, however, is less extensible and less composable than having proper options (<code>map</code> and <code>flatMap</code> are not specific to <code>Optional</code>).</p>
<pre><code class="language-java">Optional&lt;Integer&gt; parseOptionalString(Optional&lt;String&gt; myOptionalString) {
    return myOptionalString.flatMap(parseInt);
}
</code></pre>
<p>Once you have properly defined your domain model, then you can have cleaner code thanks to the information encoded in your types. Aiming at terser code without proper abstraction will lead you to perlish nightmares.</p>
<h2 id="to-sum-up">To sum up</h2>
<p>Using <code>null</code> causes problems. Resorting to solutions that erase information (setting a default value too early or worse, the null object pattern) will cause graver and subtler problems.</p>
<p>In order of decreasing importance, the problems with <code>null</code> are:</p>
<ol>
<li>it&#39;s not clear for people if a value can be missing</li>
<li>the program can blow up if somebody forgot to handle a missing value</li>
<li>it&#39;s tedious to sequence operations returning null</li>
</ol>
<p>Don&#39;t settle for solutions that only address #2 and #3.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How Facebook Infer Can Make your Software Safer</title>
		<link>https://www.clever.cloud/blog/engineering/2015/06/22/fb-infer-safer-code/</link>
		
		<dc:creator><![CDATA[Marc-Antoine Perennou]]></dc:creator>
		<pubDate>Mon, 22 Jun 2015 17:05:00 +0000</pubDate>
				<category><![CDATA[Engineering]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[compilation]]></category>
		<category><![CDATA[static-analysis]]></category>
		<guid isPermaLink="false">https://www2.cleverapps.io/wp/blog/technology/2015/06/22/fb-infer-safer-code/</guid>

					<description><![CDATA[<p><img width="1400" height="540" src="https://cdn.clever-cloud.com/uploads/2021/08/fb-infer-1.jpg" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="fb infer 1" decoding="async" loading="lazy" srcset="https://cdn.clever-cloud.com/uploads/2021/08/fb-infer-1.jpg 1400w, https://cdn.clever-cloud.com/uploads/2021/08/fb-infer-1-300x116.jpg 300w, https://cdn.clever-cloud.com/uploads/2021/08/fb-infer-1-1024x395.jpg 1024w, https://cdn.clever-cloud.com/uploads/2021/08/fb-infer-1-768x296.jpg 768w, https://cdn.clever-cloud.com/uploads/2021/08/fb-infer-1-1368x528.jpg 1368w" sizes="auto, (max-width: 1400px) 100vw, 1400px" /></p><p>At Clever Cloud, we like giving a try to new technologies when they&#39;re released, especially if it means safer software. When facebook <a href="https://code.facebook.com/posts/1648953042007882/open-sourcing-facebook-infer-identify-bugs-before-you-ship/">recently announced</a> the release of <a href="http://fbinfer.com/">infer</a>, we surely wanted to try it out!</p>
<span id="more-2801"></span>

<h3 id="the-concept-of-static-analysis">The concept of static analysis</h3>
<p>The idea behind infer is quite simple: it analyses your code before you actually run it and looks for common code issues or misconceptions which can lead to unsafe code.</p>
<p>You can think of it as some kind of compiler plugin which will do some stricter checks and produce more warnings than your compiler usually does.</p>
<p>The idea of static analysis is really great as you get to know some of your code&#39;s problems before even trying to run it. An issue with most static analyzers though is the false positives, those warnings you know shouldn&#39;t be there as you perfectly know that this problem is 100% impossible to happen.</p>
<h3 id="testing-our-code-with-infer">Testing our code with infer</h3>
<p>We tend to use a lot of different technologies to run our platform. From the languages supported by infer, Java was the most interesting to us. I decided to run a first pass on all of our Java projects.</p>
<p>After getting <a href="https://github.com/facebook/infer">the code</a> on github and following the installation instructions in <code>INSTALL.md</code>, I started with one of our core small pieces of software.</p>
<p>I didn&#39;t expect to see many defects but was afraid of getting a ton of false positives when I ran the test: <code>infer -- mvn clean package</code>. Turned out we got a couple of legitimate warnings about resource leaks when spawning a process and then waiting for it but not destroying it once done, and nothing more! No false positives at all.</p>
<p>For the record, those warnings looked like that:</p>
<p><code>error: RESOURCE_LEAK resource acquired by call to exec(...) at line 89 is not released after line 89</code></p>
<p>Fix was as easy as changing <code>return Runtime.getRuntime().exec(cmd).waitFor();</code> to</p>
<pre><code class="language-java">Process p = Runtime.getRuntime().exec(cmd);
int res = p.waitFor();
p.destroy();
return res;
</code></pre>
<p>As the beginning was encouraging, I went on and ran it on one of our core libraries. One similar problem found, one potential null dereference in an error path and another resource leak warning. The first two were trivial to fix, the last one was a weird one as in some cases it didn&#39;t detect a stream to be closed. After refactoring this piece of code which was quite old, the warning was gone, but what appeared to me as being a false positive frightened me when it came to running it on our big API.</p>
<p>The relevant annoying warning: <code>error: RESOURCE_LEAK resource acquired by call to FileReaderHelper(...) at line 28 is not released after line 30</code></p>
<p>FileReaderHelper makes use of a BufferedReader; changing the scope of the inside reader fixed the issue.</p>
<p>Most of our other projects are really small and no defects were detected at all; then came our API. Our API is the oldest piece of software we&#39;re running. As time goes by, we externalise the codebase into separate projects but it&#39;s still quite a huge one with its 415 files.</p>
<p>Good surprise: only 20 defects, 18 of them in the same error path which would mean that our platform is unavailable anyways. Amongst the two others, one was clearly a false positive. I added an assertion to silence the warning with a comment explaining why it was there. But the warning wouldn&#39;t go. I&#39;ve since then opened <a href="https://github.com/facebook/infer/issues/68">an issue</a> for this which hopefully will be fixed soon.</p>
<p>An example of NULL pointer dereference for which we added checks because &quot;better safe than sorry&quot; when it really should never happen: <code>error: NULL_DEREFERENCE object returned by token.getUserId() could be null and is dereferenced at line 86</code></p>
<h3 id="conclusions">Conclusions</h3>
<p>Only two false positives in all of our codebase, and a couple of really pertinent warnings makes this project really promising. It doesn&#39;t throw a lot of warnings yet (or we didn&#39;t hit much) but it reduces the noise by giving only relevant ones and avoiding false positives, which is a really good thing for a static analyser.</p>
<p>With an integration into our CI, it&#39;ll now provide yet more guaranties about code safety.</p>
<p>Go and try it for yourself!</p>
]]></description>
										<content:encoded><![CDATA[<p><img width="1400" height="540" src="https://cdn.clever-cloud.com/uploads/2021/08/fb-infer-1.jpg" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="fb infer 1" decoding="async" loading="lazy" srcset="https://cdn.clever-cloud.com/uploads/2021/08/fb-infer-1.jpg 1400w, https://cdn.clever-cloud.com/uploads/2021/08/fb-infer-1-300x116.jpg 300w, https://cdn.clever-cloud.com/uploads/2021/08/fb-infer-1-1024x395.jpg 1024w, https://cdn.clever-cloud.com/uploads/2021/08/fb-infer-1-768x296.jpg 768w, https://cdn.clever-cloud.com/uploads/2021/08/fb-infer-1-1368x528.jpg 1368w" sizes="auto, (max-width: 1400px) 100vw, 1400px" /></p><p>At Clever Cloud, we like giving a try to new technologies when they&#39;re released, especially if it means safer software. When facebook <a href="https://code.facebook.com/posts/1648953042007882/open-sourcing-facebook-infer-identify-bugs-before-you-ship/">recently announced</a> the release of <a href="http://fbinfer.com/">infer</a>, we surely wanted to try it out!</p>
<span id="more-2801"></span>

<h3 id="the-concept-of-static-analysis">The concept of static analysis</h3>
<p>The idea behind infer is quite simple: it analyses your code before you actually run it and looks for common code issues or misconceptions which can lead to unsafe code.</p>
<p>You can think of it as some kind of compiler plugin which will do some stricter checks and produce more warnings than your compiler usually does.</p>
<p>The idea of static analysis is really great as you get to know some of your code&#39;s problems before even trying to run it. An issue with most static analyzers though is the false positives, those warnings you know shouldn&#39;t be there as you perfectly know that this problem is 100% impossible to happen.</p>
<h3 id="testing-our-code-with-infer">Testing our code with infer</h3>
<p>We tend to use a lot of different technologies to run our platform. From the languages supported by infer, Java was the most interesting to us. I decided to run a first pass on all of our Java projects.</p>
<p>After getting <a href="https://github.com/facebook/infer">the code</a> on github and following the installation instructions in <code>INSTALL.md</code>, I started with one of our core small pieces of software.</p>
<p>I didn&#39;t expect to see many defects but was afraid of getting a ton of false positives when I ran the test: <code>infer -- mvn clean package</code>. Turned out we got a couple of legitimate warnings about resource leaks when spawning a process and then waiting for it but not destroying it once done, and nothing more! No false positives at all.</p>
<p>For the record, those warnings looked like that:</p>
<p><code>error: RESOURCE_LEAK resource acquired by call to exec(...) at line 89 is not released after line 89</code></p>
<p>Fix was as easy as changing <code>return Runtime.getRuntime().exec(cmd).waitFor();</code> to</p>
<pre><code class="language-java">Process p = Runtime.getRuntime().exec(cmd);
int res = p.waitFor();
p.destroy();
return res;
</code></pre>
<p>As the beginning was encouraging, I went on and ran it on one of our core libraries. One similar problem found, one potential null dereference in an error path and another resource leak warning. The first two were trivial to fix, the last one was a weird one as in some cases it didn&#39;t detect a stream to be closed. After refactoring this piece of code which was quite old, the warning was gone, but what appeared to me as being a false positive frightened me when it came to running it on our big API.</p>
<p>The relevant annoying warning: <code>error: RESOURCE_LEAK resource acquired by call to FileReaderHelper(...) at line 28 is not released after line 30</code></p>
<p>FileReaderHelper makes use of a BufferedReader; changing the scope of the inside reader fixed the issue.</p>
<p>Most of our other projects are really small and no defects were detected at all; then came our API. Our API is the oldest piece of software we&#39;re running. As time goes by, we externalise the codebase into separate projects but it&#39;s still quite a huge one with its 415 files.</p>
<p>Good surprise: only 20 defects, 18 of them in the same error path which would mean that our platform is unavailable anyways. Amongst the two others, one was clearly a false positive. I added an assertion to silence the warning with a comment explaining why it was there. But the warning wouldn&#39;t go. I&#39;ve since then opened <a href="https://github.com/facebook/infer/issues/68">an issue</a> for this which hopefully will be fixed soon.</p>
<p>An example of NULL pointer dereference for which we added checks because &quot;better safe than sorry&quot; when it really should never happen: <code>error: NULL_DEREFERENCE object returned by token.getUserId() could be null and is dereferenced at line 86</code></p>
<h3 id="conclusions">Conclusions</h3>
<p>Only two false positives in all of our codebase, and a couple of really pertinent warnings makes this project really promising. It doesn&#39;t throw a lot of warnings yet (or we didn&#39;t hit much) but it reduces the noise by giving only relevant ones and avoiding false positives, which is a really good thing for a static analyser.</p>
<p>With an integration into our CI, it&#39;ll now provide yet more guaranties about code safety.</p>
<p>Go and try it for yourself!</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
