<?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: DeepCode.AI</title>
    <description>The latest articles on DEV Community by DeepCode.AI (@deepcode).</description>
    <link>https://dev.to/deepcode</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%2Forganization%2Fprofile_image%2F1723%2F1f1c0d36-59a0-4deb-ab94-eed482d003d1.jpg</url>
      <title>DEV Community: DeepCode.AI</title>
      <link>https://dev.to/deepcode</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/deepcode"/>
    <language>en</language>
    <item>
      <title>JavaScript Promises: All you need to know</title>
      <dc:creator>cu_0xff 🇪🇺</dc:creator>
      <pubDate>Tue, 30 Jun 2020 11:36:21 +0000</pubDate>
      <link>https://dev.to/deepcode/javascript-promises-all-you-need-to-know-1ig1</link>
      <guid>https://dev.to/deepcode/javascript-promises-all-you-need-to-know-1ig1</guid>
      <description>&lt;p&gt;Without any doubt: The most frequent suggestion we see corrected in the wild is actually a pretty trivial one (who would have thought). We found roughly 20,000 changes in our training repos addressing one thing: Unhandled rejections in promises. Maybe it is time to provide a fundamental guide.&lt;/p&gt;

&lt;p&gt;If you want your code to be scanned, just go to &lt;a href="https://deepcode.ai"&gt;deepcode.ai&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why are promises needed?
&lt;/h2&gt;

&lt;p&gt;JavaScript provides a single threaded environment: No two pieces of code run at the same time. This reduces issues regarding consistency with mutexes (think race conditions), but add the need for others. JavaScript makes use of callback functions to provide asynchronous calculations. While this in itself is a possible way to write asynchronous code, it leads to what is known as &lt;em&gt;Pyramid of Doom&lt;/em&gt; where you have callback in callback in callback in ... until you totally lose track what happens when. Here come promises to help.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A promise is an object that represents eventual completion or failure of an asynchronous operation and its subsequent result value.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Dive deeper with &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise"&gt;MDN docs on Promise&lt;/a&gt; or &lt;a href="https://javascript.info/async"&gt;Javascript.info&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Functionality
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Here is the workload&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="cm"&gt;/* Success */&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="cm"&gt;/* result can be a Promise or thenable */&lt;/span&gt;
        &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="cm"&gt;/* Best practice: reason is instanceof Error */&lt;/span&gt;
        &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reason&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;Within a promise, two callback functions are provided: &lt;code&gt;resolve()&lt;/code&gt; shall be called in the case of a positive outcome. The result can be handed on as a parameter. &lt;code&gt;reject&lt;/code&gt; shall be called in the case of an error (including an explanation as a parameter). There is no third option (like cancelled or whatever). A promise is always in one of three states:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;pending:&lt;/em&gt; Initial state and still work in progress&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;fulfilled:&lt;/em&gt; Completed successfully&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;rejected:&lt;/em&gt; Operation failed&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;Promises can be hand-on within the app as long as their value is not directly needed. This gives the system the opportunity to resolve whatever is asked in the background without the need to wait for things to settle. When the application has the need for the result to proceed, it can query the result and react upon it.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Side note:&lt;/em&gt; Try to delay the need for a result of a promise as much as possible to gain the maximum benefit of using promises.&lt;/p&gt;

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

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promise1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Here is the result&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;promise1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="cm"&gt;/* Success */&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&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="c1"&gt;// Do something with the result&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="cm"&gt;/* Fail */&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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="c1"&gt;// React on error case&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;If the success handler is not provided (as a function), it is replaced by the identity function (aka, it returns the parameter). &lt;/p&gt;

&lt;p&gt;This is the lifecycle as provided by Mozilla's documentation (yet omitting &lt;code&gt;finally()&lt;/code&gt;):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BDtxbm8A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mdn.mozillademos.org/files/15911/promises.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BDtxbm8A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mdn.mozillademos.org/files/15911/promises.png" alt="Promises Lifecycle"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see in the life cycle: There are two options to provide an &lt;strong&gt;error handler: Either by providing a callback as a parameter for then or by explicitly providing a callback in catch.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  async and await
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;async&lt;/code&gt; in front of a function means the function always returns a promise. If the function returns a different type, it is wrapped in a promise.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;f&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="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// We will get a promise with result 42 and success state&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//prints 42&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So far, we only stacked on promises but what if we really need the value to be settled. Here comes &lt;code&gt;await&lt;/code&gt;. This keyword makes JavaScript wait until the promise is settled. Obviously, while the system stops here and waits with the execution of the code until the promise is settled, it continues to execute other code. We will see more sophisticated methods to combine promises a little later.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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="c1"&gt;// Do something here&lt;/span&gt;
        &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;f&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;Note:&lt;/strong&gt; &lt;code&gt;await&lt;/code&gt; only works inside an &lt;code&gt;async&lt;/code&gt; function. You cannot use &lt;code&gt;await&lt;/code&gt; on top level code but you can wrap it in an anonymous async function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&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;let&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;promise1&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;h2&gt;
  
  
  Chaining of Promises
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://javascript.info/promise-chaining"&gt;Nice in-depth article on the topic here&lt;/a&gt;&lt;br&gt;
The result of a promise can be piped through subsequent functions. Those functions can provide alternative parameters to their subsequent followers allowing to build a pipeline to manipulate data such as filtering or enriching. The then function returns a promise itself.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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="c1"&gt;// Promise&lt;/span&gt;
    &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;result&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="c1"&gt;// First handler&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;handler_result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;handlerResult&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="c1"&gt;// Second handler&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;secondHandlerResult&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;secondHandlerResult&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Objects providing a &lt;code&gt;then&lt;/code&gt; function, are called &lt;em&gt;thenable&lt;/em&gt;. These objects can be used with then or await as we saw above.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Thenable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;success&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//in case of success &lt;/span&gt;
            &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;
            &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Thenable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There are two more important functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promise1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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="k"&gt;throw&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// calling reject() also leads to rejected state&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nx"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&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="c1"&gt;// Handle your error here&lt;/span&gt;
    &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="k"&gt;finally&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="c1"&gt;//Do clean up needed whether success or failure&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;catch:&lt;/em&gt; This is actually a shortcut for &lt;code&gt;then(undefined, onRejected)&lt;/code&gt; and provides a promise that handles error cases. It does an implicit try-catch (as shown above).&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;finally:&lt;/em&gt; Again a promise which is called in both end states. Helps to reduce code doubling in &lt;code&gt;then&lt;/code&gt; and &lt;code&gt;catch&lt;/code&gt; as you can put all clean up needed in both cases in here. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Side note:&lt;/em&gt; Since both &lt;code&gt;catch()&lt;/code&gt; and &lt;code&gt;finally()&lt;/code&gt; return promises, you can chain one them again by using &lt;code&gt;then&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If there is no error handler provided (&lt;em&gt;"unhandled rejection"&lt;/em&gt;), the error bubbles up and leads to the script crashing with an error message. This is what DeepCode complains when it finds an unhandled rejection. &lt;/p&gt;

&lt;h2&gt;
  
  
  Combination of Promises
&lt;/h2&gt;

&lt;p&gt;Promises can be collected and combined in various ways. In general, combining promises in a iterable object and provide this to the combination.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;all&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;promise1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;promise2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;promise3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;promise4&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;values&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;values&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Result Array [result1, result2, result3, result4]&lt;/span&gt;
    &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&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="c1"&gt;// something went wrong&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here is a table of all combination functions:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Function&lt;/th&gt;
&lt;th&gt;Explanation&lt;/th&gt;
&lt;th&gt;Typical Use&lt;/th&gt;
&lt;th&gt;Reaction on Rejection&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Promise.all()&lt;/td&gt;
&lt;td&gt;Returns a single promise to collect all results of the input promises&lt;/td&gt;
&lt;td&gt;Multiple asynchronous tasks that are dependent on each other&lt;/td&gt;
&lt;td&gt;Will reject immediately on one input promise reject&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Promise.allSettled()&lt;/td&gt;
&lt;td&gt;Returns a single promise to collect all results of the input promises&lt;/td&gt;
&lt;td&gt;Multiple asynchronous tasks that are not dependent on each other&lt;/td&gt;
&lt;td&gt;Will collect all results and rejects&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Promise.race()&lt;/td&gt;
&lt;td&gt;Returns a single Promise that returns as soon as one of the input promises resolve or rejects&lt;/td&gt;
&lt;td&gt;Used in batching or for time-outs&lt;/td&gt;
&lt;td&gt;Immediately returns on one input rejects&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Promise.any()(still experimental)&lt;/td&gt;
&lt;td&gt;Returns a single Promise which resolves as soon as one of the input promises resolves but wait for all promises to reject before reject&lt;/td&gt;
&lt;td&gt;When two resources race to provide data (e.g., cache versus network)&lt;/td&gt;
&lt;td&gt;Swallow rejections until all input promises reject&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Next Steps
&lt;/h2&gt;

&lt;p&gt;From our point of view, knowing the above should equip you to understand and use promises like a pro. Did we miss something, let us know. And, make sure to check your code on &lt;a href="https://deepcode.ai"&gt;deepcode.ai&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Smart Labeling of Suggestions</title>
      <dc:creator>cu_0xff 🇪🇺</dc:creator>
      <pubDate>Fri, 24 Apr 2020 12:14:00 +0000</pubDate>
      <link>https://dev.to/deepcode/smart-labeling-of-suggestions-44</link>
      <guid>https://dev.to/deepcode/smart-labeling-of-suggestions-44</guid>
      <description>&lt;p&gt;When using DeepCode, you might see the following effect:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GYfsjhlB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/h7qr4pefqselm1nkjafy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GYfsjhlB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/h7qr4pefqselm1nkjafy.png" alt="Same suggestion twice?"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the first glimpse, it looks like the same suggestion. The difference is actually in the tags. One of them is &lt;em&gt;Info&lt;/em&gt; and one is &lt;em&gt;InTest&lt;/em&gt;. How comes?&lt;/p&gt;

&lt;h1&gt;
  
  
  Suggestions in Test Code
&lt;/h1&gt;

&lt;p&gt;When you run tests, you probably want to to check for unsupported conditions and when mocking environments, you will use static elements. As an example, you can test your code by providing a wrong type to a function call, or mocking by using a hard-coded password. Obviously, DeepCode’s rule would point out this problem.&lt;br&gt;
While we are providing the option to exclude certain directories from a scan using &lt;code&gt;.dcignore&lt;/code&gt; (see &lt;a href="https://deepcode.freshdesk.com/support/solutions/articles/60000531055-how-can-i-ignore-files-"&gt;here&lt;/a&gt; ) but what if you scan a repo for the first time. It might clutter the whole result set. So, DeepCode applies some smartness to flagging suggestions.&lt;/p&gt;

&lt;h1&gt;
  
  
  Smart Flags
&lt;/h1&gt;

&lt;p&gt;DeepCode tries to understand if the code is actually part of the test suite. This is done by applying some rules including scanning the path and file name. Since DeepCode cannot finally decide if you want to skip the test code analysis results. If you see the example above, it might make sense to touch the tests, too. Therefore, we add a flag that enables you to decide to have testing code in or not.&lt;/p&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iDf9u5M5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/eekm5rqvjmfa3mtf13vp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iDf9u5M5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/eekm5rqvjmfa3mtf13vp.png" alt="Filter Options"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A best practice is to keep all results in but watch out for the &lt;em&gt;InTest&lt;/em&gt; flag. If you see a security issue like a hardcoded password in a test, you can relax. But sometimes, you will find nuggets in your test code that you should have a look at because it might break your tests. Btw, simply clicking on a tag, makes it a search query. If you click on the filters in the top right corner, you can deselect InTest to suppress all suggestions in test files.&lt;/p&gt;

</description>
      <category>deepcode</category>
      <category>testing</category>
    </item>
    <item>
      <title>Ability to Infer Types or What Python Got to do With It</title>
      <dc:creator>cu_0xff 🇪🇺</dc:creator>
      <pubDate>Wed, 22 Apr 2020 08:01:05 +0000</pubDate>
      <link>https://dev.to/deepcode/ability-to-infer-types-or-what-python-got-to-do-with-it-212d</link>
      <guid>https://dev.to/deepcode/ability-to-infer-types-or-what-python-got-to-do-with-it-212d</guid>
      <description>&lt;p&gt;&lt;a href="https://deepcode.ai"&gt;DeepCode&lt;/a&gt; recently added a feature to infer types from hints in source code. For "hard-typed" languages like Java, this is pretty straight forward. For languages like Python, it is not. Let us start with a small example and dig into the details after that.&lt;/p&gt;

&lt;h1&gt;
  
  
  Example First
&lt;/h1&gt;

&lt;p&gt;Our example below uses Python and if you want to follow along, you can use this &lt;a href="https://www.deepcode.ai/app/gh/oneadvent/plugin.video.liveleak/787d677e53f8ac0ff5ee446bdbb47dbd3e716a15/_/dashboard"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4iOaZWQN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jy5dxrwjquy1146qcw7d.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4iOaZWQN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jy5dxrwjquy1146qcw7d.PNG" alt="Original Suggestion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The suggestion says that a variable that is a list might be accessed using the index operator by providing a string. While this would normally lead to a crash, in our case since we &lt;code&gt;try&lt;/code&gt; &lt;code&gt;except&lt;/code&gt; &lt;code&gt;pass&lt;/code&gt; ... well, it is a kind of creative use of the exception mechanism. I guess not a best-practice. Let us unveil what happens here.&lt;/p&gt;

&lt;p&gt;As you can see the variable &lt;code&gt;params&lt;/code&gt; is initialized using the function &lt;code&gt;get_params()&lt;/code&gt; (see line 113). Since later on we are using the index operator &lt;code&gt;[]&lt;/code&gt; with a string (see lines 119, 123, and 127), we would need this to be a dictionary (signalled by &lt;code&gt;{}&lt;/code&gt;) or something along that lines.&lt;/p&gt;

&lt;p&gt;Let us have a look at &lt;code&gt;get_params()&lt;/code&gt; now:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tWPuMTto--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fhwmifpax22fvct0k6pv.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tWPuMTto--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fhwmifpax22fvct0k6pv.PNG" alt="get_params()"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, line 81 the return value is the variable &lt;code&gt;param&lt;/code&gt; which is initialized to a list in line 66. If you follow the paths, you see that &lt;code&gt;param&lt;/code&gt; actually becomes a dictionary in line 74. But only if &lt;code&gt;len(paramstring)&lt;/code&gt; is greater or equal to two. Otherwise, the &lt;code&gt;param&lt;/code&gt; stays a list and we will see the creative use of the exception mechanism kicking in. I spare any comment if someone should use exceptions in that regard and focus on what happens from a language point of view.&lt;/p&gt;

&lt;h1&gt;
  
  
  Behind the Scenes
&lt;/h1&gt;

&lt;p&gt;The DeepCode engine analysed that piece of code and inferred that the return value of &lt;code&gt;get_params()&lt;/code&gt; can be list or dictionary. In Python, is less strict with types like Java is. And in Python, it is quite common to use different types for the same variable to signal different states. Following the flow of the application, DeepCode noticed that later an access mechanism is used that is not compatible with all possible values. This is what is mentioned in the suggestion.&lt;/p&gt;

&lt;p&gt;The DeepCode engine solves several tasks here: First, the possible types of an element in memory. Next, the path this element takes within the code. It might be — just as in our case — that variables get assigned to results of function calls or other variables. Last, DeepCode checks the usage of these elements for example in function calls or operators. The engine learns which types are acceptable where by using a combination of open-source code usage, documentation scanning, and human engineers reviewing the rules. Augmented AI at its best.&lt;/p&gt;

&lt;p&gt;This feature is especial interest for developers using languages like Python or JavaScript. In these languages, variables can change type. A lot of developers put this to great use as the type is used as a flag to carry information. But sometimes, you can entangle yourself and these faults are hard to trace. In the above’s example, our developer — instead of making a call and catch the exception — could have delivered a &lt;code&gt;None&lt;/code&gt; back if there is no parameter, check for this case and act accordingly.&lt;/p&gt;

&lt;p&gt;We added several new rules that find patterns like passing linear data structures into &lt;code&gt;**kwargs&lt;/code&gt; or map data into &lt;code&gt;*args&lt;/code&gt;, attribute address on primitive values, mutation of immutables, iterating over non-iterables, and more.&lt;/p&gt;

&lt;p&gt;In DeepCode, we are committed to serve the developer community to achieve the best code quality possible. We provide our service for free for teams below 30 people and for open source projects. Simply, check it out at &lt;a href="https://deepcode.ai"&gt;DeepCode.AI&lt;/a&gt;&lt;/p&gt;

</description>
      <category>deepcode</category>
      <category>python</category>
      <category>staticcodeanalysis</category>
      <category>codequality</category>
    </item>
    <item>
      <title>Stay Home, Stay Safe, Code On</title>
      <dc:creator>cu_0xff 🇪🇺</dc:creator>
      <pubDate>Mon, 20 Apr 2020 09:32:09 +0000</pubDate>
      <link>https://dev.to/deepcode/stay-home-stay-safe-code-on-1d72</link>
      <guid>https://dev.to/deepcode/stay-home-stay-safe-code-on-1d72</guid>
      <description>&lt;p&gt;We from &lt;a href="https://deepcode.ai"&gt;DeepCode&lt;/a&gt; are committed to our developer community! As seen on &lt;a href="https://www.computerworld.com/article/3534478/tech-pitches-in-to-fight-covid-19-pandemic.html"&gt;Computerworld&lt;/a&gt; or &lt;a href="https://www.heise.de/developer/meldung/Developer-Snapshots-Programmierer-News-in-ein-zwei-Saetzen-4702583.html"&gt;Heise&lt;/a&gt;, we keep on to provide our service free of charge plus extended it to teams larger than 30 people.&lt;/p&gt;

&lt;p&gt;We are encouraging all developers to do the right thing and &lt;strong&gt;stay home&lt;/strong&gt; to &lt;strong&gt;stay safe&lt;/strong&gt;. Also, &lt;strong&gt;code on&lt;/strong&gt; as the world needs more good software than ever. Let us lend you a hand to be productive early on by using our &lt;a href="https://code.visualstudio.com/docs/editor/extension-gallery"&gt;Visual Studio Code&lt;/a&gt; or &lt;a href="https://atom.io/packages/deepcode"&gt;Atom Package&lt;/a&gt;. Make sure to use DeepCode so your code stays safe, too.&lt;/p&gt;

&lt;p&gt;We are also here to help whenever you need us. Send us an email on &lt;a href="//mail:hello@deepcode.ai"&gt;hello@deepcode.ai&lt;/a&gt; or visit our &lt;a href="https://deepcode.ai"&gt;website&lt;/a&gt; to chat, DM on &lt;a href="https://twitter.com/DeepCodeAI"&gt;@DeepCodeAI&lt;/a&gt;, &lt;a href="https://www.linkedin.com/company/deepcodeai/"&gt;LinkedIn&lt;/a&gt; … just no office visits for now :-)&lt;/p&gt;

</description>
      <category>deepcode</category>
      <category>staticcodeanalysis</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>DeepCode's Top Findings #13: Writing Immutable Objects in Python</title>
      <dc:creator>cu_0xff 🇪🇺</dc:creator>
      <pubDate>Fri, 10 Apr 2020 08:57:49 +0000</pubDate>
      <link>https://dev.to/deepcode/deepcode-s-top-findings-13-writing-immutable-objects-in-python-18i2</link>
      <guid>https://dev.to/deepcode/deepcode-s-top-findings-13-writing-immutable-objects-in-python-18i2</guid>
      <description>&lt;p&gt;&lt;a href="https://deepcode.ai" rel="noopener noreferrer"&gt;DeepCode&lt;/a&gt; offers an AI-based Static Program Analysis for Java, JavaScript and TypeScript, C/C++ and Python. As you might know, DeepCode uses thousands of open source repos to train our engine. We asked the engine team to provide some stats on the findings. On the top suggestions from our engine, we want to introduce and give some background in this series of blog articles.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Language:&lt;/em&gt; Python&lt;br&gt;
&lt;em&gt;Defect:&lt;/em&gt; X is an immutable object and one of its elements gets assigned&lt;br&gt;
&lt;em&gt;Diagnose:&lt;/em&gt; The code tries to write to an element within an instance of an immutable data type &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhmhbbykvqswk7oomu4c2.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhmhbbykvqswk7oomu4c2.PNG" alt="Writing into Range"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I found this example in &lt;a href="https://github.com/monkeylyf/interviewjam" rel="noopener noreferrer"&gt;monkeylyf / interviewjam&lt;/a&gt; and as usual, you can open the repo in &lt;a href="https://deepcode.ai" rel="noopener noreferrer"&gt;DeepCode&lt;/a&gt; and follow along. Below is the snippet of the dashboard in DeepCode:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;findCelebrityWithExtraSpace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    :type n: int
    :rtype: int
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="bp"&gt;...&lt;/span&gt;
            &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&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;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
&lt;span class="bp"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;DeepCode gives us the feedback "Trying to store a value in element of immutable type range (from call to range) will lead to a crash." But does &lt;code&gt;range()&lt;/code&gt; not bring a list back? Well, that used to be...&lt;/p&gt;

&lt;p&gt;Let us replay what happens in the Python interpreter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;Python&lt;/span&gt; &lt;span class="mf"&gt;3.8&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;span class="n"&gt;tags&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;v3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mf"&gt;8.1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="n"&gt;b293b6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Dec&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt; &lt;span class="mi"&gt;2019&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;39&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;MSC&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;1916&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt; &lt;span class="nf"&gt;bit &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Intel&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt; &lt;span class="n"&gt;win32&lt;/span&gt;
&lt;span class="n"&gt;Type&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;help&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;copyright&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;credits&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;license&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;more&lt;/span&gt; &lt;span class="n"&gt;information&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;
&lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&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;stack&lt;/span&gt;&lt;span class="p"&gt;[&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="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&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;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
&lt;span class="nc"&gt;Traceback &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;most&lt;/span&gt; &lt;span class="n"&gt;recent&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;&amp;lt;stdin&amp;gt;&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;module&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nb"&gt;TypeError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;range&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="nb"&gt;object&lt;/span&gt; &lt;span class="n"&gt;does&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;support&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="n"&gt;assignment&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we are calling here is the &lt;code&gt;range()&lt;/code&gt; function. In Python3 it is a renamed version of Python2's &lt;code&gt;xrange()&lt;/code&gt; function. This means it does not actually produce a list with a sequence of values. Rather, it uses yielding to produce the specific value when it is needed instead of storing vast amounts of data. That is the reason why Python says the &lt;code&gt;range&lt;/code&gt; &lt;em&gt;object&lt;/em&gt; does not support item assignment. And by the way, it also does not support the &lt;code&gt;pop()&lt;/code&gt; function which is used in the code example.&lt;/p&gt;

&lt;p&gt;In the above code example, the application would crash when trying to write into the &lt;code&gt;range&lt;/code&gt; object. Yet, as ranges are used often as a sequencer for loops, ranges can get quite large. Thus it makes sense to use such a range object instead of a list. Yet, if you need the list as a datastore - such as in our example here - there is an easy way to get &lt;code&gt;lists&lt;/code&gt; with value sequences:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the static code analysis, the task here was to understand the underlying type returned by the &lt;code&gt;range()&lt;/code&gt; function and infer the abilities the type has (like supporting index operators).&lt;/p&gt;

&lt;p&gt;Test it yourself and run analysis over your code. It is very fast and free to check out at &lt;a href="https://deepcode.ai" rel="noopener noreferrer"&gt;deepcode.ai&lt;/a&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Java Syntax Versus Semantics by Using Some GitPod...</title>
      <dc:creator>cu_0xff 🇪🇺</dc:creator>
      <pubDate>Thu, 02 Apr 2020 06:38:12 +0000</pubDate>
      <link>https://dev.to/deepcode/java-syntax-versus-semantics-by-using-some-gitpod-5elg</link>
      <guid>https://dev.to/deepcode/java-syntax-versus-semantics-by-using-some-gitpod-5elg</guid>
      <description>&lt;p&gt;&lt;a href="https://deepcode.ai"&gt;DeepCode&lt;/a&gt; offers an AI-based Static Program Analysis for Java, JavaScript and TypeScript, C/C++ and Python. &lt;/p&gt;

&lt;p&gt;Let us have fun and do something cool to start, just follow along and I promise you will enjoy it. No need to install anything on your machine.&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 1: Let us open the project in an IDE
&lt;/h1&gt;

&lt;p&gt;First, let us open the project in a Visual-Studio-Code-like IDE. You can do so by simply clicking on the following link:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gitpod.io/#github.com/confluentinc/ksql"&gt;https://gitpod.io/#github.com/confluentinc/ksql&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You might have to log in with you GitHub-Account. Then it will open the repo on GitPod. This means GitPod builds a container with the repo for you and provides a browser-based IDE on top. It only takes a few seconds.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KlRqg3SK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5eoceh2yujzzd5qz6gfi.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KlRqg3SK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5eoceh2yujzzd5qz6gfi.PNG" alt="Project in GitPod"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 2: Get DeepCode analysing the repo
&lt;/h1&gt;

&lt;p&gt;Now, simply follow this link&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/DeepCodeAI/vscode-extension/releases"&gt;https://github.com/DeepCodeAI/vscode-extension/releases&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will find our latest release for the Visual Studio Extension (the &lt;em&gt;vsix&lt;/em&gt;-file) there. Just download it somewhere on your machine where it comes handy for later drag-n-drop.&lt;br&gt;
Back on your GitPod, find the Extensions Icon (shown below) and click on it&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O86UEa-N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xcd5swa7mffuhulk7234.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O86UEa-N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xcd5swa7mffuhulk7234.PNG" alt="Extensions Logo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In there, you have two options: Either install for the project only (upper box) or for your whole account (lower box). The decision is yours in which field you want to drag-n-drop the &lt;em&gt;vsix&lt;/em&gt;-file that we just downloaded.&lt;br&gt;
As soon as you did that, the extension is uploaded and you will the following message on the lower right side:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UAWDIWt7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7gp227dwfcwhw55sysna.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UAWDIWt7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7gp227dwfcwhw55sysna.PNG" alt="Confirm"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Please click &lt;em&gt;Confirm and start analysis&lt;/em&gt; to start the analysis. It should take only a few seconds.&lt;br&gt;
Cool, you made it.&lt;/p&gt;
&lt;h1&gt;
  
  
  Step 3: Let's inspect the code
&lt;/h1&gt;

&lt;p&gt;Within a few seconds, DeepCode should have done the analysis and you can find the results listed in the &lt;em&gt;Problems&lt;/em&gt; tab:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TcpZsgpo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/dtno4joowzrt6taxzw93.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TcpZsgpo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/dtno4joowzrt6taxzw93.PNG" alt="Problems Tab"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let us talk about one issue at hand here. By following the red circles on the icons, navigate to the following file: &lt;em&gt;ksqldb-engine/src/main/java/io/confluent/ksql/function/UserFunctionLoader.java&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In line 87, you find the following problem:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GaR0bVeC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/b2lcp52pti7ocmydfape.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GaR0bVeC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/b2lcp52pti7ocmydfape.PNG" alt="Problem in File"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code highlighted is shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="o"&gt;...&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// load functions packaged as part of ksql first&lt;/span&gt;
    &lt;span class="n"&gt;loadFunctions&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parentClassLoader&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;empty&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;loadCustomerUdfs&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;try&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;pluginDir&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;exists&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;pluginDir&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isDirectory&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
          &lt;span class="no"&gt;LOGGER&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;info&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
              &lt;span class="s"&gt;"UDFs can't be loaded as as dir {} doesn't exist or is not a directory"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
              &lt;span class="n"&gt;pluginDir&lt;/span&gt;
          &lt;span class="o"&gt;);&lt;/span&gt;
          &lt;span class="k"&gt;return&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;h1&gt;
  
  
  Step 4: What do we see here?
&lt;/h1&gt;

&lt;p&gt;The &lt;code&gt;if&lt;/code&gt;-conditional is the interesting part here. It seems the developer wanted to check if the &lt;code&gt;pluginDir&lt;/code&gt; exists and if it is a directory. What he did was that first, it tests if &lt;code&gt;pluginDir&lt;/code&gt; does &lt;em&gt;not&lt;/em&gt; exist and that it is also not a directory - which in the case it does not exist, is kind of trivial.&lt;br&gt;
From a pure syntactical perspective, there is nothing wrong with this code. But from a semantical perspective, it looks fishy. DeepCode's suggestion highlights this.&lt;/p&gt;

&lt;p&gt;The strength of DeepCode showing here is the knowledge of the relationship between &lt;code&gt;exists()&lt;/code&gt; and &lt;code&gt;isDirectory()&lt;/code&gt;. Not &lt;code&gt;exists()&lt;/code&gt; defines the result of &lt;code&gt;isDirectory()&lt;/code&gt;. DeepCode learned that a negative result of the first automatically results in a negative result in the latter. Therefore, the logical combination we see in this conditional makes no sense.&lt;/p&gt;

&lt;p&gt;DeepCode, therefore, argues that the code pretty likely does not reflect the intention of the developer.&lt;/p&gt;

&lt;p&gt;We are happy to help! Try it yourself on your code by using our web-dashboard at &lt;a href="https://deepcode.ai"&gt;deepcode.ai&lt;/a&gt; or using one of our plugins for Visual Studio Code or Atom.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>DeepCode's Top Findings #12: Integer Promotion on Bitwise Operations in C</title>
      <dc:creator>cu_0xff 🇪🇺</dc:creator>
      <pubDate>Thu, 19 Mar 2020 08:14:22 +0000</pubDate>
      <link>https://dev.to/deepcode/deepcode-s-top-findings-12-integer-promotion-on-bitwise-operations-in-c-42o4</link>
      <guid>https://dev.to/deepcode/deepcode-s-top-findings-12-integer-promotion-on-bitwise-operations-in-c-42o4</guid>
      <description>&lt;p&gt;&lt;a href="https://deepcode.ai"&gt;DeepCode&lt;/a&gt; offers an AI-based Static Program Analysis for Java, JavaScript and TypeScript, C/C++ and Python. As you might know, DeepCode uses thousands of open source repos to train our engine. We asked the engine team to provide some stats on the findings. On the top suggestions from our engine, we want to introduce and give some background in this series of blog articles.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Language:&lt;/em&gt; C&lt;br&gt;
&lt;em&gt;Defect:&lt;/em&gt; Integer Promotion on Bitwise Operations&lt;br&gt;
&lt;em&gt;Diagnose:&lt;/em&gt; Cast the result of shift left to unsigned short to avoid unexpected behavior because of integral type promotion. The shifted expression is promoted to unsigned int, which may introduce a number of unknown bits.&lt;/p&gt;

&lt;p&gt;This example is sponsored by Linux in the Alpha architecture (see &lt;a href="https://www.deepcode.ai/app/gh/torvalds/linux/fb33c6510d5595144d585aa194d377cf74d31911/_/%2Farch%2Falpha%2Finclude%2Fasm%2Fdma.h/cpp%2Fdc%2FBinaryOperationTypePromotion/358/code/"&gt;here&lt;/a&gt; ). Obviously, you can load them also in your own dashboard.&lt;/p&gt;

&lt;p&gt;So, here is the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;__inline__&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;get_dma_residue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;dmanr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;io_port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dmanr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt;&lt;span class="mi"&gt;3&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;span class="n"&gt;dmanr&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;IO_DMA1_BASE&lt;/span&gt;
                     &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;dmanr&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&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="n"&gt;IO_DMA2_BASE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="cm"&gt;/* using short to get 16-bit wrap around */&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;short&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;dma_inb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;io_port&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;dma_inb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;io_port&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;8&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;dmanr&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What we should observe is there &lt;code&gt;count&lt;/code&gt; variable which type is &lt;code&gt;unsigned short&lt;/code&gt;. The function returns the type &lt;code&gt;int&lt;/code&gt; and in the return statement, we have a conditional that either returns &lt;code&gt;count&lt;/code&gt;(which would need a typecast to &lt;code&gt;int&lt;/code&gt;) or a bitwise shift left of &lt;code&gt;count&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The type conversion rules for bitwise operations in C are actually not that straight forward. The long explanation - as pointed out by DeepCode with the link &lt;em&gt;More Info&lt;/em&gt; - is &lt;a href="https://wiki.sei.cmu.edu/confluence/display/c/INT02-C.+Understand+integer+conversion+rules"&gt;here&lt;/a&gt;. Under the hood, C converts operants oftentimes to &lt;code&gt;int&lt;/code&gt;, applies the operation, and truncates the result to fit into the target variable. Bitwise operators are such an example.&lt;/p&gt;

&lt;p&gt;Given the specific architecture, the implicit typecast between &lt;code&gt;unsigned short&lt;/code&gt; and &lt;code&gt;int&lt;/code&gt; might just work out perfectly. I would argue it is hard to follow the intent of the developer and he was aware (therefor the comment). Still, following the best practices, he should do explicit type conversions (see also the link above).&lt;/p&gt;

</description>
      <category>deepcode</category>
      <category>staticcodeanalysis</category>
      <category>c</category>
      <category>cpp</category>
    </item>
    <item>
      <title>DeepCode - UX updates...</title>
      <dc:creator>cu_0xff 🇪🇺</dc:creator>
      <pubDate>Tue, 10 Mar 2020 15:42:18 +0000</pubDate>
      <link>https://dev.to/deepcode/deepcode-ux-updates-jg1</link>
      <guid>https://dev.to/deepcode/deepcode-ux-updates-jg1</guid>
      <description>&lt;p&gt;Hey,&lt;br&gt;
we constantly get feedback that we incorporate into the product. Based on your feedback, we changed some aspects on our UX and we will do some more in the coming days. Here is a first glance...&lt;/p&gt;

&lt;h1&gt;
  
  
  Suggestions Page
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--E8tnVDcm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/l00kltrz35i8oc9kc01v.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--E8tnVDcm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/l00kltrz35i8oc9kc01v.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the new layout of the Suggestion page. On the top left (Number 1), you see the current repo and you can select which branch to work on using the drop down. Mid top (Number 2), you find how many different suggestions and their category were found. Top right (Number 3), you can find how long it took DeepCode to scan and what amount of files were covered. By clicking on &lt;em&gt;File Coverage&lt;/em&gt;, you can drill down on what file types were scanned or not. Lastly (Number 4), we have the typical &lt;em&gt;Suggestion&lt;/em&gt; tiles.&lt;/p&gt;

&lt;h1&gt;
  
  
  Suggestion Detail Page
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SOISnxsG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/m74l7dj4uctoxp6mmdfn.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SOISnxsG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/m74l7dj4uctoxp6mmdfn.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also a few changes in &lt;em&gt;Suggestion Details&lt;/em&gt;. We implemented the option to flick through the suggestions quickly (top right, Number 1) to get an overview. Also, we included help to add the ignore comments (lower right, Number 2). As you might know, you can switch off suggestions made by DeepCode by using comments. We provide these comments that would switch off the suggestion.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jhiIpezH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6jeibuv012vfsmzv6doi.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jhiIpezH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6jeibuv012vfsmzv6doi.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We hope you find these changes helpful. Also, check out the &lt;a href="https://marketplace.visualstudio.com/items?itemName=DeepCode.deepcode"&gt;Visual Studio Code Extension&lt;/a&gt; and our &lt;a href="https://atom.io/packages/deepcode"&gt;Atom Package&lt;/a&gt;. But - for sure - check us out at &lt;a href="https://deepcode.ai"&gt;deepcode.ai&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>deepcode</category>
      <category>ux</category>
      <category>staticcodeanalysis</category>
    </item>
    <item>
      <title>DeepCode's Top Findings #11: Synchronizing Strings</title>
      <dc:creator>cu_0xff 🇪🇺</dc:creator>
      <pubDate>Tue, 10 Mar 2020 06:12:32 +0000</pubDate>
      <link>https://dev.to/deepcode/deepcode-s-top-findings-11-synchronizing-strings-1oh9</link>
      <guid>https://dev.to/deepcode/deepcode-s-top-findings-11-synchronizing-strings-1oh9</guid>
      <description>&lt;p&gt;DeepCode offers an AI-based Static Program Analysis for Java, JavaScript and TypeScript, and Python. As you might know, DeepCode uses thousands of open source repos to train our engine. We asked the engine team to provide some stats on the findings. On the top suggestions from our engine, we want to introduce and give some background in this series of blog articles.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Language:&lt;/em&gt; Java&lt;br&gt;
&lt;em&gt;Defect:&lt;/em&gt; Do not use Strings for synchronization.&lt;br&gt;
&lt;em&gt;Diagnose:&lt;/em&gt; Do not use Strings for synchronization, this could lead to unwanted deadlocks and race-conditions. Strings with the same value might be represented as the same object in the JVM.&lt;/p&gt;

&lt;p&gt;I found this one in the JDK 14 code. I copied parts of it out to this repo here &lt;a href="https://github.com/CU-0xff/jdk14_parts"&gt;https://github.com/CU-0xff/jdk14_parts&lt;/a&gt; to make things manageable. So, if you want to follow along, open that repo in &lt;a href="https://DeepCode.ai"&gt;DeepCode.ai&lt;/a&gt;. It is a really nasty one, as it forces you to know some of the implementation details of the JVM to understand the problem. Here is the code of interest:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;WindowsPath&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Path&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;
    &lt;span class="c1"&gt;// normalized path&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;
        &lt;span class="c1"&gt;// cache the resolved path (except drive relative paths as the working&lt;/span&gt;
        &lt;span class="c1"&gt;// directory on removal media devices can change during the lifetime&lt;/span&gt;
        &lt;span class="c1"&gt;// of the VM)&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;type&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nc"&gt;WindowsPathType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;DRIVE_RELATIVE&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;synchronized&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;pathForWin32Calls&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WeakReference&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;resolved&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;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Background:
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Synchronized
&lt;/h3&gt;

&lt;p&gt;Let us introduce the idea of &lt;code&gt;synchronized()&lt;/code&gt; first. In a multi-threaded environment, we have the issue to ensure that access to variables is managed properly. Java provides &lt;em&gt;synchronized&lt;/em&gt; as functions or blocks to manage concurrent access. Below is a simple example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ListOfNumbers&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;printListOfNumbers&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&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;number&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="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&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;1&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;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;10&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="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// Simulate some substantial work&lt;/span&gt;
                &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sleep&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&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;number&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d "&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;number&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;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyThread&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;ListOfNumbers&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;flag&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;MyThread&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;ListOfNumbers&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;flag&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;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;l&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;flag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;flag&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="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printListOfNumbers&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;flag&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;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TestSynchronization1&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="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;[])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;ListOfNumbers&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ListOfNumbers&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;&lt;span class="c1"&gt;// only one object&lt;/span&gt;
        &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;MyThread&lt;/span&gt; &lt;span class="n"&gt;t1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MyThread&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;MyThread&lt;/span&gt; &lt;span class="n"&gt;t2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MyThread&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&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;Here we have a class &lt;code&gt;ListOfNumbers&lt;/code&gt;, that stores an integer and prints a series of it by looping and adding it. Now, we have two threads that are working on one object instance of &lt;code&gt;ListOfNumbers&lt;/code&gt; and it happens as you would expect it. We would like to see &lt;code&gt;1 2 3 4 5 6 7 8 9 10 7 14 21 28 35 42 49 56 63 70&lt;/code&gt;on the console, but we get &lt;code&gt;8 8 9 16 24 24 25 32 39 39 46 47 55 55 62 62 69 69 76 77&lt;/code&gt;, then &lt;code&gt;1 1 9 9 17 10 24 25 32 32 40 40 48 48 55 56 57 57 65 65&lt;/code&gt;, then...&lt;/p&gt;

&lt;p&gt;This teaches us two things: (1) Obviously, the two threads are overwriting each other as they are pointing to the same object. (2) The outcome is unpredictable as it depends on the sequence of how the threads are called.&lt;/p&gt;

&lt;p&gt;With a simple change, we can sort the issue:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyThread&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Thread&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="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;synchronized&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printListOfNumbers&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;flag&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;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With this, a thread asks for control over the instance before changing its state by calling the method. So far, so good.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why not using &lt;code&gt;synchronized()&lt;/code&gt; with strings?
&lt;/h3&gt;

&lt;p&gt;In Java, &lt;code&gt;Strings&lt;/code&gt; are immutable; they cannot be changed. If you overwrite an existing one, you will generate a new instance. There are several reasons why the language designers decided to do so, amongst them is that &lt;code&gt;Strings&lt;/code&gt; are thread-safe (as they do not change, several threads can use them concurrently). &lt;em&gt;But&lt;/em&gt; there is something that is called &lt;em&gt;String Pool&lt;/em&gt;. Under the hood, the JVM optimizes the data usage. If the program asks for a string to be created and one with the same content already exists in the pool, the JVM returns the already existing instance instead of creating a new one.&lt;/p&gt;

&lt;p&gt;Now, you get the larger issue of using a &lt;code&gt;String&lt;/code&gt; in &lt;code&gt;synchronized&lt;/code&gt;. You could end up locking your application because two strings have the same content (aka point to the same object) but within the code, they are different variables. Good luck debugging this one. By the way, see the DeepCode-provided explanation in this regard. Spot on I would say. In our example above, the &lt;code&gt;path&lt;/code&gt; variable is a string and it is not guaranteed to be unique. &lt;/p&gt;

&lt;p&gt;If you are using &lt;code&gt;synchronized()&lt;/code&gt;, make sure to use an object that is not a string. One way for our example could be to use &lt;code&gt;synchronized(this)&lt;/code&gt; as it would use an instance of a complex class.&lt;/p&gt;

&lt;p&gt;This example also shows the difference between &lt;em&gt;dynamic&lt;/em&gt; and &lt;em&gt;static&lt;/em&gt; analysis. In the dynamic case, we would have to have the case of having two strings with the same content as a test case or by running the system, falling over it. In the static case, we test if the case is within all possible cases and ring the bell.&lt;/p&gt;

&lt;p&gt;As you see from the text, DeepCode found this issue fixed in 57 projects, so it is not that uncommon. Have a look at your own code on &lt;a href="https://deepcode.ai"&gt;deepcode.ai&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>deepcode</category>
      <category>java</category>
      <category>staticcodeanalysis</category>
    </item>
    <item>
      <title>DeepCode's Top Findings #10: Confusing Use of '!'</title>
      <dc:creator>cu_0xff 🇪🇺</dc:creator>
      <pubDate>Wed, 04 Mar 2020 06:27:19 +0000</pubDate>
      <link>https://dev.to/deepcode/deepcode-s-top-findings-10-confusing-use-of-297</link>
      <guid>https://dev.to/deepcode/deepcode-s-top-findings-10-confusing-use-of-297</guid>
      <description>&lt;p&gt;&lt;a href="https://deepcode.ai"&gt;DeepCode&lt;/a&gt; offers an AI-based Static Program Analysis for Java, JavaScript and TypeScript, and Python. As you might know, DeepCode uses thousands of open source repos to train our engine. We asked the engine team to provide some stats on the findings. On the top suggestions from our engine, we want to introduce and give some background in this series of blog articles.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Language:&lt;/em&gt; JavaScript&lt;br&gt;
&lt;em&gt;Defect:&lt;/em&gt; Confusing Use of '!' (Category Defect)&lt;br&gt;
&lt;em&gt;Diagnose:&lt;/em&gt; Negation applied before &lt;em&gt;instanceof&lt;/em&gt;. Check operator priorities - negation has higher priority than &lt;em&gt;instanceof&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This instance is sponsored by &lt;a href="https://github.com/npm/cli"&gt;npm cli&lt;/a&gt; - as usual, you can open the project in &lt;a href="https://deepcode.ai"&gt;deepcode&lt;/a&gt; and follow along.&lt;/p&gt;

&lt;p&gt;Have a look at the code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;strings&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;TypeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;argument must be an Array[String]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;For me, this is a typical issue of &lt;em&gt;syntax&lt;/em&gt; versus &lt;em&gt;semantics&lt;/em&gt;. As from a syntactical perspective, this is fine code, it compiles, and kind of works. But seeing the double parenthesis, I guess the developer intended a different behaviour. Per definition, the condition will always be &lt;code&gt;false&lt;/code&gt; (see &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof"&gt;MDN&lt;/a&gt; see &lt;em&gt;Not Instance Of&lt;/em&gt;). It is pretty likely the developer made a mistake here.&lt;/p&gt;

&lt;p&gt;DeepCode correctly provides an explanation of the problem. The negation operator (&lt;code&gt;!&lt;/code&gt;) has a higher priority than &lt;code&gt;instanceof&lt;/code&gt; and is there executed before. &lt;code&gt;!&lt;/code&gt; reviews the operand and if the value is &lt;em&gt;truthy&lt;/em&gt; (see &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/truthy"&gt;MDN Truthy&lt;/a&gt; ) negates that and delivers a &lt;code&gt;false&lt;/code&gt;. Otherwise a &lt;code&gt;true&lt;/code&gt;. Then comes instance of and obviously, both of them can never be an &lt;code&gt;Array&lt;/code&gt;. The condition will always be false. The code should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="k"&gt;if&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;span class="nx"&gt;strings&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;TypeError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;argument must be an Array[String]&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;DeepCode has seen this in 202 projects and also provides examples below. For me, these examples are extremely helpful as they nicely show the before / after. Especially when parenthesis and their ordering comes into play, I tend to get blind sometimes. So this helps enormously.&lt;/p&gt;

&lt;p&gt;If you have another 5 minutes to spare, have a look at this table here &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence"&gt;MDN Operator precedence&lt;/a&gt;. Always good to refresh things a bit. My general advise: Instead of trusting on operator precedence wizardry - use correct parenthesis :-)  &lt;/p&gt;

&lt;p&gt;And if you are not sure if you have seen this in your own code, give it a check - &lt;a href="https://deepcode.ai"&gt;deepcode.ai&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>deepcode</category>
      <category>javascript</category>
      <category>codequality</category>
    </item>
    <item>
      <title>DeepCode's Top Findings #9: Deadlocks</title>
      <dc:creator>cu_0xff 🇪🇺</dc:creator>
      <pubDate>Wed, 26 Feb 2020 06:28:41 +0000</pubDate>
      <link>https://dev.to/deepcode/deepcode-s-top-findings-9-deadlocks-f14</link>
      <guid>https://dev.to/deepcode/deepcode-s-top-findings-9-deadlocks-f14</guid>
      <description>&lt;p&gt;&lt;a href="https://deepcode.ai"&gt;DeepCode&lt;/a&gt; offers an AI-based Static Program Analysis for Java, JavaScript and TypeScript, and Python. As you might know, DeepCode uses thousands of open source repos to train our engine. We asked the engine team to provide some stats on the findings. On the top suggestions from our engine, we want to introduce and give some background in this series of blog articles.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Language:&lt;/em&gt; Python&lt;br&gt;
&lt;em&gt;Defect:&lt;/em&gt; Deadlock (Category Defect)&lt;br&gt;
&lt;em&gt;Diagnose:&lt;/em&gt; Using &lt;em&gt;wait()&lt;/em&gt; can deadlock if the child process prints larger output. Use communicate().&lt;/p&gt;

&lt;p&gt;This finding is sponsored by an ansible extension for spacewalk. As usual, you can open it in (deepcode.ai)[&lt;a href="https://deepcode.ai"&gt;https://deepcode.ai&lt;/a&gt;]. The repo is &lt;a href="https://github.com/ansible/ansible"&gt;ansible/ansible&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;spacewalk_report&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="s"&gt;"""Yield a dictionary form of each CSV output produced by the specified
    spacewalk-report
    """&lt;/span&gt;
    &lt;span class="n"&gt;cache_filename&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CACHE_DIR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cache_filename&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; \
            &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cache_filename&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;st_mtime&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;CACHE_AGE&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# Update the cache
&lt;/span&gt;        &lt;span class="n"&gt;fh&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cache_filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'w'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;subprocess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Popen&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;SW_REPORT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;fh&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;fh&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&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;Let us follow along with the code above: First, it generates a filename and checks if either the file does not exist or is outdated. If so, it generates the file. Then it asks subprocess to spawn a command and write the results into the file as the output pipe. Then it waits until this is done and closes the file. The rest is not of importance to us for now.&lt;/p&gt;

&lt;h2&gt;
  
  
  Background:
&lt;/h2&gt;

&lt;p&gt;It made me think about &lt;em&gt;pure functions&lt;/em&gt;. Let me explain a bit. A pure function is defined by two aspects: First, it returns the same value whenever it is called with the same parameters. So, it does not depend on the state of the program or environment. Secondly, it has no side-effects, so no changes to non-local variables or I/O streams. One nice thing on these pure functions is that we can easily test them, right? We can write unit tests easily for example.&lt;/p&gt;

&lt;p&gt;I guess the problem is that we are actually paid for the not pure parts of our applications. Changing the external state by writing data or switching off the oven is what our customers actually want. We hardly can stay in a pure world only.&lt;/p&gt;

&lt;p&gt;The unpure world though is a jungle, my friends. Because unpure means physics kicks in. As an example: In the theory of relational databases no indexes are needed. Because in theory, the access time is zero. In reality, hard drive heads need to move and that takes time. So, indexes speed up access time.&lt;/p&gt;

&lt;p&gt;In our little example, we are calling an external command and wait for it to finish. I mean what could go wrong? &lt;/p&gt;

&lt;p&gt;A deadlock means that two processes are waiting on different resources for each other. The child waits for the parent to do something, while the parent waits for the child. Both of them are in a stalemate. As you can see, DeepCode warns us that &lt;em&gt;wait&lt;/em&gt; can deadlock the child process. In between child and parent, there is a limited pipe I/O buffer. Given the child would fill this buffer with data, the system would force the child to wait until the parent process reads off significant amounts of data from the buffer. With typical mutexes, the developer is normally aware of deadlock situations but with these I/O buffers, developers tend to forget about it.&lt;/p&gt;

&lt;p&gt;It was found to be an issue that was corrected by 41 projects out of our training set. Actually, if you click on more info, it brings you &lt;a href="https://docs.python.org/3.7/library/subprocess.html#subprocess.Popen.wait"&gt;here&lt;/a&gt; which is the official Python documentation telling you the same. It states the same as DeepCode: To use &lt;code&gt;Popen.communicate()&lt;/code&gt; instead. Also, see the examples DeepCode shows to see how to use the command. The &lt;code&gt;communicate&lt;/code&gt; method takes responsibility for avoiding deadlock; it only sends the next chunk of the stdin string when the subprocess is ready to read it, and it only tries to read the next chunk of stdout or stderr when the subprocess is ready to provide it. &lt;/p&gt;

&lt;p&gt;Don't get me wrong: &lt;code&gt;communicate&lt;/code&gt; is not the silver bullet. You should think about the size of your data and choose the proper means to communicate between the sub-processes.&lt;/p&gt;

&lt;p&gt;Yet, there is even more to this story: Obviously, we have no error management here. No time out, no way to find out that the hard-drive just filled up (yeah, physics with these inconvenient issues :-) ). That is simply a bad style.&lt;/p&gt;

&lt;p&gt;Whenever interacting with the external, unpure, world, make sure to ask yourself these questions:&lt;br&gt;
(1) Do I call something that might not come back in time or maybe never?&lt;br&gt;
(2) Do I receive data from an external, untrusted source?&lt;br&gt;
(3) What error conditions might arise resulting from the size or makeup of the data, timing issues?&lt;/p&gt;

&lt;p&gt;With the list in hand, decide what aspects to address in code by handling them and whatnot (as it is too unlikely or pain-versus-gain does not favour a handler). But make sure, you write them down as not handled error cases.&lt;/p&gt;

&lt;p&gt;Next, maybe you want to add some testing on the ones you addressed.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Introducing: The DeepCode CLI</title>
      <dc:creator>cu_0xff 🇪🇺</dc:creator>
      <pubDate>Tue, 25 Feb 2020 08:35:49 +0000</pubDate>
      <link>https://dev.to/deepcode/introducing-the-deepcode-cli-nhk</link>
      <guid>https://dev.to/deepcode/introducing-the-deepcode-cli-nhk</guid>
      <description>&lt;p&gt;Hey,&lt;/p&gt;

&lt;p&gt;DeepCode provides plugins and extensions for Visual Studio Code and Atom. But what about including it into your CI pipeline? Well, there comes the CLI quite handy.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;System:&lt;/em&gt; Ubuntu 18.04.4 LTS&lt;/p&gt;

&lt;h2&gt;
  
  
  Preparations:
&lt;/h2&gt;

&lt;p&gt;There are a few steps I would suggest to do before installing the CLI.&lt;/p&gt;

&lt;p&gt;(1) Make sure you have Python 3 and Pip3 installed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;~$ python3 --version
Python 3.6.9
~$ python3 -m pip --version
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If it is not installed, do so by calling:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt install python3.8
sudo apt install python3-pip
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note:&lt;/em&gt; You might want to use a separate environment (see &lt;a href="https://github.com/pyenv/pyenv"&gt;pyenv&lt;/a&gt;) to separate it and keep things organized. With this, you can easily switch between environments - one of the great strengths of Python.&lt;/p&gt;

&lt;p&gt;(2) Get an API key. On your &lt;a href="https://www.deepcode.ai/app/gh/account"&gt;DeepCode Account Dashboard&lt;/a&gt; you can generate new access tokens (scroll to the bottom).&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z4hjrGab--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/agvi8lcbncnq1ea79ob2.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z4hjrGab--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/agvi8lcbncnq1ea79ob2.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(3) You can store the API key in the config file so you do not have to handle it again. You can also provide the API key using the &lt;code&gt;-a&lt;/code&gt; parameter when calling the CLI. To store it in the config file, use the editor of your choice and generate a text file with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{"api_key": "[Your key goes here]"}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Per default, the name &lt;code&gt;.deepcode.json&lt;/code&gt; is used.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note:&lt;/em&gt; The CLI also supports the command &lt;code&gt;config&lt;/code&gt; that records this file for you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing DeepCode CLI:
&lt;/h2&gt;

&lt;p&gt;(1) Installation is rather simple&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip3 install deepcode
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You might have to restart the shell so that the path is set correctly and the &lt;code&gt;deepcode&lt;/code&gt; command is found.&lt;/p&gt;

&lt;h2&gt;
  
  
  First Look Around:
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Note:&lt;/em&gt; The parameter &lt;code&gt;--help&lt;/code&gt; is always there for you.&lt;/p&gt;

&lt;p&gt;In essence, the CLI supports three main commands: &lt;code&gt;analyze&lt;/code&gt;, &lt;code&gt;config&lt;/code&gt;, &lt;code&gt;login&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;With &lt;code&gt;deepcode login&lt;/code&gt;, the command starts a browser and guides you through the login process. As we have an API key already, we do not need this for now.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;deepcode config&lt;/code&gt; asks you two main questions (the service URL and the API code) and writes them to the default config file &lt;code&gt;.deepcode.json&lt;/code&gt;. The service URL (default is DeepCode's internet-facing service) enables you to point towards your internal DeepCode server. The API key is the key you gathered as described above.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;deepcode analyze&lt;/code&gt; provides the interface to send your code for analysis. There are three main things to be aware of:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;em&gt;Sources:&lt;/em&gt;&lt;/em&gt; You can provide a local path using the &lt;code&gt;-p&lt;/code&gt; option or a remote, GIT repository using the &lt;code&gt;-r&lt;/code&gt; option. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;em&gt;Linters:&lt;/em&gt;&lt;/em&gt; Providing the &lt;code&gt;-l&lt;/code&gt; option asks DeepCode to run the optional linters. One word of warning: The speed of DeepCode analysis is way(!!) faster than the linters. Sending big chunks of source code may slow the answer down significantly when enabling the linters.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;em&gt;Output Format:&lt;/em&gt;&lt;/em&gt; Per default DeepCode returns a JSON format described here: &lt;a href="https://www.deepcode.ai/docs/REST%20APIs%2FBundles"&gt;REST API Bundles&lt;/a&gt; (see GetAnalysis Results). It has the following structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{ "id": string,
  "results": {
  "suggestions": {
    suggestionIndex: {
      "id": string,
      "message": string,
      "severity": 1 | 2 | 3
    },
    ...
  },
  "files": {
    filePath: {
      suggestionIndex: [
        {
          "cols": [number, number],
          "rows": [number, number],
          "markers": [marker, ...]
        },
        ...
      ],
      ...
    },
    ...
  }
 }
 "url", url
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To map the many-to-many relationship between suggestions and files, each suggestion is identified by a short &lt;code&gt;suggestionIndex&lt;/code&gt; string (e.g. &lt;code&gt;"0"&lt;/code&gt;, &lt;code&gt;"1"&lt;/code&gt;, etc). Every suggestion object contains the respective &lt;code&gt;id&lt;/code&gt;, the description message, and a numeric severity (&lt;code&gt;1&lt;/code&gt; for Info suggestions, &lt;code&gt;2&lt;/code&gt; for Warnings, or &lt;code&gt;3&lt;/code&gt; for Critical issues). Each file is identified by its path and maps all the matching suggestions with the respective array of positions in the files. A position uses two ranges &lt;code&gt;[ from , to ]&lt;/code&gt;, one for the rows and one for the columns (cols), to describe an area of the file included between two characters. Each position also has an array of markers with the following structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "msg": [number, number],
  "pos": [
    {
      "cols": [number, number],
      "rows": [number, number]
    },
    ...
  ]
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Each marker object maps a substring of the suggestion's message, identified by the &lt;code&gt;msg&lt;/code&gt; property as a &lt;code&gt;[ from , to ]&lt;/code&gt; range between two characters, to an array of positions in the code (pos) where that substring has a meaningful match. &lt;/p&gt;

&lt;p&gt;Additionally, you can find a unique identifier (&lt;code&gt;id&lt;/code&gt;) and the URL (&lt;code&gt;url&lt;/code&gt;) under which you can find the DeepCode dashboard for your request. The URL is valid for a day from the time you ran the analysis.&lt;/p&gt;

&lt;p&gt;On top, the CLI provides exit codes:&lt;/p&gt;

&lt;p&gt;0 - no issues found&lt;br&gt;
1 - some issues found &lt;br&gt;
2 - Execution was interrupted by the user&lt;br&gt;
3 - Some error happened while executing&lt;/p&gt;
&lt;h2&gt;
  
  
  Example:
&lt;/h2&gt;

&lt;p&gt;When running this command, you request a public demo repo from GitHub to run which results in three suggestions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;deepcode analyze -r https://github.com/CU-0xff/deepcode-vuln-logviewer.git@cu-0xff
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Resources:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://pypi.org/project/deepcode/"&gt;PyPI deepcode CLI&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.deepcode.ai/docs/REST%20APIs%2FBundles"&gt;Developer docu&lt;/a&gt;&lt;/p&gt;

</description>
      <category>deepcode</category>
      <category>staticcodeanalysis</category>
      <category>python</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
