<?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: Unicorn Developer</title>
    <description>The latest articles on DEV Community by Unicorn Developer (@pvsdev).</description>
    <link>https://dev.to/pvsdev</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1110345%2F0615f05a-4746-4c6f-89ff-a4ef95008a77.png</url>
      <title>DEV Community: Unicorn Developer</title>
      <link>https://dev.to/pvsdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pvsdev"/>
    <language>en</language>
    <item>
      <title>C++: Write, shorten, optimize</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Thu, 23 Jul 2026 14:01:55 +0000</pubDate>
      <link>https://dev.to/pvsdev/c-write-shorten-optimize-16il</link>
      <guid>https://dev.to/pvsdev/c-write-shorten-optimize-16il</guid>
      <description>&lt;p&gt;I stumble upon a C++ function that will be a really illustrative example of how refactoring can both shorten and optimize code. Today, we'll need to dust off our human brains in the era of vibe coding and recall what it's like to know what the good is and what the bad is—after all, someone has to do it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fau2ezvb9t42sjbbpg5yn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fau2ezvb9t42sjbbpg5yn.png" alt="1395_Write_Simplify_Optimize/image1.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code snippet below from the vibe-coded project &lt;a href="https://github.com/NVlabs/vibetensor/tree/main" rel="noopener noreferrer"&gt;VibeTensor&lt;/a&gt;. I like exploring such projects as if I were a naturalist studying a new ecosystem—you can meet with my "field reports" &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1354/" rel="noopener noreferrer"&gt;here&lt;/a&gt; and &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1366/" rel="noopener noreferrer"&gt;there&lt;/a&gt;. So, I'm very curious to discover the genesis of new defects and flaws in code. Why? It helps us better understand and describe the challenges code can face in the GenAI era.&lt;/p&gt;

&lt;p&gt;One of my discoveries is code bloat that hurts readability and compilers' optimization. The following example showcases it in all its beauty.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;TensorImpl&lt;/span&gt; &lt;span class="nf"&gt;make_contiguous_tensor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;nd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nd&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="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;acc&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="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;ptrdiff_t&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;ptrdiff_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nd&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="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;size_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;size_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)];&lt;/span&gt;
    &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sz&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="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;ne&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="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;any_zero&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sizes&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="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;any_zero&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="n"&gt;s&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="n"&gt;any_zero&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It may seem that code size and performance are not so critical because they're used in tests.&lt;/p&gt;

&lt;p&gt;However, the code is duplicated across nine files, and ideally, that shouldn't happen at all, but if code is going to spread like this, it should at least stay compact.&lt;/p&gt;

&lt;p&gt;Also, we shouldn't forget that the generated C++ code must be optimized because the C++'s main purpose is to create highly efficient apps. In this case, we have to deal only with the slow test, but in another context, such patterns would significantly downgrade app performance and the negative impact would only grow as more flawed generated code accumulates over time.&lt;/p&gt;

&lt;p&gt;Slow C++ code goes against the idea of this programming language. That's why it's still a must-have to enhance the expertise in determining whether the code is well-written and how it can be refined, shortened, or optimized. Advanced knowledge can also help in regenerating code with better prompts.&lt;/p&gt;

&lt;p&gt;So, let's practice and do some refactoring. Let's start with this code fragment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;ne&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="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;any_zero&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sizes&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="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;any_zero&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="n"&gt;s&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="n"&gt;any_zero&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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;All the array elements are multiplied. If it encounters &lt;code&gt;0&lt;/code&gt;, the loop terminates to avoid unnecessarily processing the remaining array elements because it'll still set the result to zero.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;any_zero&lt;/code&gt; flag resets the variable that stores the product, but we can go easier:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;ne&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="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sizes&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="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="n"&gt;s&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;When we encounter &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;ne&lt;/code&gt; will be set to &lt;code&gt;0&lt;/code&gt; and the loop will end. We have another place for simplification: modern processors perform multiplication operations quickly, so we can do the multiplication first and then check it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;ne&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="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="n"&gt;s&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="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code's functionality hasn't changed, but it's become shorter and, in my opinion, even easier to understand.&lt;/p&gt;

&lt;p&gt;If we already know that the array is small or that performance isn't critical (for example, because the function is called infrequently), we could simplify it even further:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;ne&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="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="n"&gt;s&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's not think about that, since further actions make the loop be absorbed by another one.&lt;/p&gt;

&lt;p&gt;Now let's go back to the beginning of the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;TensorImpl&lt;/span&gt; &lt;span class="nf"&gt;make_contiguous_tensor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;nd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nd&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="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;acc&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="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;ptrdiff_t&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;ptrdiff_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nd&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="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;size_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;size_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)];&lt;/span&gt;
    &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sz&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="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;static_cast&lt;/code&gt; makes code clunky. The first type casting is needed so that certain compilers or static analyzers don't flag odd arithmetic operations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;ptrdiff_t&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;ptrdiff_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nd&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's consider what would happen if we removed the &lt;code&gt;static_cast&lt;/code&gt; and the input array turned out to be empty:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If the array is empty, then &lt;code&gt;nd = 0&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;If we subtract one from an unsigned zero, we get &lt;code&gt;SIZE_MAX&lt;/code&gt;, that is, a very large positive unsigned number.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;SIZE_MAX&lt;/code&gt; of the&lt;code&gt;size_t&lt;/code&gt; type is implicitly converted to &lt;code&gt;ptrdiff_t&lt;/code&gt; and stored in the &lt;code&gt;i&lt;/code&gt; variable. It turns out that &lt;code&gt;i = -1&lt;/code&gt; works as intended. But this is exactly where warnings may be issued. We initialize &lt;code&gt;ptrdiff_t&lt;/code&gt; with a number that is larger than the range of the largest number it can hold. Before C++20, the result was implementation-defined. Since C++20, the behavior is well-defined.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As a result, it'd be better to leave the first &lt;code&gt;static_cast&lt;/code&gt; as is. We can't say the same about the rest. They make no sense at all.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;vector&lt;/code&gt; class, the &lt;code&gt;[]&lt;/code&gt; &lt;a href="https://en.cppreference.com/cpp/container/vector/operator_at" rel="noopener noreferrer"&gt;operator&lt;/a&gt; takes an argument of the &lt;code&gt;size_type&lt;/code&gt; type (typically an alias of &lt;code&gt;size_t&lt;/code&gt;). The &lt;code&gt;i&lt;/code&gt; variable value will automatically be converted to &lt;code&gt;size_t&lt;/code&gt;, and there's nothing unsafe or suspicious about it. The explicit casts only add visual noise, so they're better removed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;ptrdiff_t&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;ptrdiff_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nd&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="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sz&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="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sz&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;You've taken the first step. But can we simplify this line even further? Let's see—I'd like to write something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ranges&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;[&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="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sz&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="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sz&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;You still need the &lt;code&gt;i&lt;/code&gt; variable to iterate through the &lt;code&gt;strides&lt;/code&gt; array from the end, so we can't eliminate &lt;code&gt;i&lt;/code&gt; completely. But we can do something like that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;acc&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="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nd&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ranges&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sz&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="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sz&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;To be honest, I don't like that &lt;code&gt;--i&lt;/code&gt; has made the code more complicated and forces us to pause and realize why the variable is decremented in the beginning and then is used to access an array element.&lt;/p&gt;

&lt;p&gt;Okay, there is less code now, and it's easier to scan through it quickly, so half the work is done. But the code is still ultimately hard to read—it's neither clearer nor intrinsically harder.&lt;/p&gt;

&lt;p&gt;Let's take a look at the result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;TensorImpl&lt;/span&gt; &lt;span class="nf"&gt;make_contiguous_tensor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;nd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nd&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="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;acc&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="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nd&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ranges&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sz&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="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;ne&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="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="n"&gt;s&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="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that there is less code, it becomes clear that the second loop is redundant. In the first loop, we iterate through all the elements. So why not multiply them right away?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;TensorImpl&lt;/span&gt; &lt;span class="nf"&gt;make_contiguous_tensor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;nd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nd&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="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;acc&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="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nd&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;ne&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="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ranges&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sz&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="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Oh, it's an absolute beauty. It's not a big deal that we multiply all the elements together, even though one of them might be zero. Microprocessors are multiplying rapidly these days. You could lose more by re-accessing all the elements in the second loop.&lt;/p&gt;

&lt;p&gt;What else is left? We don't need to initialize the &lt;code&gt;strides&lt;/code&gt; container with zeros. All of its elements will be overwritten anyway.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nd&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="c1"&gt;// delete the second argument&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I think that's all. We can also make one more small change: remove the &lt;code&gt;nd&lt;/code&gt; variable. It's not needed here or in the code that follows.&lt;/p&gt;

&lt;p&gt;As a result, we have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;TensorImpl&lt;/span&gt; &lt;span class="nf"&gt;make_contiguous_tensor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;acc&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="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;ne&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="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ranges&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sz&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="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hooray! We cut the code in half: from 20 lines down to just 9!&lt;/p&gt;

&lt;h2&gt;
  
  
  What's inside the assembly code?
&lt;/h2&gt;

&lt;p&gt;The function is shorter now, but does that also improve performance? Yes! Let's take a look at the main section of the assembly code for the original function version. &lt;/p&gt;

&lt;p&gt;Clang is used with the &lt;code&gt;-O2&lt;/code&gt; option:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LBB1_7&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rsi&lt;/span&gt;
        &lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;qword&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;rbx&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;r12&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;rsi&lt;/span&gt;
        &lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qword&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;r13&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;r12&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;cmp&lt;/span&gt;     &lt;span class="n"&gt;rsi&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;adc&lt;/span&gt;     &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="n"&gt;imul&lt;/span&gt;    &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rax&lt;/span&gt;
        &lt;span class="n"&gt;dec&lt;/span&gt;     &lt;span class="n"&gt;r12&lt;/span&gt;
        &lt;span class="n"&gt;cmp&lt;/span&gt;     &lt;span class="n"&gt;r12&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;ja&lt;/span&gt;      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LBB1_7&lt;/span&gt;
        &lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;r12d&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;cmp&lt;/span&gt;     &lt;span class="n"&gt;rbp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r13&lt;/span&gt;
        &lt;span class="n"&gt;je&lt;/span&gt;      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LBB1_9&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LBB1_4&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qword&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;r13&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;test&lt;/span&gt;    &lt;span class="n"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rax&lt;/span&gt;
        &lt;span class="n"&gt;je&lt;/span&gt;      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LBB1_5&lt;/span&gt;
        &lt;span class="n"&gt;imul&lt;/span&gt;    &lt;span class="n"&gt;r12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rax&lt;/span&gt;
        &lt;span class="n"&gt;add&lt;/span&gt;     &lt;span class="n"&gt;r13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;
        &lt;span class="n"&gt;cmp&lt;/span&gt;     &lt;span class="n"&gt;r13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rbp&lt;/span&gt;
        &lt;span class="n"&gt;jne&lt;/span&gt;     &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LBB1_4&lt;/span&gt;
        &lt;span class="n"&gt;jmp&lt;/span&gt;     &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LBB1_9&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see a rather verbose assembly code with two loops. Now let's compare it with the assembly generated for the refactored version.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LBB1_10&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rsi&lt;/span&gt;
        &lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qword&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;rbp&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;add&lt;/span&gt;     &lt;span class="n"&gt;rbp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;
        &lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;qword&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;rsi&lt;/span&gt;
        &lt;span class="n"&gt;cmp&lt;/span&gt;     &lt;span class="n"&gt;rdx&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;mov&lt;/span&gt;     &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rdx&lt;/span&gt;
        &lt;span class="n"&gt;adc&lt;/span&gt;     &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="n"&gt;imul&lt;/span&gt;    &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rcx&lt;/span&gt;
        &lt;span class="n"&gt;imul&lt;/span&gt;    &lt;span class="n"&gt;r12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rdx&lt;/span&gt;
        &lt;span class="n"&gt;add&lt;/span&gt;     &lt;span class="n"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;
        &lt;span class="n"&gt;cmp&lt;/span&gt;     &lt;span class="n"&gt;rbp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r13&lt;/span&gt;
        &lt;span class="n"&gt;jne&lt;/span&gt;     &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LBB1_10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Oh, that's pure beauty. One loop. Compact and efficient. Wow!&lt;/p&gt;

&lt;p&gt;To those who aren't very familiar with the assembly language, it might seem strange that there isn't a conditional jump to implement this: &lt;code&gt;sz == 0 ? 1 : sz&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;
  Here's how it works
  &lt;br&gt;
The value of &lt;code&gt;acc&lt;/code&gt; is stored in the &lt;code&gt;RSI&lt;/code&gt; register. We'll need a copy of this value later, so it's stored in the &lt;code&gt;RCX&lt;/code&gt; register.&lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rsi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The &lt;code&gt;RBP&lt;/code&gt; register contains the address used to iterate through the elements of the &lt;code&gt;sizes&lt;/code&gt; array in reverse order. Here, we load the element into the &lt;code&gt;sz&lt;/code&gt; variable (&lt;code&gt;RDX&lt;/code&gt;) and shift the iterator by 8 bytes (the size of the &lt;code&gt;int64_t&lt;/code&gt; element) to the left.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qword&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;rbp&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;add&lt;/span&gt;     &lt;span class="n"&gt;rbp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;RSI&lt;/code&gt; is &lt;code&gt;acc&lt;/code&gt;. Let's put the register in the &lt;code&gt;strides&lt;/code&gt; array. &lt;code&gt;RAX&lt;/code&gt; holds the element address. The jump to the previous element will be executed below (see &lt;code&gt;add rax, -8&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;qword&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;rsi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we've reached the cherry on top: &lt;code&gt;acc *= (sz == 0 ? 1 : sz)&lt;/code&gt; is executed and the RDX register(&lt;code&gt;sz&lt;/code&gt; variable) is compared with 1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;cmp&lt;/span&gt;     &lt;span class="n"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;   &lt;span class="c1"&gt;// CF = is moved when sz - 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How does it work? &lt;/p&gt;

&lt;p&gt;1 is subtracted from &lt;code&gt;RDX&lt;/code&gt;. If a jump occurs, the carry flag (&lt;code&gt;CF&lt;/code&gt;) is set. This happens only when &lt;code&gt;CF = 1&lt;/code&gt; if &lt;code&gt;RDX&lt;/code&gt; is 0.&lt;/p&gt;

&lt;p&gt;This is &lt;code&gt;acc = sz&lt;/code&gt;. We overwrote the variable's value, but that's okay because we have a copy of it in &lt;code&gt;RCX&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rdx&lt;/span&gt; &lt;span class="c1"&gt;// acc = sz&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;sz&lt;/code&gt; was 0, let's change it to 1. To do this, let's add the &lt;code&gt;CF&lt;/code&gt; carry flag. If &lt;code&gt;sz&lt;/code&gt; is not &lt;code&gt;0&lt;/code&gt;, then the carry flag is 0, and nothing will change. That's how the compiler avoided the conditional jump.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;adc&lt;/span&gt;     &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;   &lt;span class="c1"&gt;// acc = acc + 0 + CF&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All that's left is to perform two multiplications, decrement &lt;code&gt;RAX&lt;/code&gt;, and continue the loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;imul&lt;/span&gt;    &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rcx&lt;/span&gt; &lt;span class="c1"&gt;// acc = acc * {ACC copy in RCS register}&lt;/span&gt;
&lt;span class="n"&gt;imul&lt;/span&gt;    &lt;span class="n"&gt;r12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rdx&lt;/span&gt; &lt;span class="c1"&gt;// ne = ne * sz&lt;/span&gt;
&lt;span class="n"&gt;add&lt;/span&gt;     &lt;span class="n"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;
&lt;span class="n"&gt;cmp&lt;/span&gt;     &lt;span class="n"&gt;rbp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r13&lt;/span&gt; &lt;span class="c1"&gt;// Do we reach the beginning of strides?&lt;/span&gt;
&lt;span class="n"&gt;jne&lt;/span&gt;     &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LBB1_10&lt;/span&gt; &lt;span class="c1"&gt;// If no, we'll get goto LBB1_10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;br&gt;
&lt;p&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Another way
&lt;/h2&gt;

&lt;p&gt;Do we have another way? Yes, we have. I don't know whether I think about it or not. Unfortunately, after finishing my own refactoring, I made the mistake of immediately asking AI for alternative solutions.&lt;/p&gt;

&lt;p&gt;DeepSeek suggestions weren't any better, and some were even worse than the original. For example, it optimized one of the code implementations by reversing the array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Wrap out strides&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But Claude Opus came up with an unusual implementation. It was noticed that we don't actually need two independent sequences of multiplications.&lt;/p&gt;

&lt;p&gt;If all elements of the input array are non-zero, then at the end of the loop, &lt;code&gt;acc == ne&lt;/code&gt;. And if even one element was zero, we can simply set &lt;code&gt;ne&lt;/code&gt; to zero at the end. We return to the &lt;code&gt;any_zero&lt;/code&gt; flag again, but in a smarter way. Using this idea, we can write the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;TensorImpl&lt;/span&gt; &lt;span class="nf"&gt;make_contiguous_tensor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;acc&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="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;any_zero&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ranges&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sizes&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;strides&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sz&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="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;any_zero&lt;/span&gt; &lt;span class="o"&gt;|=&lt;/span&gt; &lt;span class="n"&gt;sz&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;ne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;any_zero&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="n"&gt;acc&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;&lt;/p&gt;
  Assembly code
  &lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LBB1_7&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rsi&lt;/span&gt;
        &lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qword&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;r13&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;add&lt;/span&gt;     &lt;span class="n"&gt;r13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;
        &lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;qword&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;rdx&lt;/span&gt;
        &lt;span class="n"&gt;test&lt;/span&gt;    &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rsi&lt;/span&gt;
        &lt;span class="n"&gt;sete&lt;/span&gt;    &lt;span class="n"&gt;dil&lt;/span&gt;
        &lt;span class="n"&gt;cmp&lt;/span&gt;     &lt;span class="n"&gt;rsi&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;adc&lt;/span&gt;     &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="n"&gt;imul&lt;/span&gt;    &lt;span class="n"&gt;rsi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rdx&lt;/span&gt;
        &lt;span class="n"&gt;or&lt;/span&gt;      &lt;span class="n"&gt;al&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dil&lt;/span&gt;
        &lt;span class="n"&gt;add&lt;/span&gt;     &lt;span class="n"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;
        &lt;span class="n"&gt;cmp&lt;/span&gt;     &lt;span class="n"&gt;r13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rbp&lt;/span&gt;
        &lt;span class="n"&gt;jne&lt;/span&gt;     &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LBB1_7&lt;/span&gt;
        &lt;span class="n"&gt;xor&lt;/span&gt;     &lt;span class="n"&gt;r13d&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r13d&lt;/span&gt;
        &lt;span class="n"&gt;test&lt;/span&gt;    &lt;span class="n"&gt;al&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;cmove&lt;/span&gt;   &lt;span class="n"&gt;r13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rsi&lt;/span&gt;
        &lt;span class="n"&gt;mov&lt;/span&gt;     &lt;span class="n"&gt;r14&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r8&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Will code that performs half as many multiplications run faster? My prediction—not necessarily. The processor can perform two unrelated multiplications simultaneously on different pipelines. I'm willing to bet that my version and Claude's version will run at almost the same speed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Performance measurement
&lt;/h2&gt;

&lt;p&gt;Let's see how these implementations compare in reality. Designing a fair benchmark took a bit of effort. First, I had to ensure the input data couldn't overflow a 64-bit signed integer, since signed overflow is &lt;a href="https://pvs-studio.com/en/blog/terms/0066/" rel="noopener noreferrer"&gt;undefined behavior&lt;/a&gt; and would invalidate the measurements. Second, to make the contest fair, the test cases alternated between arrays with and without zero elements. Finally, the input arrays had varying lengths.&lt;/p&gt;

&lt;p&gt;I'm not claiming that the measurements are rigorous, but they represent a general idea. The speed of the first original algorithm is taken as the baseline. I used Clang with the &lt;code&gt;-O2&lt;/code&gt; and &lt;code&gt;-m64&lt;/code&gt; options.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The original version with two loops: &lt;strong&gt;1&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;My version with a single loop: &lt;strong&gt;0.88&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The Claude version with a single multiplication: &lt;strong&gt;0.92&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The refactoring improved performance by about 10% on average. I was actually hoping for a 20–30% speedup, but 10% is still a nice result—especially considering that it came from simplifying the code rather than making it more complicated.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrap-up
&lt;/h2&gt;

&lt;p&gt;Let's stop our little refactoring adventure here. Was it worth it? &lt;/p&gt;

&lt;p&gt;From the perspective of improving this particular piece of code, I'd say no. Tests are generated. It's not critical that code is longer and slower than it could be.&lt;/p&gt;

&lt;p&gt;It doesn't matter whether code is written by a human or generated by AI. One thing remains constant, someone must recognize what bad is and what good is. There must be experts who can spot the difference. Is the code good? Is it bad? Is the code fast? Is it secure?&lt;/p&gt;

&lt;p&gt;Some people can accept just the fact that the code works—and who cares what's inside it. In some cases—for example, for building a prototype—this is reasonable. However, those who aren't interested in architecture, optimization, or security most likely simply don't see or understand the whole picture of developing and maintaining large software products. So, I think we'll likely see a lot of drawbacks on this topic.&lt;/p&gt;

&lt;p&gt;AI is a powerful tool, but it's not a magic wand. When Claude suggested an alternative version of the algorithm to me, it was a good example of how AI can be used for research and to enhance human capabilities. But it can just as easily cause harm if used carelessly.&lt;/p&gt;

&lt;p&gt;If you want to continue improving your skills as a C++ specialist and create reliable, secure software, I suggest you check out these useful e-books and webinars on our website:&lt;/p&gt;

&lt;h2&gt;
  
  
  E-books &amp;amp; Webinars
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1215/" rel="noopener noreferrer"&gt;C++ programmer's guide to undefined behavior&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://pvs-studio.com/en/book-giveaway/" rel="noopener noreferrer"&gt;Terrible tips for a C++ developer&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://youtube.com/playlist?list=PLGVoaOmC1PBw&amp;amp;si=fZ4RgpnxGZs5y1uh" rel="noopener noreferrer"&gt;Webinar series: Let's make a programming language&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>cpp</category>
      <category>programming</category>
      <category>vibecoding</category>
    </item>
    <item>
      <title>Let's make a programming language. AST—Key points</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Wed, 22 Jul 2026 12:50:23 +0000</pubDate>
      <link>https://dev.to/pvsdev/lets-make-a-programming-language-ast-key-points-3ld8</link>
      <guid>https://dev.to/pvsdev/lets-make-a-programming-language-ast-key-points-3ld8</guid>
      <description>&lt;p&gt;In this part, we continue the development of a toy programming language by introducing the Abstract Syntax Tree (AST), the data structure that represents the hierarchical structure of source code after parsing.&lt;/p&gt;

&lt;h2&gt;
  
  
  About the speaker
&lt;/h2&gt;

&lt;p&gt;Yuri Minaev is an experienced C++ developer, architect at PVS-Studio, and a recognized voice in the C++ community who has spoken at CppCast, C++ on Sea, and CppCon. Over the course of ten sessions, he'll guide you through each stage of building your own programming language.&lt;/p&gt;

&lt;h2&gt;
  
  
  The foundation
&lt;/h2&gt;

&lt;p&gt;Each language construct corresponds to an AST node, while collections of nodes, such as function bodies or parameter lists, are represented by dedicated list nodes. Yuri explains the core node types, including literals, identifiers, unary and binary expressions, assignments, function calls, declarations, statements, and error nodes. &lt;/p&gt;

&lt;p&gt;Error nodes allow parsing to continue after syntax errors, enabling the compiler to collect multiple errors instead of stopping at the first one. Invalid nodes propagate their state upward so the entire program can be marked invalid before evaluation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's grow our tree!
&lt;/h2&gt;

&lt;p&gt;A central component of the implementation is the AST builder, which owns every created node and manages memory automatically. As the parser recognizes grammar rules, it instructs the builder to create nodes. Terminal nodes are stored in an internal state, while compound nodes consume previously created child nodes from that state and combine them into larger structures until the root node of the program is produced.&lt;/p&gt;

&lt;h2&gt;
  
  
  Traversing the Tree
&lt;/h2&gt;

&lt;p&gt;The second half of the talk focuses on traversing the AST with a visitor. The visitor performs a depth-first traversal using preview and visit functions. Preview executes when entering a node and can decide whether to traverse its children, while visit runs after returning from them. This design allows specialized visitors to process only the node types they need. &lt;/p&gt;

&lt;p&gt;As an example, Yuri demonstrates an AST printer that uses the visitor to produce a tree-like representation of the program structure, concluding with a preview of future topics covering variables, types, semantics, functions, and evaluation.&lt;/p&gt;

&lt;p&gt;Want more?&lt;/p&gt;

&lt;p&gt;If you want to watch other talks or see the whole episode, &lt;a href="https://www.youtube.com/watch?v=ZY7N6ALq2Bw" rel="noopener noreferrer"&gt;follow this link.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also sign up for our upcoming webinars, for example: &lt;a href="https://pvs-studio.com/en/webinar/31/" rel="noopener noreferrer"&gt;Let's make a programming language. Variables&lt;/a&gt;. We hope to see you there!&lt;/p&gt;

&lt;p&gt;If you'd like to learn more about PVS-Studio analyzer, check out our &lt;a href="https://pvs-studio.com/en/" rel="noopener noreferrer"&gt;website&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;See ya!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>cpp</category>
      <category>webinar</category>
    </item>
    <item>
      <title>PVS-Studio brings Unreal Engine support to Team licenses</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Mon, 20 Jul 2026 12:58:32 +0000</pubDate>
      <link>https://dev.to/pvsdev/pvs-studio-brings-unreal-engine-support-to-team-licenses-58n</link>
      <guid>https://dev.to/pvsdev/pvs-studio-brings-unreal-engine-support-to-team-licenses-58n</guid>
      <description>&lt;p&gt;Good news for Unreal Engine developers: we've made PVS-Studio more accessible! You can now analyze UE projects in PVS-Studio with a Team license, not just an Enterprise license. We'll explain below why we changed our licensing model and what it means for small and mid-sized game studios.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkzeaf2yb0ocgg2muxbdo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkzeaf2yb0ocgg2muxbdo.png" alt="1394_ue_team/image1.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is PVS-Studio?
&lt;/h2&gt;

&lt;p&gt;In case you haven't heard of us yet: PVS-Studio is a static analyzer for C, C++, C#, and Java code (+ JavaScript/TypeScript and Go in the upcoming August release). PVS-Studio is a tool that automatically checks your project's code for potential errors, security vulnerabilities, and optimization opportunities without executing the code itself.&lt;/p&gt;

&lt;p&gt;If you'd like to learn more about the analyzer's capabilities and use cases, check out our &lt;a href="https://pvs-studio.com/en/blog/posts/1358/" rel="noopener noreferrer"&gt;overview article&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  PVS-Studio is now more accessible to Unreal Engine developers
&lt;/h2&gt;

&lt;p&gt;Game projects continue to grow in both scale and complexity. A modern game can contain millions of code lines, hundreds of modules, and countless interconnected systems. Even experienced teams may miss bugs that slip past the compiler and code review.&lt;/p&gt;

&lt;p&gt;At the same time, a large part of the industry consists of indie and AA studios. Despite having smaller budgets and fewer developers, such teams tackle complex engineering challenges and care just as much about delivering high-quality products.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PVS-Studio 7.43, released in June 2026, introduced an important change: Unreal Engine project analysis is now available with the Team license instead of being limited to Enterprise. This &lt;a href="https://pvs-studio.com/en/order/" rel="noopener noreferrer"&gt;makes the tool more affordable&lt;/a&gt; for small and mid-sized game studios and lowers the barrier to adopting static analysis as part of their daily development workflow.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Why did we make this change?
&lt;/h3&gt;

&lt;p&gt;Unreal Engine has long been the go-to option for teams of all sizes, from AAA studios and AA developers to indie teams and solo creators. For many of them, the Enterprise license was excessive, especially when working within a tight budget and carefully selecting development tools.&lt;/p&gt;

&lt;p&gt;According to the &lt;a href="https://gdconf.com/article/gdc-2026-state-of-the-game-industry-reveals-impact-of-layoffs-generative-ai-and-more/" rel="noopener noreferrer"&gt;2026 State of the Game Industry&lt;/a&gt; report, Unreal Engine is now the primary engine for the largest percentage of game developers, with approximately 42% of respondents using it as their main engine. It's particularly popular among AA studios, where the engine is used even more broadly than in the AAA segment.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F03vxnwfvqm1trtt38z7o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F03vxnwfvqm1trtt38z7o.png" alt="1394_ue_team/image2.png" width="664" height="272"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Figure 1—GDC 2026 State of the Game Industry Report: What game engine do you primarily use for game development?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Like larger studios, these teams face technical challenges such as complex game logic, ever-growing codebases, third-party integrations, platform-specific issues, and hard-to-reproduce bugs. The difference is that they usually have fewer resources available for extensive debugging, manual code reviews, and dedicated QA workflows.&lt;/p&gt;

&lt;p&gt;QA often takes a back seat while the team focuses on building gameplay features. As a result, bugs negatively impact the player experience and damage the game's reputation right at release. In smaller studios, developers often have to handle testing themselves because the team isn't big enough and the &lt;a href="https://globalstep.com/blog/game-qa-in-indie-vs-aaa-studios-whats-the-difference/" rel="noopener noreferrer"&gt;budget may not allow&lt;/a&gt; for hiring dedicated QA specialists.&lt;/p&gt;

&lt;p&gt;The Team license is designed with such teams in mind. By adding Unreal Engine support at this licensing tier, we've made static analysis more accessible where it can have the greatest impact: in teams where catching bugs early saves time, money, and effort while helping developers stay focused on creating great games.&lt;/p&gt;

&lt;h2&gt;
  
  
  How bugs affect development pace
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The cost of an error
&lt;/h3&gt;

&lt;p&gt;Players pay especially close attention to the quality of a game during the first few days after its launch. If bugs, crashes, or critical failures arise during that period, the team must immediately shift its focus to analyzing feedback, deploying hotfixes, and stabilizing the game. This means investing time and money on work that is rarely part of the intended project plan.&lt;/p&gt;

&lt;p&gt;High-profile releases such as Assassin's Creed Unity and &lt;a href="https://www.playstationlifestyle.net/2020/12/17/cdpr-founders-lose-1b-after-cyberpunk-2077-release/" rel="noopener noreferrer"&gt;Cyberpunk 2077&lt;/a&gt; demonstrate that technical issues at launch can lead to months of post-release fixes. Teams with limited resources may find it even more challenging to recover from a rocky launch.&lt;/p&gt;

&lt;p&gt;In reality, the benefits of catching bugs early extend beyond saving money. Understanding and fixing the issue is usually easier when the code is still fresh in the developer's mind. If the bug surfaces much later, the team has to reconstruct the original context, untangle dependencies, and stop working on other projects to investigate it.&lt;/p&gt;

&lt;p&gt;Static analysis naturally fits into everyday development workflows, including local development, builds, and CI. It helps developers spot suspicious code before fixes require lengthy investigations, repeated testing, or urgent patches.&lt;/p&gt;

&lt;p&gt;If you'd like to learn more, check out our article on &lt;a href="https://pvs-studio.com/en/blog/posts/1373/" rel="noopener noreferrer"&gt;how static analysis affects the cost of fixing errors throughout a project's lifecycle&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What does this mean for a team?
&lt;/h3&gt;

&lt;p&gt;Based on &lt;a href="https://www.reddit.com/r/gamedev/comments/1fv2tmc/why_did_you_start_gamedev_i_know_so_many_people/" rel="noopener noreferrer"&gt;discussions within the community&lt;/a&gt;, it's clear that most people don't pursue a career in game development to spend their days shipping emergency patches or tracking down typos in legacy code. They'd rather focus on designing gameplay mechanics, building systems, creating content, and making their game better.&lt;/p&gt;

&lt;p&gt;The static analyzer doesn't replace developers or make decisions for the team. Its job is much simpler: it highlights potential issues in code and helping teams find bugs earlier with less effort.&lt;/p&gt;

&lt;p&gt;For the team, this means a more streamlined workflow with fewer interruptions and more time to focus on tasks that advance the project.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does PVS-Studio offer for Unreal Engine projects?
&lt;/h2&gt;

&lt;p&gt;Unreal Engine has its own architecture, type system, coding conventions, and internal mechanisms. So, the key to a thorough analysis is considering the engine specifics instead of relying solely on general C++ checks.&lt;/p&gt;

&lt;p&gt;We continue to expand Unreal Engine support in PVS-Studio, including in collaboration with Epic Games. It helps us better account for UE-specific behavior, improve our integrations, and respond more quickly to issues developers encounter when using the analyzer with real-world Unreal Engine projects.&lt;/p&gt;

&lt;p&gt;This translates into a smoother out-of-the-box experience for teams working with Unreal Engine:&lt;/p&gt;

&lt;h4&gt;
  
  
  Annotations for Unreal Engine code
&lt;/h4&gt;

&lt;p&gt;PVS-Studio includes built-in annotations for Unreal Engine. They help the analyzer interpret code more accurately, account for engine-specific behavior, and reduce false positives without requiring extensive manual configuration.&lt;/p&gt;

&lt;h4&gt;
  
  
  UE-specific diagnostic rules
&lt;/h4&gt;

&lt;p&gt;The analyzer also includes diagnostic rules specifically designed for Unreal Engine projects. They help detect issues stemming from UE-specific coding patterns, engine conventions, and the object lifecycle of the engine. Here are a couple of examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://pvs-studio.com/en/docs/warnings/v1100/" rel="noopener noreferrer"&gt;V1100&lt;/a&gt;: Declaring a pointer to a type derived from 'UObject' in a class that is not derived from 'UObject' is dangerous.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://pvs-studio.com/en/docs/warnings/v1102/" rel="noopener noreferrer"&gt;V1102&lt;/a&gt;: Violation of naming conventions may cause Unreal Header Tool to work incorrectly.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Integration with the Unreal Engine toolchain
&lt;/h4&gt;

&lt;p&gt;Our goal is to support the workflows and tools that Unreal Engine developers already rely on. This way, introducing static analysis won't disrupt an established development process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Run PVS-Studio in Unreal Build Tool&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Starting with Unreal Engine 4.17, you can run PVS-Studio directly through Unreal Build Tool. This makes it easy to integrate the analyzer into your existing build pipeline and use it as part of your regular development workflow.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Support for multiple Unreal Engine versions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio supports both legacy and current Unreal Engine releases. This is especially important for teams that can't immediately migrate to the latest engine version and need to continue developing projects using their current version.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Integration with Unreal Build Accelerator, Horde, and SN-DBS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For large projects and CI pipelines, analysis speed and seamless integration with existing build infrastructure are essential. PVS-Studio is compatible with &lt;a href="https://pvs-studio.com/en/docs/manual/0043/#uba" rel="noopener noreferrer"&gt;Unreal Build Accelerator&lt;/a&gt;, Horde, and &lt;a href="https://pvs-studio.com/en/docs/manual/0043/#sndbs" rel="noopener noreferrer"&gt;SN-DBS&lt;/a&gt;. This streamlines the integration of static analysis into existing distributed build and code check workflows.&lt;/p&gt;

&lt;h4&gt;
  
  
  A comprehensive set of diagnostic rules for everyday C++ development
&lt;/h4&gt;

&lt;p&gt;In addition to providing diagnostic rules specific to Unreal Engine, PVS-Studio detects a wide range of issues that can affect any C++ codebase. For example, it can identify:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;memory and resource management issues: memory leaks (&lt;a href="https://pvs-studio.com/en/docs/warnings/v773/" rel="noopener noreferrer"&gt;V773&lt;/a&gt;), use-after-free bugs (&lt;a href="https://pvs-studio.com/en/docs/warnings/v774/" rel="noopener noreferrer"&gt;V774&lt;/a&gt;), and double resource deallocation (&lt;a href="https://pvs-studio.com/en/docs/warnings/v586/" rel="noopener noreferrer"&gt;V586&lt;/a&gt;);&lt;/li&gt;
&lt;li&gt;uses of uninitialized data: in local variables, class members, and constructors (&lt;a href="https://pvs-studio.com/en/docs/warnings/v614/" rel="noopener noreferrer"&gt;V614&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v730/" rel="noopener noreferrer"&gt;V730&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v670/" rel="noopener noreferrer"&gt;V670&lt;/a&gt;);&lt;/li&gt;
&lt;li&gt;object lifetime and reference issues: dangling references, incorrect handling of temporary objects, &lt;code&gt;string_view&lt;/code&gt;, &lt;code&gt;lambda&lt;/code&gt;, and objects after &lt;code&gt;std::move&lt;/code&gt; (&lt;a href="https://pvs-studio.com/en/docs/warnings/v758/" rel="noopener noreferrer"&gt;V758&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v1017/" rel="noopener noreferrer"&gt;V1017&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v1047/" rel="noopener noreferrer"&gt;V1047&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v1030/" rel="noopener noreferrer"&gt;V1030&lt;/a&gt;);&lt;/li&gt;
&lt;li&gt;logic errors in conditions and expressions: conditions that always evaluate to true or false, suspicious comparisons, and incorrect operators (&lt;a href="https://pvs-studio.com/en/docs/warnings/v547/" rel="noopener noreferrer"&gt;V547&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v517/" rel="noopener noreferrer"&gt;V517&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v709/" rel="noopener noreferrer"&gt;V709&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v564/" rel="noopener noreferrer"&gt;V564&lt;/a&gt;);&lt;/li&gt;
&lt;li&gt;copy-paste errors and other human mistakes: duplicated code fragments, variable name typos, and suspicious inconsistencies between similar checks (&lt;a href="https://pvs-studio.com/en/docs/warnings/v778/" rel="noopener noreferrer"&gt;V778&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v722/" rel="noopener noreferrer"&gt;V722&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v1013/" rel="noopener noreferrer"&gt;V1013&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v524/" rel="noopener noreferrer"&gt;V524&lt;/a&gt;);&lt;/li&gt;
&lt;li&gt;integer overflows, unsafe conversions, and signedness issues (&lt;a href="https://pvs-studio.com/en/docs/warnings/v1028/" rel="noopener noreferrer"&gt;V1028&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v1083/" rel="noopener noreferrer"&gt;V1083&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v1112/" rel="noopener noreferrer"&gt;V1112&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v1029/" rel="noopener noreferrer"&gt;V1029&lt;/a&gt;);&lt;/li&gt;
&lt;li&gt;issues with loops, indexing, and array bounds (&lt;a href="https://pvs-studio.com/en/docs/warnings/v557/" rel="noopener noreferrer"&gt;V557&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v621/" rel="noopener noreferrer"&gt;V621&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/docs/warnings/v776/" rel="noopener noreferrer"&gt;V776&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Fast fixes for issues and false positives
&lt;/h4&gt;

&lt;p&gt;Working effectively with Unreal Engine requires a stable and reliable tool. That's why we focus on both adding new features and refining existing integrations. We're constantly working to fix reported issues, improve compatibility, and reduce the number of false positives.&lt;/p&gt;

&lt;p&gt;Our collaboration with Epic Games plays an important role in this effort. It helps us respond more quickly to changes in the Unreal Engine ecosystem and adapt the analyzer to the real-world needs of developers.&lt;/p&gt;

&lt;p&gt;To learn more about our collaboration, check out the &lt;a href="https://www.unrealengine.com/blog/how-pvs-studio-team-improved-unreal-engines-code" rel="noopener noreferrer"&gt;article on the Epic Games website&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ready to give it a try?
&lt;/h2&gt;

&lt;p&gt;Starting with PVS-Studio 7.43, you can analyze Unreal Engine projects under the Team license.&lt;/p&gt;

&lt;p&gt;If you use Unreal Engine for development and have considered trying PVS-Studio, now's the perfect time to run the analyzer on your project. If you have any questions about integration or configuration, our support team will be happy to help you.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/pvs-studio/try-free/?utm_source=website&amp;amp;utm_medium=devto&amp;amp;utm_campaign=article&amp;amp;utm_content=1394" rel="noopener noreferrer"&gt;You can get a trial version here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Stay creative, and we'll be there to help protect your code from bugs, vulnerabilities, and typos with PVS-Studio.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>tooling</category>
      <category>news</category>
      <category>gamedev</category>
    </item>
    <item>
      <title>One mistake no one has ever made. Right?</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Thu, 16 Jul 2026 14:02:41 +0000</pubDate>
      <link>https://dev.to/pvsdev/one-mistake-no-one-has-ever-made-right-6kh</link>
      <guid>https://dev.to/pvsdev/one-mistake-no-one-has-ever-made-right-6kh</guid>
      <description>&lt;p&gt;As early as your freshman year of college—or even earlier—you might have realized that writing arr[len(arr)] is a bad idea. Surely, no one could get that wrong, right? We thought the same thing, until we checked 1,000 projects.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5xyh3dm05v7mz9klzp6t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F5xyh3dm05v7mz9klzp6t.png" alt="1393_len_error/image1.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  A quick explanation of the error, just in case
&lt;/h2&gt;

&lt;p&gt;Any Go developer knows that the built-in &lt;code&gt;len()&lt;/code&gt; function takes a value and returns its length based on the value's type.&lt;/p&gt;

&lt;p&gt;Let's check the &lt;a href="https://pkg.go.dev/builtin#len" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; and take a closer look at a function signature:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The built-in &lt;code&gt;len&lt;/code&gt; function returns the length of &lt;code&gt;v&lt;/code&gt; based on the type of the passed value:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Array: the number of elements in &lt;code&gt;v&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Pointer to array: the number of elements in &lt;code&gt;*v&lt;/code&gt; (even if &lt;code&gt;v&lt;/code&gt; is &lt;code&gt;nil&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Slice, or map: the number of elements in &lt;code&gt;v&lt;/code&gt;; if &lt;code&gt;v&lt;/code&gt; is &lt;code&gt;nil&lt;/code&gt;, &lt;code&gt;len(v)&lt;/code&gt; is zero.&lt;/li&gt;
&lt;li&gt;String: the number of bytes in &lt;code&gt;v&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Channel: the number of elements queued (unread) in the channel buffer; if &lt;code&gt;v&lt;/code&gt; is &lt;code&gt;nil&lt;/code&gt;, &lt;code&gt;len(v)&lt;/code&gt; is zero.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Look at this simple example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;SimpleExample&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&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;Collection indices always start at 0. So, for a slice of the &lt;code&gt;n&lt;/code&gt; length, the valid indices range from 0 to &lt;code&gt;n – 1&lt;/code&gt;. Then &lt;code&gt;len(arr)&lt;/code&gt; returns &lt;code&gt;n&lt;/code&gt;. As a result, accessing &lt;code&gt;arr[len(arr)]&lt;/code&gt; always leads to a program panic.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's V8031, and how did we discover it?
&lt;/h2&gt;

&lt;p&gt;First, a quick note: in case you didn't know, we're developing PVS-Studio, a static analyzer for C, C++, C#, and Java code. The early access program for the new Go and JS/TS analyzers just ended. We're currently preparing them for the release, which is scheduled for August. Around the same time, the early access program for PVS-Studio Atlas—a team solution for managing code analysis results—will launch. &lt;a href="https://pvs-studio.com/en/subscribe/" rel="noopener noreferrer"&gt;Stay tuned&lt;/a&gt; for updates.&lt;/p&gt;

&lt;p&gt;Now back to V8031. It's a &lt;a href="https://pvs-studio.com/en/docs/warnings/v8031/" rel="noopener noreferrer"&gt;diagnostic rule&lt;/a&gt; for the Go analyzer. We didn't expect it to deliver such good results when we were working on it. We even assumed it might not find anything. We thought, "What if this is a made-up error that can be spotted in any review or test?" From our experience, though, we knew there could be dozens of such errors that are easy to spot but often overlooked due to exhaustion, carelessness, or a lack of effort.&lt;/p&gt;

&lt;p&gt;Implementing the diagnostic rule was easy: it's built entirely on &lt;a href="https://pvs-studio.com/en/blog/terms/0004/" rel="noopener noreferrer"&gt;AST&lt;/a&gt; and uses as little of our own code as possible beyond what Go's built-in analysis tools provide. Feel free to create your own diagnostic rules, by the way. You might want to start with our introductory article, "&lt;a href="https://pvs-studio.com/en/blog/posts/go/1329/" rel="noopener noreferrer"&gt;How to create your own Go static analyzer?&lt;/a&gt;" &lt;/p&gt;

&lt;p&gt;The next step is testing. In our team, it consists of three stages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Unit testing;&lt;/li&gt;
&lt;li&gt;Running a diagnostic rule on our collection of GitHub projects;&lt;/li&gt;
&lt;li&gt;Running a diagnostic rule on the 1,000 most popular Go projects (selected by number of stars).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Right now, we'll focus on the third stage. We were surprised by the results as soon as we got them. Let's take a look at them.&lt;/p&gt;

&lt;h2&gt;
  
  
  The results
&lt;/h2&gt;

&lt;p&gt;In our articles, we usually describe errors and explain possible ways to fix them. We won't do that here since all the code snippets would have the same description as the one at the beginning of the article.&lt;/p&gt;

&lt;h3&gt;
  
  
  Project No. 1
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/bfenetworks/bfe" rel="noopener noreferrer"&gt;bfe&lt;/a&gt; is a modern load balancer used by Baidu.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="n"&gt;BasicLitList&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;BasicLit&lt;/span&gt;
&lt;span class="p"&gt;....&lt;/span&gt;
&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="n"&gt;BasicLitList&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;End&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Pos&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)].&lt;/span&gt;&lt;span class="n"&gt;End&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;PVS-Studio warning: V8031 Using the call of the 'len' function as index will cause off-by-one error. Consider using 'len(b) - 1' instead. &lt;a href="https://github.com/bfenetworks/bfe/blob/d8d6dcb5c49e586f19b433acfee57fb57412ea7a/bfe_basic/condition/parser/ast.go#L138" rel="noopener noreferrer"&gt;ast.go&lt;/a&gt; 138&lt;/p&gt;

&lt;h3&gt;
  
  
  Project No. 2
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/MontFerret/ferret" rel="noopener noreferrer"&gt;Ferret&lt;/a&gt; is a programmable data extraction and automation tool for developers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vec&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Vector&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="n"&gt;core&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;Vector&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;slice&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getCurrentSlice&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="n"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PVS-Studio warning: V8031 Using the call of the 'len' function as index will cause off-by-one error. Consider using 'len(slice) - 1' instead. &lt;a href="https://github.com/MontFerret/ferret/blob/13d1aa56e1aff334b741fc1b5de2841f763d1946/pkg/runtime/expressions/vector.go#L28" rel="noopener noreferrer"&gt;vector.go&lt;/a&gt; 28&lt;/p&gt;

&lt;h3&gt;
  
  
  Project No. 3
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/wader/fq/tree/61d5e42f478cd3800847baaf608ccd6c0277de26" rel="noopener noreferrer"&gt;fq&lt;/a&gt; is a tool, language, and set of decoders for working with binary and text formats.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;blockFns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;uint64&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;D&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dc&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;decodeContext&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
  &lt;span class="nl"&gt;blockTypeInterfaceDescription:&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;D&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dc&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;decodeContext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;typ&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FieldU16&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"link_type"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LinkTypeMap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FieldU16&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"reserved"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FieldU32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"snap_len"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FieldArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"options"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;....)&lt;/span&gt;

  &lt;span class="n"&gt;dc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;interfaceTypes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;interfaceTypes&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;typ&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PVS-Studio warning: V8031 Using the call of the 'len' function as index will cause off-by-one error. Consider using 'len(dc.interfaceTypes) - 1' instead. &lt;a href="https://github.com/wader/fq/blob/61d5e42f478cd3800847baaf608ccd6c0277de26/format/pcap/pcapng.go#L234" rel="noopener noreferrer"&gt;pcapng.go&lt;/a&gt; 234&lt;/p&gt;

&lt;h3&gt;
  
  
  Project No. 4
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/lxc/incus" rel="noopener noreferrer"&gt;Incus&lt;/a&gt; is a modern container management system and virtual machine manager.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;qemuRawCfgOverride&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conf&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="n"&gt;cfg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Section&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;confOverride&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(....)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
  &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;sectionContent&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;comment:&lt;/span&gt; &lt;span class="n"&gt;sec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Comment&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;entries:&lt;/span&gt; &lt;span class="n"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;indexedSection&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;indexedSection&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;
  &lt;span class="n"&gt;orderedSections&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderedSections&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;section&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;name:&lt;/span&gt;    &lt;span class="n"&gt;sec&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="nl"&gt;content:&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PVS-Studio warning: V8031 Using the call of the 'len' function as index will cause off-by-one error. Consider using 'len(indexedSection) - 1' instead. &lt;a href="https://github.com/lxc/incus/blob/46b29cb5f017f14bae9ba33d93def890890c1c1a/internal/server/instance/drivers/driver_qemu_config_override.go#L50" rel="noopener noreferrer"&gt;driver_qemu_config_override.go&lt;/a&gt; 50&lt;/p&gt;

&lt;p&gt;We spotted two such errors in this project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;heartbeatFixture&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;State&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;cluster&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Gateway&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
  &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gateways&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gateways&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gateway&lt;/span&gt;
  &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;states&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;gateway&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;
  &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;servers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;gateway&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gateway&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;V8031 Using the call of the 'len' function as index will cause off-by-one error. Consider using 'len(f.gateways) - 1' instead. &lt;a href="https://github.com/lxc/incus/blob/46b29cb5f017f14bae9ba33d93def890890c1c1a/internal/server/cluster/heartbeat_test.go#L253" rel="noopener noreferrer"&gt;heartbeat_test.go&lt;/a&gt; 253&lt;/p&gt;

&lt;p&gt;We've encountered some errors multiple times in other projects. The reason for this was that some Project 1 included some other Project 2 as a dependency. So, it's fair to say that the errors are piling up.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's summarize
&lt;/h2&gt;

&lt;p&gt;We wanted to share these exciting results with you. Although unlikely, this error may lurk in your own project. Not long ago, we discussed another interesting error in the article &lt;a href="https://pvs-studio.com/en/blog/posts/go/1350/" rel="noopener noreferrer"&gt;Does the operator ^ represent exponentiation or exclusion?&lt;/a&gt;. And to ensure your project's code is clean, you can use &lt;a href="https://pvs-studio.com/en/pvs-studio/try-free/?utm_source=website&amp;amp;utm_medium=devto&amp;amp;utm_campaign=article&amp;amp;utm_content=1393" rel="noopener noreferrer"&gt;PVS-Studio static analyzer&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>History of JavaScript: Browser wars, ECMAScript, Node.js, TypeScript, and React</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Thu, 09 Jul 2026 12:44:06 +0000</pubDate>
      <link>https://dev.to/pvsdev/history-of-javascript-browser-wars-ecmascript-nodejs-typescript-and-react-3fbi</link>
      <guid>https://dev.to/pvsdev/history-of-javascript-browser-wars-ecmascript-nodejs-typescript-and-react-3fbi</guid>
      <description>&lt;p&gt;It only took ten days to develop the language that powers the web. This article tells the story of JavaScript and the tools that helped shape it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ff6zpgxltcntrxdgvl8fh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ff6zpgxltcntrxdgvl8fh.png" alt="1392_JavaScript_history/image1.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  1995. The birth of a legend
&lt;/h2&gt;

&lt;p&gt;The idea for JavaScript was born at Netscape. At the time, web pages consisted almost entirely of HTML, and Netscape wanted to make them more interactive.&lt;/p&gt;

&lt;p&gt;The first step in that direction was licensing Java for use in the Netscape browser. However, Java's complexity proved challenging for web designers.&lt;/p&gt;

&lt;p&gt;Brendan Eich was then tasked with creating a programming language that wasn't too complex and could be embedded directly into HTML pages.  Eventually, Marc Andreessen, co-founder of Netscape Communications, and Bill Joy, co-founder of Sun Microsystems, also contributed to the language development. To meet the deadline for the Netscape browser release, the companies agreed to collaborate on the language.&lt;/p&gt;

&lt;p&gt;During its development, the language changed its name several times. For example, the first version Eich created in just ten days was called Mocha. It was then renamed to LiveScript. The final name was chosen because the word Java was already popular and well-known. &lt;/p&gt;

&lt;p&gt;JavaScript was first announced shortly before the second beta release of Netscape Navigator. Meanwhile, Netscape announced that 28 leading IT companies planned to incorporate JavaScript into their future products.&lt;/p&gt;

&lt;p&gt;JavaScript 1.0 was released in 1996 alongside Netscape Navigator 2.&lt;/p&gt;

&lt;h2&gt;
  
  
  1997-1999. ECMAScript
&lt;/h2&gt;

&lt;p&gt;In 1996, Microsoft also released JScript as part of Internet Explorer 3, which was an open-source implementation of JavaScript for Windows. By the way, the name was changed to avoid negotiating trademark rights for Java with Sun Microsystems.&lt;/p&gt;

&lt;p&gt;To eliminate browser incompatibilities caused by different implementations, Netscape handed the JavaScript specification over to the ECMA international organization.&lt;/p&gt;

&lt;p&gt;So, the ECMA-262 specification was created. The language got the name ECMAScript because JavaScript was already trademarked. Around the same time, the &lt;a href="https://tc39.es/" rel="noopener noreferrer"&gt;TC39&lt;/a&gt; technical committee was formed, bringing together representatives from Netscape, Microsoft, Sun Microsystems, and other companies. It still exists today, continuing to define the evolution of ECMAScript.&lt;/p&gt;

&lt;p&gt;The ECMAScript 2 specification was released in September 1998. Although it didn't introduce any new features, it laid the groundwork for all future changes.&lt;/p&gt;

&lt;p&gt;ECMAScript 3 was released in 1999, introducing several new language capabilities, including regular expressions, exception handling, the &lt;code&gt;switch&lt;/code&gt;-&lt;code&gt;case&lt;/code&gt; statement, and the &lt;code&gt;do&lt;/code&gt;-&lt;code&gt;while&lt;/code&gt; loop variant.&lt;/p&gt;

&lt;h2&gt;
  
  
  2001. The end of the first browser war
&lt;/h2&gt;

&lt;p&gt;The first major rivalry among web browser companies began in 1995.&lt;/p&gt;

&lt;p&gt;At the time, Netscape Navigator was the most well-known browser because it was available for free non-commercial use. However, as part of the Windows 95 Plus! package, Microsoft released Internet Explorer 1.0.&lt;/p&gt;

&lt;p&gt;Just three months later, Internet Explorer 2.0 became available for free download. All Windows users, including commercial organizations, could use the browser for free, which was its key advantage over Netscape Navigator.&lt;/p&gt;

&lt;p&gt;From that point on, every Netscape Navigator and Internet Explorer update was an attempt to outdo the competition. In 1996, Internet Explorer began seriously challenging Netscape's browser when it became the first to offer commercial support for Cascading Style Sheets (CSS).&lt;/p&gt;

&lt;p&gt;The browser wars involved more than just the companies behind the browsers. Web designers at the time actively placed badges on their websites saying things like "Best viewed in Netscape" or "Best viewed in Internet Explorer." These badges usually linked to the corresponding browser download page and meant that the site had only been tested in one of them. In response, supporters of the open web launched the "Viewable with Any Browser" campaign.&lt;/p&gt;

&lt;p&gt;In the end, Microsoft won the battle, securing over 90% of the global market. This was primarily due to the widespread adoption of Windows, which came preinstalled with Internet Explorer as the default browser.&lt;/p&gt;

&lt;p&gt;Back in 1998, when defeat became inevitable, Netscape released the source code of its browser. This decision would later play a role in future browser wars. In 2003, AOL, which owned Netscape by that time, finally disbanded the development team.&lt;/p&gt;

&lt;p&gt;Meanwhile, Microsoft faced a major lawsuit from the U.S. Department of Justice for violating antitrust laws. The court ruled that the company held a monopoly, requiring it to allow computer manufacturers and users to remove Internet Explorer access icons and install different browsers without altering the Windows interface. &lt;/p&gt;

&lt;h3&gt;
  
  
  That same year: the first JSON message
&lt;/h3&gt;

&lt;p&gt;The first message in JSON format was sent in April 2001 from a computer in a garage in the San Francisco Bay Area.&lt;/p&gt;

&lt;p&gt;Douglas Crockford and Chip Morningstar, co-founders of the State Software technology consulting company, were already trying to build AJAX applications even before the term AJAX was first coined.&lt;/p&gt;

&lt;p&gt;The message looked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;domain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="n"&gt;fudco&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;receive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"session"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="nl"&gt;text:&lt;/span&gt; &lt;span class="s"&gt;"Hello world"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only a small part of it resembles the JSON we know today. The message itself is an HTML document containing JavaScript code.&lt;/p&gt;

&lt;p&gt;The format was originally supposed to be called JavaScript Markup Language (JSML), but that acronym was already taken by Java Speech Markup Language, so it was renamed to the JavaScript Object Notation we know today.&lt;/p&gt;

&lt;p&gt;The authors realized they had developed a versatile format, but customers were reluctant to use an unproven technology without specifications.&lt;/p&gt;

&lt;p&gt;This prompted Crockford to purchase the &lt;a href="https://www.json.org/json-en.html" rel="noopener noreferrer"&gt;JSON.org&lt;/a&gt; domain in 2002, where he published the JSON grammar and a parser implementation.&lt;/p&gt;

&lt;h2&gt;
  
  
  2004. Google invents AJAX
&lt;/h2&gt;

&lt;p&gt;On April 1, 2004, Google launched Gmail, marking the first large-scale showcase of AJAX. The service loaded emails, updated the interface, and displayed search suggestions without reloading the page, which was a true breakthrough at the time. Paul Buchheit, project creator and one of Google's first employees, used XMLHttpRequest to make the browser experience feel like a desktop application.&lt;/p&gt;

&lt;p&gt;Interestingly, the term AJAX appeared a little later. In February 2005, Jesse James Garrett introduced it in his article "&lt;a href="https://designftw.mit.edu/lectures/apis/ajax_adaptive_path.pdf" rel="noopener noreferrer"&gt;Ajax: A New Approach to Web Applications&lt;/a&gt;."&lt;/p&gt;

&lt;p&gt;Asynchronous JavaScript and XML (AJAX) is an approach to web application development that enables websites to exchange data with a server in the background without fully reloading the page.&lt;/p&gt;

&lt;p&gt;AJAX marked the beginning of a new era in web applications and frameworks. It also kicked off Web 2.0, which transformed internet users from passive readers into active content creators.&lt;/p&gt;

&lt;h2&gt;
  
  
  2006. jQuery
&lt;/h2&gt;

&lt;p&gt;John Resig created &lt;a href="https://jquery.com/" rel="noopener noreferrer"&gt;jQuery&lt;/a&gt; at BarCamp NYC in January 2006. Its key sources of inspiration included Dean Edwards' CSSQuery library and other community projects from that time.&lt;/p&gt;

&lt;p&gt;The library was designed to streamline common tasks like DOM traversal, event handling, and AJAX.&lt;/p&gt;

&lt;p&gt;jQuery fixed many issues caused by browser incompatibilities, attracting developers with its well-documented and concise API.&lt;/p&gt;

&lt;p&gt;As of August 2022, 77% of the 10 million most popular websites used jQuery. On average, jQuery was used three to four times more often than any other JavaScript library, according to web analytics.&lt;/p&gt;

&lt;h2&gt;
  
  
  2007. Apple reinvents mobile web
&lt;/h2&gt;

&lt;p&gt;When the first iPhones hit the market in 2007, they didn't support Flash, which was the top technology for interactive web content at the time. Flash powered videos, animations, and browser games. Instead of actual content, users saw empty rectangles on websites opened in mobile Safari.&lt;/p&gt;

&lt;p&gt;Critics pointed out the obvious contradiction that without Flash, Apple's promotion of Safari as a "full-featured browser" was an exaggeration. Seizing the opportunity, Android added Flash support in version 2.2, presenting it as a key advantage over the iPhone.&lt;/p&gt;

&lt;p&gt;In April 2010, Steve Jobs published an open letter titled "Thoughts on Flash." He explained why Flash would never appear on Apple devices: a proprietary technology developed in the mouse-driven era was incompatible with touchscreen interfaces. Flash used too much power and wasn't compatible with hardware acceleration on mobile chips. According to Jobs, the future belonged to open-source standards: HTML5, CSS, and JavaScript.&lt;/p&gt;

&lt;p&gt;The market ruled in Apple's favor. The iPhone and iPad quickly gained market share, and web developers had to rewrite websites without Flash while also solving mobile adaptation issues. &lt;/p&gt;

&lt;p&gt;In November 2011, Adobe ceased development of Flash for mobile devices and announced the end of the desktop version in 2017. Flash officially ceased to exist on December 31, 2020.&lt;/p&gt;

&lt;p&gt;JavaScript was the biggest beneficiary of this story. Open-source standards filled the void left by Flash, with JS becoming their main driving force.&lt;/p&gt;

&lt;h2&gt;
  
  
  2008. Google Chrome
&lt;/h2&gt;

&lt;p&gt;For six years, Google CEO Eric Schmidt opposed the development of the company's own web browser. He had several reasons for doing so, but the main one was that he didn't want to drag the company into another browser war.&lt;/p&gt;

&lt;p&gt;However, the company's co-founders, Sergey Brin and Larry Page, hired several Mozilla Firefox developers and created a demo version of Chrome. Schmidt later admitted, "It was so good that it essentially forced me to change my mind."&lt;/p&gt;

&lt;p&gt;On September 2, 2008, Google released the first beta version of Chrome. Thanks to its new V8 JavaScript engine, the browser was built around performance from the start.&lt;/p&gt;

&lt;p&gt;Google's engine was revolutionary. Open source from day one, it influenced the development of other tools, most notably Node.js.&lt;/p&gt;

&lt;p&gt;The release of Google Chrome marked an important milestone in the second browser war.&lt;/p&gt;

&lt;h3&gt;
  
  
  That same year: ECMAScript 4 gets canceled
&lt;/h3&gt;

&lt;p&gt;Development of the new language version began in 2000 and continued through 2008; the creators envisioned it as a complete overhaul of JavaScript. However, it caused a real crisis within TC39.&lt;/p&gt;

&lt;p&gt;The committee split into two groups: supporters, including Adobe, Mozilla, Opera, and Google, and opponents, including Microsoft and Yahoo. The first group wanted large-scale changes, while the second considered ES4 too complex and incompatible with existing code.&lt;/p&gt;

&lt;p&gt;The situation got so bad that Microsoft effectively sabotaged the process by blocking the adoption of proposed changes.&lt;/p&gt;

&lt;p&gt;In the end, ECMAScript 4 was canceled. Instead, a more conservative ECMAScript 5 was released later, while some of the biggest changes returned with the release of ES6. Adobe developers adopted many of those ideas, which became part of ActionScript 3.&lt;/p&gt;

&lt;p&gt;Following this, TC39 introduced a staged process where each feature follows its own path from proposal to inclusion in the standard. This approach paved the way for the annual release of new ECMAScript versions in the future.&lt;/p&gt;

&lt;h2&gt;
  
  
  2009. Node.js
&lt;/h2&gt;

&lt;p&gt;The first version of an open-source, cross-platform JavaScript runtime environment outside of a browser was released in May 2009, approximately 13 years after Netscape introduced the LiveWire Pro Web server environment.&lt;/p&gt;

&lt;p&gt;Ryan Dahl, the creator of Node.js, criticized the Apache HTTP Server for its limited capacity to handle large numbers of simultaneous connections and the sequential programming approach dominant in the industry. &lt;/p&gt;

&lt;p&gt;Node.js overcame these challenges by combining Google's V8 engine, an event loop, and a low-level I/O API.&lt;/p&gt;

&lt;p&gt;Node.js was first presented on November 8, 2009, at JSConf. In early 2010, npm, a package manager for the Node.js environment that allows developers to publish and share packages, was introduced.&lt;/p&gt;

&lt;h3&gt;
  
  
  The same year: ECMAScript 5
&lt;/h3&gt;

&lt;p&gt;Released in 2009, ECMAScript 5 was the first major update to the language since ES3. It introduced many features that have become essential to JavaScript.&lt;/p&gt;

&lt;p&gt;This version added the &lt;code&gt;use strict&lt;/code&gt; directive. It helped identify errors that engines previously ignored. For example, creating a variable without first declaring it started throwing a &lt;code&gt;ReferenceError&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It also added many features for working with strings, arrays, objects, JSON, and dates.&lt;/p&gt;

&lt;p&gt;It's difficult to imagine modern code without features like &lt;code&gt;Array.forEach&lt;/code&gt;, &lt;code&gt;JSON.stringify&lt;/code&gt;, and &lt;code&gt;Date.now&lt;/code&gt;, all of which appeared in this version of the language.&lt;/p&gt;

&lt;h3&gt;
  
  
  That same year: Oracle acquires Sun Microsystems
&lt;/h3&gt;

&lt;p&gt;In April 2009, Oracle announced its acquisition of Sun Microsystems, with the deal closing in January 2010. Along with Sun, Oracle also acquired the JavaScript trademark.&lt;/p&gt;

&lt;p&gt;The acquisition of the trademark by the corporation resulted in several unusual situations. For example, the Rust for JavaScript Developers community received a letter demanding that they stop using the word "JavaScript" in their name. When renewing the trademark in 2019, Oracle submitted a screenshot of the Node.js website to the USPTO as proof of use, as if claiming ownership rights over any JavaScript projects.&lt;/p&gt;

&lt;p&gt;In 2022, this situation led Ryan Dahl, the creator of Node.js and Deno, to publicly ask Oracle to give up the trademark. In 2024, an open letter was released and signed by over 27,000 developers, including Brendan Eich, the creator of JavaScript.&lt;/p&gt;

&lt;p&gt;The Deno team filed a petition with the USPTO to cancel the trademark in November of the same year. The case is still ongoing.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Read our &lt;a href="https://pvs-studio.com/en/blog/posts/java/1256/" rel="noopener noreferrer"&gt;article on the history of Java&lt;/a&gt; to learn more about how Sun Microsystems was acquired by Oracle.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  2012. TypeScript
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.typescriptlang.org/" rel="noopener noreferrer"&gt;TypeScript&lt;/a&gt;, a language that added static typing to JavaScript, was released in October 2012. The project aimed to streamline the development and maintenance of large-scale JavaScript applications. Anders Hejlsberg, the creator of Turbo Pascal and C#, started developing it in 2010 at Microsoft.&lt;/p&gt;

&lt;p&gt;Over the years, TypeScript has influenced the entire ecosystem. &lt;/p&gt;

&lt;p&gt;Many TypeScript features appeared before they became part of the ECMAScript specification, including optional chaining (&lt;code&gt;?.&lt;/code&gt;), nullish coalescing (&lt;code&gt;??&lt;/code&gt;), and others. &lt;/p&gt;

&lt;p&gt;Also, strong typing, once considered unnecessary in the JavaScript world, has become the norm. Nowadays, many libraries, both large and small, ship with &lt;code&gt;.d.ts&lt;/code&gt; declaration files. &lt;/p&gt;

&lt;h2&gt;
  
  
  2013. React
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://react.dev/" rel="noopener noreferrer"&gt;React&lt;/a&gt; is a library for building UIs. It was first introduced at the JSConf US conference in May 2013.&lt;/p&gt;

&lt;p&gt;Development of React began at Facebook in 2010: Jordan Walke created an internal prototype called FaxJS for the news feed. By 2011, React was already in use within the company. The community response was skeptical, with people asking, "How can you mix HTML and JavaScript?"&lt;/p&gt;

&lt;p&gt;However, React changed the way developers build interfaces. The component-based model, Virtual DOM, and the one-way data flow have become the new industry standard.&lt;/p&gt;

&lt;p&gt;Vue.js, released in 2014, took direct inspiration from React's ideas. Starting with version 2, Angular also switched to a component-based architecture. These approaches have now become the standard.&lt;/p&gt;

&lt;p&gt;React Native was released in 2015, enabling developers to build mobile applications using React and paving the way for JavaScript in mobile development. &lt;/p&gt;

&lt;p&gt;The Hooks API, introduced in React in 2019, replaced class components and popularized a functional approach to writing code across the JavaScript ecosystem.&lt;/p&gt;

&lt;h3&gt;
  
  
  The same year: Electron
&lt;/h3&gt;

&lt;p&gt;The framework started as an internal GitHub tool for the Atom text editor, which entered public beta in April 2014. It combines Node.js and Chromium, enabling developers to reuse web code with minimal changes while running it in a controlled browser environment that provides native application privileges.&lt;/p&gt;

&lt;p&gt;The technology's potential was quickly recognized, prompting the renaming of Atom Shell to Electron in April 2015. Slack was among the first major adopters, transforming its web client into a full desktop application. Many other products soon followed.&lt;/p&gt;

&lt;p&gt;Electron revolutionized desktop application development by making it accessible to the web development community. In the past, building cross-platform applications required extensive knowledge of native technologies. Electron allowed developers to create them using only HTML, CSS, and JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  2014. Webpack
&lt;/h2&gt;

&lt;p&gt;In 2012, &lt;a href="https://webpack.js.org/" rel="noopener noreferrer"&gt;Webpack&lt;/a&gt; was released as an open-source JavaScript module bundler. It takes dependencies as input and builds a dependency graph, enabling developers to take a modular approach to web application development. This allowed them to import almost anything to client-side code and, over time, became the foundation of the build process for React, Angular, Vue, and many other frameworks. &lt;/p&gt;

&lt;p&gt;The foundation laid by Webpack later gave rise to modern build tools, including Rollup, Vite, and ESBuild.&lt;/p&gt;

&lt;h2&gt;
  
  
  2015. ECMAScript 6
&lt;/h2&gt;

&lt;p&gt;The new language standard, released in 2015, was the largest update in JavaScript's history. In many ways, JavaScript split into two eras: before and after ES6.&lt;/p&gt;

&lt;p&gt;The new version introduced &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; as replacements for &lt;code&gt;var&lt;/code&gt;, bringing block scope and eliminating many classic hoisting-related bugs. It also brought arrow functions, which made code more concise and solved common issues with the &lt;code&gt;this&lt;/code&gt; context. ES6 also added classes, modules, destructuring, and the &lt;code&gt;spread&lt;/code&gt; and &lt;code&gt;rest&lt;/code&gt; operators.&lt;/p&gt;

&lt;p&gt;ES6 introduced &lt;code&gt;Promise&lt;/code&gt; as JavaScript's native solution to the callback hell problem. ES8 later built on that foundation with &lt;code&gt;async&lt;/code&gt;/&lt;code&gt;await&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Many practices that are now considered standard in modern JavaScript originated with ES6.&lt;/p&gt;

&lt;h3&gt;
  
  
  That same year: the end of the second browser war
&lt;/h3&gt;

&lt;p&gt;The second browser war lasted much longer than the first one, and it was on a completely different scale. While the first one was a showdown between Netscape and Microsoft, the second one was a battle of everyone against everyone else.&lt;/p&gt;

&lt;p&gt;By 2015, Internet Explorer still held around 80% of the market. Microsoft had grown complacent, causing the browser to stop evolving. Users noticed the stagnation and started switching to other options.&lt;/p&gt;

&lt;p&gt;Mozilla Firefox and Opera were the first to capitalize on the situation. Firefox launched in 2004 and immediately offered features Internet Explorer lacked: tabbed browsing, extensions, and better performance. The "ghost" of Netscape had returned for a rematch.&lt;/p&gt;

&lt;p&gt;Microsoft responded too late. After five years of stagnation, Internet Explorer 7 was released in October 2006. Microsoft even allowed users with pirated Windows copies to install the new version, but that didn't stop them from leaving. Internet Explorer continued to lose market share quickly.&lt;/p&gt;

&lt;p&gt;In 2008, Google Chrome entered the race and permanently shifted the balance of power. Its fast V8 engine, minimalist interface, and aggressive promotion through Google's services all contributed to its success. By the early 2010s, Chrome had overtaken every competitor.&lt;/p&gt;

&lt;p&gt;IE lingered on for a few more years until Microsoft released Edge in 2015. Internet Explorer was officially discontinued in 2022.&lt;/p&gt;

&lt;p&gt;Chrome has become the web's most popular browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  2016. Visual Studio Code
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;Visual Studio Code&lt;/a&gt;, a code editor created by Microsoft, was first introduced on April 29, 2015, at the Build conference.&lt;/p&gt;

&lt;p&gt;Microsoft developed it during a period of major change in the web development field, when many developers were switching to lightweight editors like Sublime Text and Atom. At the time, Microsoft was still closely associated with the heavyweight Visual Studio IDE. VS Code was built on Electron and has been open source and cross-platform since day one.&lt;/p&gt;

&lt;p&gt;The community initially reacted with caution. Many doubted Microsoft's ability to build a tool that would appeal to everyone.&lt;/p&gt;

&lt;p&gt;However, VS Code redefined what a code editor could be. Its powerful Extension API and built-in debugger combined with a lightweight user experience proved to be exactly what the market had been missing.&lt;/p&gt;

&lt;p&gt;Language Server Protocol was one of the important byproducts. The VS Code team proposed the standard for language integration, which quickly gained adoption across the industry in editors such as Vim, Emacs, and Eclipse. Stack Overflow's developer survey ranked VS Code as the most popular development tool in 2018, and it has held that position ever since.&lt;/p&gt;

&lt;p&gt;VS Code was especially important for the JavaScript ecosystem because it shipped with built-in support for JavaScript and TypeScript from the beginning.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;By the way, we're developing a Visual Studio Code extension that makes it possible to work with PVS-Studio directly from within the editor. You can learn more about it in the &lt;a href="https://pvs-studio.com/en/docs/manual/6646/" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  2017. WebAssembly
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://webassembly.org/" rel="noopener noreferrer"&gt;WebAssembly&lt;/a&gt; is a binary instruction format for a stack-based virtual machine that became a W3C web standard in 2019. It emerged after years of struggling against JavaScript's performance limits.&lt;/p&gt;

&lt;p&gt;Back in 2013, Mozilla introduced asm.js, a strict subset of JavaScript with explicit type annotations that browsers could compile ahead of time. &lt;a href="https://youtu.be/nB9Pm9Mr7xo?si=nrK9HNaQSPjCrDCC" rel="noopener noreferrer"&gt;A live demonstration of Unreal Engine 4 running directly in Firefox&lt;/a&gt; proved the concept was viable and made a strong impression. However, asm.js was cumbersome, and only one browser supported it.&lt;/p&gt;

&lt;p&gt;In 2015, Mozilla, Google, Microsoft, and Apple came together to develop a solution. They all faced the same challenge: JavaScript engines had reached an architectural limit that compiler optimizations alone couldn't overcome.&lt;/p&gt;

&lt;p&gt;The MVP was released in early 2017 and appeared in all four major browsers almost simultaneously, which was a level of coordinated rollout the web had never seen before. For compute-intensive workloads, WebAssembly achieved 60–80% of native performance.&lt;/p&gt;

&lt;p&gt;Its arrival opened up new possibilities that were previously impossible in web development. For example, Figma rebuilt its rendering engine using C++ and converted it to Wasm, enabling its browser-based editor to perform like a native application. Adobe Photoshop also made its way to the browser, while Unity and Unreal Engine got official Wasm export support.&lt;/p&gt;

&lt;p&gt;Although WebAssembly proved invaluable for workloads where JavaScript struggled, it never fully replaced JavaScript—that was never the goal. Instead, JavaScript became the glue that connects Wasm modules to the DOM and browser APIs.&lt;/p&gt;

&lt;h2&gt;
  
  
  2018. Deno
&lt;/h2&gt;

&lt;p&gt;In 2018, Ryan Dahl, the creator of Node.js, gave a talk at JSConf EU in Berlin titled "&lt;a href="https://youtu.be/M3BM9TB-8yA?si=usVIPTYqLuF0APHN" rel="noopener noreferrer"&gt;10 Things I Regret About Node.js&lt;/a&gt;." He outlined what he considered to be the biggest design flaws of Node.js, including its module system, unrestricted access to the network and file system, and so on.&lt;/p&gt;

&lt;p&gt;He concluded the talk by announcing &lt;a href="https://deno.com/" rel="noopener noreferrer"&gt;Deno&lt;/a&gt; (an anagram of Node—Ed.), a new JavaScript runtime that addresses those issues.&lt;/p&gt;

&lt;p&gt;For example, instead of granting unrestricted access to the file system and network, Deno introduced permission flags such as &lt;code&gt;--allow-write&lt;/code&gt; and &lt;code&gt;--allow-net&lt;/code&gt;. Rather than relying on an ever-growing &lt;code&gt;node_modules&lt;/code&gt; directory and &lt;code&gt;package.json&lt;/code&gt;, it adopted URL-based imports similar to the way external JavaScript files are included in an HTML page.&lt;/p&gt;

&lt;p&gt;The first stable version of Deno was released in 2020. In 2021, the project joined TC39. Although Deno differs from Node.js in many ways, its developers remain committed to the JavaScript standard.&lt;/p&gt;

&lt;p&gt;Deno 2.0, released in 2024, maintained the original's simplicity while adding compatibility with Node.js and npm.&lt;/p&gt;

&lt;h2&gt;
  
  
  2020. Vite
&lt;/h2&gt;

&lt;p&gt;In 2020, Evan You, the creator of Vue.js, made a surprisingly obvious point that had been overlooked by everyone: modern browsers already support native ES modules. This meant developers no longer had to bundle code during development. The browser could resolve the required modules on its own.&lt;/p&gt;

&lt;p&gt;This idea led to the creation of &lt;a href="https://vite.dev/" rel="noopener noreferrer"&gt;Vite&lt;/a&gt; (French for "fast" — Ed.). Unlike traditional tools, Vite's development server didn't waste time bundling the entire project at startup. Instead, it sent source files directly to the browser like ES modules do, while using esbuild, a Go-based bundler, to pre-bundle dependencies from &lt;code&gt;node_modules&lt;/code&gt;. As a result, the time required to initiate these large projects was reduced to mere seconds. When a single file changed, the browser no longer had to reload the entire project; it only needed to reload the modified module.&lt;/p&gt;

&lt;p&gt;The first release focused exclusively on Vue, but in February 2021 Vite 2.0 arrived as a completely redesigned, framework-agnostic tool. &lt;/p&gt;

&lt;p&gt;Vite played a major role in the decline of Create React App, the longstanding standard for building React projects.&lt;/p&gt;

&lt;p&gt;The same ecosystem also gave rise to Vitest, a test runner that uses the same configuration as the build system, allowing developers to avoid duplicating settings for Jest. Vite redefined expectations for development speed in the JavaScript ecosystem, pushing competing tools to match its performance.&lt;/p&gt;

&lt;p&gt;By the mid-2020s, Vite had become one of the world's most downloaded npm packages. In March 2026, Vite 8 was released with Rolldown as its sole bundler, replacing the previous esbuild + Rollup combination. Rolldown, written in Rust, enabled application builds that were dozens of times faster.&lt;/p&gt;

&lt;h2&gt;
  
  
  2022. React on the server
&lt;/h2&gt;

&lt;p&gt;In December 2020, the React team published an RFC titled "&lt;a href="https://legacy.reactjs.org/blog/2020/12/21/data-fetching-with-react-server-components.html" rel="noopener noreferrer"&gt;Zero-Bundle-Size React Server Components&lt;/a&gt;." The idea was simple: what if some components never reached the browser at all? They would run on the server, issue the final output, and then disappear. No client-side re-rendering. No JavaScript added to the bundle.&lt;/p&gt;

&lt;p&gt;The proposal addressed a very real problem: large dependencies ended up in users' browsers simply because components imported them. Also, it required a lot of requests: we first send JavaScript, which then fetches the necessary data.&lt;/p&gt;

&lt;p&gt;For two years, React Server Components remained an experimental feature. They required a dedicated streaming server and worked only within a supporting framework rather than in React itself. Wider adoption began in October 2022 when the beta version of the App Router was introduced with Next.js 13. In May 2023, version 13.4 was declared stable. Server Actions came alongside it. These are functions marked with the &lt;code&gt;use server&lt;/code&gt; directive that are called from the client but executed on the server. &lt;/p&gt;

&lt;p&gt;From that point on, a single React project contained two distinct worlds, each with a different execution model. These worlds were separated by the &lt;code&gt;use client&lt;/code&gt; and &lt;code&gt;use server&lt;/code&gt; directives.&lt;/p&gt;

&lt;p&gt;The impact extended far beyond React. Solid.js, Qwik, and Svelte all embraced the idea of sending less JavaScript to the client. Qwik built its entire resumability model around this concept. Instead of rerunning all the necessary JavaScript to rebuild a page, the browser resumes execution from where the server left off.&lt;/p&gt;

&lt;p&gt;React 19, released in December 2024, officially added Server Actions to the public API. However, most developers still have to choose between the traditional approach and this newer model.&lt;/p&gt;

&lt;h2&gt;
  
  
  2026. PVS-Studio analyzer for JavaScript and TypeScript
&lt;/h2&gt;

&lt;p&gt;On April 6, 2026, &lt;a href="https://pvs-studio.com/en/" rel="noopener noreferrer"&gt;PVS-Studio&lt;/a&gt; launched an &lt;a href="https://pvs-studio.com/en/pvs-studio-eap/" rel="noopener noreferrer"&gt;Early Access Program (EAP)&lt;/a&gt; for its new analyzers. For many years, PVS-Studio has helped developers catch bugs and vulnerabilities in C, C++, C#, and Java projects. Now it's JavaScript's and TypeScript's turn.&lt;/p&gt;

&lt;p&gt;The initial release included 20 diagnostic rules, typo and logic error detection, a CLI version, and a plugin for WebStorm. TypeScript support and a VS Code extension followed in May.&lt;/p&gt;

&lt;p&gt;The EAP also introduced a new analyzer for Go.&lt;/p&gt;

&lt;p&gt;The new analyzers are scheduled for full release in August 2026, alongside PVS-Studio 8.00.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This is where our journey through the history of JavaScript comes to an end. It has been an eventful one, as JavaScript's story is deeply intertwined with the evolution of the web itself.&lt;/p&gt;

&lt;p&gt;This article reflects the author's perspective on the history of JavaScript and may not include every milestone you consider significant. If you think something important is missing, please add it in the comments! :)&lt;/p&gt;

&lt;p&gt;If you're interested in the history of other programming languages, check out our series of articles on the topic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1189/" rel="noopener noreferrer"&gt;History of C and C++. Part one: emergence and standardization of C language.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1224/" rel="noopener noreferrer"&gt;History of C and C++. Part two: standardization of C and C++, Qt, Clang, and Unreal Engine&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/csharp/1248/" rel="noopener noreferrer"&gt;History of C#: versions, .NET, Unity, Blazor, and MAUI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/java/1256/" rel="noopener noreferrer"&gt;History of Java: evolution, legal battles with Microsoft, Mars exploration, Spring, Gradle and Maven, IDEA and Eclipse&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See ya soon!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>javascript</category>
      <category>typescript</category>
      <category>react</category>
    </item>
    <item>
      <title>Let’s make a programming language. Variables</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Wed, 08 Jul 2026 12:44:33 +0000</pubDate>
      <link>https://dev.to/pvsdev/lets-make-a-programming-language-variables-2pl6</link>
      <guid>https://dev.to/pvsdev/lets-make-a-programming-language-variables-2pl6</guid>
      <description>&lt;p&gt;We continue building our own programming language from scratch. In this session, we’ll take the next step by adding variables to the language.&lt;/p&gt;

&lt;p&gt;So far, our programs have consisted of a single expression built from smaller language constructs. Now it’s time to evolve the language.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9ys9m7jjl5te76jqrb02.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9ys9m7jjl5te76jqrb02.png" alt=" " width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Instead of parsing a single expression, we'll introduce a list of declarations, allowing our language to support variable declarations and laying the groundwork for more complex programs.&lt;/p&gt;

&lt;p&gt;During the webinar, Yuri will walk through the implementation step by step, showing how variables are represented and processed in our language, and how to add this functionality to your language in C++.&lt;/p&gt;

&lt;p&gt;This webinar is designed for developers who want to understand how programming languages work under the hood - not just how to use them.&lt;/p&gt;

&lt;p&gt;Register to join the session. All registered participants will receive the recording of this webinar, along with recordings of all previous webinars in the series, after the webinar has concluded.&lt;/p&gt;

&lt;p&gt;If you missed any of the previous sessions, don't worry. All registered participants get access to the recordings. You can also catch up on YouTube or the PVS-Studio website.&lt;/p&gt;

&lt;p&gt;First talk: &lt;strong&gt;&lt;a href="https://pvs-studio.com/en/blog/video/11585?utm_source=devto&amp;amp;utm_medium=pvs&amp;amp;utm_campaign=webinar&amp;amp;utm_content=web5" rel="noopener noreferrer"&gt;Let's make a programming language. Intro&lt;br&gt;
&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Second talk: &lt;strong&gt;&lt;a href="https://pvs-studio.com/en/blog/video/11644?utm_source=devto&amp;amp;utm_medium=pvs&amp;amp;utm_campaign=webinar&amp;amp;utm_content=web5" rel="noopener noreferrer"&gt;Let's make a programming language. Grammars&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Third talk: &lt;strong&gt;&lt;a href="https://pvs-studio.com/en/blog/video/11665?utm_source=devto&amp;amp;utm_medium=pvs&amp;amp;utm_campaign=webinar&amp;amp;utm_content=web5" rel="noopener noreferrer"&gt;Let's make a programming language. Lexer&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Fourth talk: &lt;strong&gt;&lt;a href="https://pvs-studio.com/en/blog/video/11709/?utm_source=devto&amp;amp;utm_medium=pvs&amp;amp;utm_campaign=webinar&amp;amp;utm_content=web6" rel="noopener noreferrer"&gt;Let's make a programming language. Parser&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Fifth talk: &lt;strong&gt;&lt;a href="https://youtu.be/ZY7N6ALq2Bw" rel="noopener noreferrer"&gt;Let’s make a programming language. AST&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Register now to join us live, ask your questions, and get the most out of the session. Don't miss the chance to build your own language alongside an experienced developer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://pvs-studio.com/en/webinar/31/?utm_source=devto&amp;amp;utm_medium=pvs&amp;amp;utm_campaign=webinar&amp;amp;utm_content=web6" rel="noopener noreferrer"&gt;Join us&lt;/a&gt;&lt;/strong&gt; on July 23, 1:00 PM UTC+1!&lt;/p&gt;

&lt;p&gt;P.S. Don't forget to check your inbox and confirm your registration!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>cpp</category>
      <category>webinar</category>
    </item>
    <item>
      <title>Inside BotSharp: Finding bugs in the .NET AI platform's code</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Tue, 07 Jul 2026 14:21:32 +0000</pubDate>
      <link>https://dev.to/pvsdev/inside-botsharp-finding-bugs-in-the-net-ai-platforms-code-13g0</link>
      <guid>https://dev.to/pvsdev/inside-botsharp-finding-bugs-in-the-net-ai-platforms-code-13g0</guid>
      <description>&lt;p&gt;C# has a reputation as a language for enterprise development but not for machine learning. BotSharp pushes back on that idea with a powerful AI platform built on .NET. We'll walk through the project and the bugs we found in its source code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3e36lv5292do79p4osm4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3e36lv5292do79p4osm4.png" alt=" " width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Foreword
&lt;/h2&gt;

&lt;p&gt;Python has long dominated machine learning, while C# made its name in enterprise systems, web development, and game development. That's starting to shift. Tools like ML.NET and Microsoft Agent Framework show that .NET can hold its own in AI development too. BotSharp is one of these tools, and it's what this article is about. Mostly, though, we'll be digging into the bugs we found in its source code using the PVS-Studio static analyzer. Let's get started. &lt;/p&gt;

&lt;h2&gt;
  
  
  A word about the project
&lt;/h2&gt;

&lt;p&gt;BotSharp is an open source framework written in C#. It's built for creating single-agent and multi-agent systems, where one or more AI models drive the decision-making. These models can process and generate multimedia data, and they can act by calling the tools they're given.&lt;/p&gt;

&lt;p&gt;So how does BotSharp differ from &lt;a href="https://learn.microsoft.com/en-us/semantic-kernel/" rel="noopener noreferrer"&gt;Semantic Kernel&lt;/a&gt;? Both currently solve similar problems, offering components for building LLM-powered agent systems. What sets BotSharp apart is a higher level of abstraction and built-in infrastructure for managing agents and dialogs, which makes it faster to deploy typical agent-based solutions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fg9uz9p4wx9yevohk9gz2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fg9uz9p4wx9yevohk9gz2.png" alt=" " width="800" height="423"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Picture 1 — The BotSharp web interface, provided for visualizing agent-based systems&lt;/p&gt;

&lt;p&gt;You can find more on BotSharp in the framework's &lt;a href="https://botsharp.readthedocs.io/en/latest/quick-start/overview.html" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;. For now, though, let's get to the interesting part, the bugs we found in its code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Error review
&lt;/h2&gt;

&lt;p&gt;PVS-Studio found the bugs covered below in the project's version 5.2, the latest one available as of this writing.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;PVS-Studio is a static analysis tool for C#, C, C++, and Java code, built to catch bugs and potential vulnerabilities. Support for Go, JavaScript, and TypeScript is on the way too, and you can &lt;a href="https://pvs-studio.com/en/pvs-studio-eap/" rel="noopener noreferrer"&gt;sign up&lt;/a&gt; now to test those. For more on PVS-Studio, check out the &lt;a href="https://pvs-studio.com/en/pvs-studio/try-free/?utm_source=website&amp;amp;utm_medium=devto&amp;amp;utm_campaign=article&amp;amp;utm_content=1391" rel="noopener noreferrer"&gt;official website&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Unused parameter
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SettingService&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ISettingService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;ILogger&lt;/span&gt; &lt;span class="n"&gt;_logger&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;SettingService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IServiceProvider&lt;/span&gt; &lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="n"&gt;IConfiguration&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="n"&gt;ILogger&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;SettingService&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;_services&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="n"&gt;_config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v3117/" rel="noopener noreferrer"&gt;V3117&lt;/a&gt;. Constructor parameter 'logger' is not used. &lt;a href="https://github.com/SciSharp/BotSharp/blob/b52cbf57f05a71a59d63b7e9f5922952a75b3ba8/src/Infrastructure/BotSharp.Core/Infrastructures/SettingService.cs#L15" rel="noopener noreferrer"&gt;SettingService.cs 15&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just a sloppy typo: the &lt;code&gt;logger&lt;/code&gt; parameter gets passed into the constructor, but nobody assigns it to the matching &lt;code&gt;_logger&lt;/code&gt; field. Nothing breaks so far, simply because that the &lt;code&gt;_logger&lt;/code&gt; field isn't used anywhere yet. But it's still a trap waiting to be triggered.&lt;/p&gt;

&lt;p&gt;Sooner or later someone may want to add logging. They'll spot the field already sitting there, write the usual logging call, and the code will compile without any complaints. Then the first call in prod throws a &lt;code&gt;NullReferenceException&lt;/code&gt; out of nowhere. Landmines like this are exactly the kind of thing you don't want to find later.&lt;/p&gt;

&lt;h3&gt;
  
  
  Missing case in a switch statement
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;UpdateAgentTask&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;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;AgentTaskField&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;metaData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;task&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;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;AgentTaskField&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="n"&gt;metaData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Description&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Description&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;AgentTaskField&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Enabled&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="n"&gt;metaData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Enabled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Enabled&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;AgentTaskField&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Content&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;AgentTaskField&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;All&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="n"&gt;metaData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;task&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;metaData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Description&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Description&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;metaData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Enabled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Enabled&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Content&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v3002/" rel="noopener noreferrer"&gt;V3002&lt;/a&gt;. The switch statement does not cover all values of the 'AgentTaskField' enum: Status. &lt;a href="https://github.com/SciSharp/BotSharp/blob/b52cbf57f05a71a59d63b7e9f5922952a75b3ba8/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentTask.cs#L234" rel="noopener noreferrer"&gt;FileRepository.AgentTask.cs 234&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This &lt;code&gt;switch&lt;/code&gt; statement updates the &lt;code&gt;metaData&lt;/code&gt; properties based on the flag it receives. Nearly every value in the &lt;code&gt;AgentTaskField&lt;/code&gt; enum shows up here, only &lt;code&gt;Status&lt;/code&gt; got left out. &lt;/p&gt;

&lt;p&gt;Because of this gap, the app misbehaves in two different scenarios. If there's an explicit request to update &lt;code&gt;Status&lt;/code&gt;, the &lt;code&gt;switch&lt;/code&gt; just ignores it and &lt;code&gt;metaData&lt;/code&gt; stays unchanged. The  &lt;code&gt;case AgentTaskField.All&lt;/code&gt; block has a problem too: it's supposed to overwrite every &lt;code&gt;metadata&lt;/code&gt; property with &lt;code&gt;task&lt;/code&gt; properties, but since &lt;code&gt;Status&lt;/code&gt; got left out, that block doesn't fully do what it's supposed to.&lt;/p&gt;

&lt;p&gt;It's a good illustration of how extending an &lt;code&gt;enum&lt;/code&gt; without checking every &lt;code&gt;switch&lt;/code&gt; statement that depends on it can quietly introduce hidden bugs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Losing a parameter's value
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;FunctionCallFromLlm&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;GetNextInstruction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;....,&lt;/span&gt; 
  &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;RoleDialogModel&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;dialogs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
  &lt;span class="n"&gt;dialogs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;RoleDialogModel&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;new&lt;/span&gt; &lt;span class="n"&gt;RoleDialogModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AgentRole&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;FunctionName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nameof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HFReasoner&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="n"&gt;MessageId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;messageId&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="n"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetChatCompletions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dialogs&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v3061/" rel="noopener noreferrer"&gt;V3061&lt;/a&gt;. Parameter 'dialogs' is always rewritten in method body before being used. &lt;a href="https://github.com/SciSharp/BotSharp/blob/b52cbf57f05a71a59d63b7e9f5922952a75b3ba8/src/Infrastructure/BotSharp.Core/Routing/Reasoning/HFReasoner.cs#L45" rel="noopener noreferrer"&gt;HFReasoner.cs 45&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;GetNextInstruction&lt;/code&gt; method takes a &lt;code&gt;dialogs&lt;/code&gt; collection as a parameter, but instead of adding to it, the method completely replaces the collection with a new, empty template. &lt;/p&gt;

&lt;p&gt;The parameter most likely stories some conversation context needed to generate a relevant response. Overwriting the variable wipes that context clean right before the request goes to &lt;code&gt;GetChatCompletions&lt;/code&gt;, which can noticeably hurt the quality of the response.&lt;/p&gt;

&lt;p&gt;The fix is straightforward. Add the new data with &lt;code&gt;Add&lt;/code&gt; or, if necessary, leave the &lt;code&gt;dialogs&lt;/code&gt; collection untouched, or build a new collection based on &lt;code&gt;dialogs&lt;/code&gt; with the extra element added in.&lt;/p&gt;

&lt;h3&gt;
  
  
  A copy-pasted condition
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;Make_Payment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Description&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"order number"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;Required&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;order_number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Description&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"total amount"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;Required&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;total_amount&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="n"&gt;order_number&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&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="n"&gt;McpException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Missing required argument 'order_number'"&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="n"&gt;order_number&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&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="n"&gt;McpException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Missing required argument 'total_amount'"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v3021/" rel="noopener noreferrer"&gt;V3021&lt;/a&gt;. There are two 'if' statements with identical conditional expressions. The first 'if' statement contains method return. This means that the second 'if' statement is senseless &lt;a href="https://github.com/SciSharp/BotSharp/blob/b52cbf57f05a71a59d63b7e9f5922952a75b3ba8/tests/BotSharp.Plugin.PizzaBot/McpServerTools/MakePaymentTool.cs#L11" rel="noopener noreferrer"&gt;MakePaymentTool.cs 11&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This one comes down to careless copying. The method is meant to check two required parameters, but since both &lt;code&gt;if&lt;/code&gt; conditions are identical, &lt;code&gt;order_number&lt;/code&gt; gets validated twice, while the second argument never gets checked at all.&lt;/p&gt;

&lt;p&gt;The exception text in the second block gives away the original intent, it was supposed to check &lt;code&gt;total_amount&lt;/code&gt;. Bugs like this slip right past a quick read, but static analysis catches them immediately.&lt;/p&gt;

&lt;h3&gt;
  
  
  A misunderstanding of operator precedence
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;RoleDialogModel&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;OnResponsedDone&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;new&lt;/span&gt; &lt;span class="n"&gt;TokenStatsModel&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;....&lt;/span&gt;
    &lt;span class="n"&gt;TextInputTokens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="n"&gt;inputTokenDetails&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
                      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TextTokens&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="n"&gt;inputTokenDetails&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
                                         &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CachedTokenDetails&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
                                         &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TextTokens&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;....&lt;/span&gt;

    &lt;span class="n"&gt;AudioInputTokens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="n"&gt;inputTokenDetails&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
                       &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AudioTokens&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="n"&gt;inputTokenDetails&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
                                           &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CachedTokenDetails&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
                                           &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AudioTokens&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

    &lt;span class="p"&gt;....&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The PVS-Studio warning:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v3123/" rel="noopener noreferrer"&gt;V3123&lt;/a&gt;. Perhaps the '??' operator works in a different way than it was expected. Its priority is lower than priority of other operators in its left part. &lt;a href="https://github.com/SciSharp/BotSharp/blob/b52cbf57f05a71a59d63b7e9f5922952a75b3ba8/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs#L542" rel="noopener noreferrer"&gt;RealTimeCompletionProvider.cs 542&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v3123/" rel="noopener noreferrer"&gt;V3123&lt;/a&gt;. Perhaps the '??' operator works in a different way than it was expected. Its priority is lower than priority of other operators in its left part. &lt;a href="https://github.com/SciSharp/BotSharp/blob/b52cbf57f05a71a59d63b7e9f5922952a75b3ba8/src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs#L544" rel="noopener noreferrer"&gt;RealTimeCompletionProvider.cs 544&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This code reads as perfectly logical to a human, but the compiler sees it differently.&lt;/p&gt;

&lt;p&gt;The developer attempted to calculate a difference, with each operand guarded against &lt;code&gt;null&lt;/code&gt; by the &lt;code&gt;??&lt;/code&gt; operator. But the subtraction operator &lt;code&gt;-&lt;/code&gt; has a higher precedence than the &lt;code&gt;??&lt;/code&gt; null-coalescing operator, so instead of the intended &lt;code&gt;(A ?? 0) - (B ?? 0)&lt;/code&gt; what actually gets evaluated is &lt;code&gt;A ?? ((0 - B) ?? 0)&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;To get the intended behavior, the expressions around &lt;code&gt;??&lt;/code&gt; need to be explicitly wrapped in parentheses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inputTokenDetails&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TextTokens&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="mi"&gt;0&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;inputTokenDetails&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
                                         &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CachedTokenDetails&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
                                         &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TextTokens&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Potential null dereference
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Issue 1&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;RoleDialogModel&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;GetChatCompletions&lt;/span&gt;&lt;span class="p"&gt;(....)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
  &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;toolcall&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;choices&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FirstOrDefault&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;message&lt;/span&gt;
                         &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tool_calls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FirstOrDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="n"&gt;responseMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;RoleDialogModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;AgentRole&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;JsonSerializer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Serialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toolcall&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v3146/" rel="noopener noreferrer"&gt;V3146&lt;/a&gt;. Possible null dereference of 'toolcall'. The 'FirstOrDefault' can return default null value. &lt;a href="https://github.com/SciSharp/BotSharp/blob/b52cbf57f05a71a59d63b7e9f5922952a75b3ba8/src/Plugins/BotSharp.Plugin.MetaGLM/Providers/ChatCompletionProvider.cs#L62" rel="noopener noreferrer"&gt;ChatCompletionProvider.cs 62&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, the &lt;code&gt;toolcall&lt;/code&gt; variable gets its value from a call chain that uses the &lt;code&gt;FirstOrDefault&lt;/code&gt; method twice. &lt;/p&gt;

&lt;p&gt;The developer took every precaution in that chain, so if either collection turns out empty, &lt;code&gt;toolcall&lt;/code&gt; ends up as &lt;code&gt;null&lt;/code&gt; without throwing anything. &lt;/p&gt;

&lt;p&gt;The problem shows up right after, inside &lt;code&gt;JsonSerializer.Serialize&lt;/code&gt;, where the variable gets dereferenced with no check beforehand. Sooner or later that throws a &lt;code&gt;NullReferenceException&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Issue 2&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;BrowserActionResult&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;InputUserText&lt;/span&gt;&lt;span class="p"&gt;(....)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
  &lt;span class="n"&gt;locator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Locator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;actionParams&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ContextId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;htmlElementContextOut&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;await&lt;/span&gt; &lt;span class="n"&gt;locator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CountAsync&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v3080/" rel="noopener noreferrer"&gt;V3080&lt;/a&gt;. Possible null dereference. Consider inspecting 'locator'. &lt;a href="https://github.com/SciSharp/BotSharp/blob/b52cbf57f05a71a59d63b7e9f5922952a75b3ba8/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.InputUserText.cs#L46" rel="noopener noreferrer"&gt;PlaywrightWebDriver.InputUserText.cs 46&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The analyzer flags another potential &lt;code&gt;null&lt;/code&gt; dereference, this time in &lt;code&gt;locator.CountAsync()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The declaration of the &lt;code&gt;Locator&lt;/code&gt; method explains why:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;ILocator&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;Locator&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;else&lt;/span&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="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsNullOrEmpty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ElementName&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;if&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;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;_logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LogError&lt;/span&gt;&lt;span class="p"&gt;(....);&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;null&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="nf"&gt;if&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;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;_logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LogError&lt;/span&gt;&lt;span class="p"&gt;(....);&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;null&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;else&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="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Index&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="n"&gt;_logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LogError&lt;/span&gt;&lt;span class="p"&gt;(....);&lt;/span&gt;
       &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;
     &lt;span class="p"&gt;....&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;element&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;There are three separate paths where this method returns &lt;code&gt;null&lt;/code&gt;. Yet in the code the analyzer points to, that return value never gets checked.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Issue 3&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;RoutableAgent&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;GetRoutableAgents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;profiles&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;routableAgents&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;profiles&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;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;....&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;profiles&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;profiles&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;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;....&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v3095/" rel="noopener noreferrer"&gt;V3095&lt;/a&gt;. The 'profiles' object was used before it was verified against null. Check lines: 131, 137. &lt;a href="https://github.com/SciSharp/BotSharp/blob/b52cbf57f05a71a59d63b7e9f5922952a75b3ba8/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs#L131" rel="noopener noreferrer"&gt;RoutingService.cs 131&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This one comes down to a validation order problem. The developer planned for &lt;code&gt;profiles&lt;/code&gt; possibly being uninitialized and added a check for it in the &lt;code&gt;else if&lt;/code&gt; block. But that check comes too late, the &lt;code&gt;if&lt;/code&gt; statement above it already calls &lt;code&gt;profiles.Count&lt;/code&gt;. Swapping the two blocks fixes the logic.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope this tour through the BotSharp source code was useful, and hopefully a bit entertaining too. Defects like these, an unclear operator precedence, a forgotten &lt;code&gt;case&lt;/code&gt; in a &lt;code&gt;switch&lt;/code&gt; statement, a lost argument value, blend right into large codebases. Finding them by hand in a massive project is a lot harder than it sounds, and that's exactly where static analysis earns its keep.&lt;/p&gt;

&lt;p&gt;If this article has you wanting to check your own project, you can &lt;a href="https://pvs-studio.com/en/pvs-studio/try-free/?utm_source=website&amp;amp;utm_medium=devto&amp;amp;utm_campaign=article&amp;amp;utm_content=1391" rel="noopener noreferrer"&gt;try the analyzer free&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;See you in the next articles!&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>ai</category>
      <category>programming</category>
    </item>
    <item>
      <title>Game++. Part 1.4: Game engine architectures</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Fri, 03 Jul 2026 14:38:04 +0000</pubDate>
      <link>https://dev.to/pvsdev/game-part-14-game-engine-architectures-43i6</link>
      <guid>https://dev.to/pvsdev/game-part-14-game-engine-architectures-43i6</guid>
      <description>&lt;p&gt;This book offers insights into C++, including algorithms and practices in game development, explores strengths and weaknesses of the language, its established workflows, and hands-on solutions. C++ continues to dominate the game development industry today thanks to its combination of high performance, flexibility, and extensive low-level control capabilities.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjqth864ltofwmq8vs88u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjqth864ltofwmq8vs88u.png" alt=" " width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  CryEngine (Microkernel Architecture)
&lt;/h2&gt;

&lt;p&gt;In the early 2000s, as developers sought ways to create more realistic and immersive game worlds, a unique game engine emerged, offering a completely new approach to architecture. CryEngine, developed by the German studio Crytek, revolutionized the industry with its stunning graphics and cutting-edge microkernel architecture. &lt;/p&gt;

&lt;p&gt;It all started with a tech demo called "X-Isle: Dinosaur Island." Built by a rather small team, the demo turned heads among publishers and developers alike. Back then, most game engines relied on a monolithic architecture with tightly coupled components, but the Yerli brothers (Avni, Faruk, and Cevat Yerli) had a different vision. Inspired by the design of the QNX operating system, they applied the principles of microkernel architecture to a game project. This laid the foundation for an engine that would later become one of the most technologically advanced of its time. &lt;/p&gt;

&lt;p&gt;Investors and studios put their faith in this approach, in which CryEngine isolates only critical functions in the central core, while the remaining components run as separate modules. Such modularity enabled developers to swap out components without destabilizing the whole system. Some implementations took it further, allowing individual logic modules and dynamic libraries to be reloaded at runtime, making it possible to update NPC behavior and fix logic bugs without restarting the game—only truly critical errors would cause a crash. Even then, a fallback handler could swap in the default module, reconnect the failed components, and let the session continue. This architecture also made it practical to create new features, as well as debug and optimize subsystems, without having to rebuild the entire engine. The brothers pushed the idea of supporting multiple genres on a single technology base, but it didn't quite work out, leaving CryEngine with the "best engine for FPS" reputation. &lt;/p&gt;

&lt;p&gt;The first commercial game to demonstrate the power of the microkernel approach was Far Cry (2004), which impressed players with its vast open areas, long draw distances, and realistic vegetation, as well as a world that responded dynamically to the player's actions. The second version of CryEngine, used in Crysis (2007), became the technical benchmark for the new generation of graphics hardware, setting the standard for rendering performance; meanwhile, the phrase "But can it run Crysis?" became a meme among gamers. &lt;/p&gt;

&lt;p&gt;The microkernel approach behind the engine left a mark on the entire game development industry, pushing concepts like scalability, fault tolerance, and cross-platform adaptability into the mainstream. Other engines began adopting similar practices. In fact, after the release of Crysis 2, scalability and adaptability became standard expectations in engine development. &lt;/p&gt;

&lt;p&gt;CryEngine, like all major game engines at the time, was cross-platform from release. The renderer supported OpenGL and DirectX 8/9 and was designed for open environments; a single game level could contain areas spanning up to three square kilometers. The physics system was built entirely in-house, yet that never compromised its quality. It handled rigid bodies, fluids, vehicles, cloth simulation, soft-body objects, and ragdoll physics—in every respect, the technology reached an exceptionally high standard for a proprietary solution.&lt;/p&gt;

&lt;p&gt;Its inverse kinematics technology allowed layering multiple animations onto a single model at once, which made character movement look far more natural. A module written in Lua, rather than C++, handled the NPCs' AI. The scripts could be changed without recompiling, a rare feature at the time. CryEngine also gave us Far Cry and its many expansions, as well as the MMORPG AION. NCSoft licensed CryEngine for AION, although the company later reworked it extensively to fit the demands of multiplayer online games. &lt;/p&gt;

&lt;p&gt;In 2004, following the release of Far Cry, the shader module gained support for Shader Model 3.0, and the engine was updated to 1.2. Shaders unlocked a long list of visual effects: per-pixel lighting, bump-mapped reflections, refraction, smoke, animated textures, transparent computer screens, see-through surfaces, and bullet decals. One particularly notable feature was the fog rendering, which looked far more realistic than what competing engines of that era could pull off. &lt;/p&gt;

&lt;p&gt;In March 2006, Ubisoft acquired full rights to the Far Cry franchise, along with a license to CryEngine. This covered the entire IP, including the trademark, logo, characters, story, and setting, as well as exclusive rights to the source code and the underlying technology—meaning the original team could no longer use any of the old code to develop a new engine. &lt;/p&gt;

&lt;p&gt;After Crytek released the final patches for Far Cry, development of the technology split into two separate paths. Ubisoft, now holding the Far Cry franchise and a licensed copy of the engine's source code, went on to develop its own version for Far Cry Instincts and Far Cry Instincts: Evolution on Xbox, Far Cry Instincts: Predator on Xbox 360, and Far Cry Vengeance for the Nintendo Wii. According to Louis-Pierre Farand, lead producer on Far Cry 2, the resulting Dunia Engine retained only 2–3% of the original CryEngine's source code (Pic. 2.7). &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Femvm1rol5tsro9w5lagc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Femvm1rol5tsro9w5lagc.png" alt=" " width="800" height="586"&gt;&lt;/a&gt;&lt;br&gt;
Picture 2.7&lt;/p&gt;

&lt;p&gt;In turn, Crytek's developers continued developing their own technology platform and created a new engine, CryEngine 2, which marked the next step in its evolution and was fully independent of Ubisoft. Like its predecessor, the engine was written in C++, but unlike the original, it did not aim for cross-platform support. It targeted a single platform: Microsoft Windows with DirectX. At the time of its release, CryEngine 2 ranked among the most technologically advanced and photorealistic game engines on the market, setting new standards for rendering, physics, and dynamic environments. However, its demanding hardware requirements and dependence on Windows and DirectX limited its audience to owners of high-end gaming PCs. &lt;/p&gt;

&lt;p&gt;The engine kept every feature from the previous version and even improved most of them. CryEngine 2 handled massive open environments just as effectively, while a new asset streaming system allowed levels to load dynamically as the player progressed. Crytek was also among the first studios to ship a 64-bit build of a game. Because game worlds had grown considerably since Far Cry, rendering them took far more system resources, making 32-bit systems inadequate. &lt;/p&gt;

&lt;p&gt;The microkernel architecture remained intact as well: the compiled engine was split into separate DLLs, with each component packaged as its own library and loaded on demand during gameplay. This meant developers could swap out or modify individual pieces without touching the rest of the engine.&lt;/p&gt;

&lt;p&gt;CryPhysics, the new in-house physics engine Crytek built for this version, was designed for multithreading from the start and fully integrated into CryEngine 2. It opened the door to far more extensive physics interactions and allowed the engine to apply physics simulation to nearly every object in a level, including trees and vegetation, so they reacted realistically to gravity, wind, explosions, and collisions.&lt;/p&gt;

&lt;p&gt;As with the previous version, the transition to CryEngine 3 came with friction, both within the team and among executives. A completely different team took over development, so the results were a mixed bag: some parts impressed, others fell flat. CryEngine 3 was officially announced on March 11, 2009, and released that same year on October 14. The first game to use the new engine was the first-person shooter Crysis 2.&lt;/p&gt;

&lt;p&gt;The engine was originally developed with DirectX 11 support, but Crysis 2 shipped without it—as well as DirectX 10—until a patch came out. The likely reason was Crytek's push to turn Crysis 2 into a cross-platform release. One major drawback of staying PC-exclusive was piracy: every legitimate copy sold was reportedly matched by about ten pirated downloads. As a result, the studio shifted its focus toward consoles.&lt;/p&gt;

&lt;p&gt;Modern versions of CryEngine continue to build on the ideas behind its microkernel architecture while gradually expanding the use of AI in both tooling and graphics. The philosophy established by the Yerli brothers left a lasting mark on the game industry, proving that modularity, flexibility, and scalability can coexist with high performance.&lt;/p&gt;

&lt;p&gt;A related branch of the technology now lives on as an open-source engine, &lt;a href="https://github.com/o3de/o3de" rel="noopener noreferrer"&gt;O3DE&lt;/a&gt;, which evolved from Amazon Lumberyard. Lumberyard was based on a 2015 version of CryEngine, whose source code is available &lt;a href="https://github.com/CRYTEK/CRYENGINE_ReadMe" rel="noopener noreferrer"&gt;separately&lt;/a&gt;. Amazon licensed the CryEngine source code, refactored and reorganized the project, and released it on GitHub as Lumberyard under a proprietary license. Later, the company handed the project over to the community as O3DE, making it freely available as open source. By the time it became open source, more than $50 million had reportedly been invested in the technology—some estimates place that figure above the development costs of Unity and Unreal Engine, or at least on a comparable level. The original modular architecture now takes the form of independent Gem components that developers can freely add to or remove from a project (Pic. 2.8).&lt;/p&gt;

&lt;p&gt;Today, O3DE remains under active development. Although the engine offers a rich feature set, it has yet to achieve the same level of adoption as Unreal or Unity.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffgu7rjtsg4f3rjj4fo4q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffgu7rjtsg4f3rjj4fo4q.png" alt=" " width="561" height="526"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Picture N2.8&lt;/p&gt;
&lt;h2&gt;
  
  
  Dagor (Data-driven Architecture)
&lt;/h2&gt;

&lt;p&gt;Among many well-known game engines, many of which trace back to the United States, there are a few notable exceptions from Eastern Europe that have shaped the industry in their own way. One such example is Dagor Engine, developed by Gaijin Entertainment. The engine stands out not only for its technical features and its ability to run on virtually any platform with a GPU and at least some kind of CPU, but also for its distinctive architecture built around data-driven design principles. Today, it's publicly available, so anyone can &lt;a href="https://github.com/GaijinEntertainment/DagorEngine" rel="noopener noreferrer"&gt;explore what's under the hood&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The engine's history goes back to the early 2000s, when an in-house game development tool gained wider recognition after the release of IL-2 Sturmovik: Birds of Prey (2009). The game showed that the engine could handle complex physics models, render realistic visual effects, and put data-driven design to real use.&lt;/p&gt;

&lt;p&gt;Data-driven architecture is an approach where application logic comes mainly from data rather than being hard-coded in the source. For game engines, that means most elements of the game world—objects and their behavior included—are described in data files such as JSON, XML, or other structured formats. The engine interprets these files at runtime, which brings a few clear advantages. Data and code stay separate. Objects and their properties are defined declaratively—describing what should happen rather than how to implement it. And configuration becomes dynamic, letting developers make changes without recompiling or even restarting the game. Put together, these traits naturally evolve into a component-based system that's defined and configured entirely through data.&lt;/p&gt;

&lt;p&gt;In traditional engines, changing game object behavior often requires writing or modifying code, recompiling it, and then testing the result. Here, game designers and artists can adjust object parameters, effects, and even core behavior simply by editing data files—effectively making changes on the fly. During an internal tournament for one of the studio's games, the admins spawned random level objects mid-game by simply dropping blk files (Dagor's equivalent of JSON) into the level folder. Cars, weapons, fridges, and a couple of tanks started appearing out of nowhere for the players.&lt;/p&gt;

&lt;p&gt;Data-driven architecture pairs naturally with a component-based approach to designing game objects. Over time, developers start thinking this way by default and can't imagine working otherwise. In Dagor Engine, each object can be built from a set of components, with each one handling a specific functional aspect. This makes it easy to create new object variations just by combining and configuring existing components. Once again, all of this happens on the fly, without recompiling the engine and often without even restarting the game.&lt;/p&gt;

&lt;p&gt;Contrary to the common belief that runtime data interpretation slows games down, a well-designed data-driven architecture can still deliver high performance. The evidence is indirect but visible: the company's games run at acceptable frame rates on mobile devices and the Nintendo Switch, neither of which is exactly known for raw power.&lt;/p&gt;

&lt;p&gt;The resource system is a cornerstone of the data-driven architecture. The engine organizes all game data into a hierarchical structure of configs, each with a unique identifier and loadable on demand. In Dagor Engine, the code might look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;vehicles&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;tanks&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;34&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;blk&lt;/span&gt; &lt;span class="n"&gt;textures&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="n"&gt;diffuse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dds&lt;/span&gt; &lt;span class="n"&gt;normal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dds&lt;/span&gt; &lt;span class="n"&gt;specular&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dds&lt;/span&gt;
&lt;span class="n"&gt;weapons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;blk&lt;/span&gt; &lt;span class="n"&gt;collision&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;blk&lt;/span&gt;


&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;weapons&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;guns&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;85&lt;/span&gt;&lt;span class="n"&gt;mm_zis&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;ballistics&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;blk&lt;/span&gt; &lt;span class="n"&gt;Visual_effects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;blk&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The *.blk files are a special structured data format used in the engine. While similar to JSON in function, they are optimized for fast loading, efficient processing, and overriding of sections and properties. When loading, the game can pull in values to override object properties from another config. It typically looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;visual_effects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;blk&lt;/span&gt; &lt;span class="n"&gt;graphics&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;rendinstDistMul:&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt; &lt;span class="n"&gt;grassRadiusMul&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;visual_effects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="mf"&gt;1.&lt;/span&gt;&lt;span class="n"&gt;blk&lt;/span&gt; &lt;span class="n"&gt;graphics&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;override&lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;rendinstDistMul&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.8&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The final configuration ends up with &lt;code&gt;rendinstDistMul = 0.8&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Despite all its advantages, data-driven architecture also comes with serious drawbacks. The first is debugging complexity. Since behavior comes from data rather than code, debugging takes a lot more effort and usually needs specialized tools to track how data changes ripple through the system. In real projects, this often means maintaining separate visual debugging tools tailored to a specific engine.&lt;/p&gt;

&lt;p&gt;The second drawback is interpretation overhead. However efficiently the engine optimizes data processing, runtime interpretation still introduces additional costs compared to hard-coded logic.&lt;/p&gt;

&lt;p&gt;Pic. 2.9 shows the systems developers can configure just by modifying scripts, without rebuilding the entire engine—and, as you can see, that covers almost the entire engine.&lt;/p&gt;

&lt;h2&gt;
  
  
  X-Ray Engine (Monolith Architecture)
&lt;/h2&gt;

&lt;p&gt;The history of the video game industry is full of technologies that arrived ahead of their time. One such example is the X-Ray Engine, created by programmers Alexander Maksimchuk and Oles Shishkovtsov for the S.T.A.L.K.E.R. series. The game was originally going to be about robots on some unknown planet filled with anomalies and lasers. At some point the team realized they did not need to invent either the planet or the robots—the perfect anomalous zone with the perfect atmosphere already existed just a three-hour drive from their office. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3uizwyrhffnin1tzrpmh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3uizwyrhffnin1tzrpmh.png" alt=" " width="800" height="977"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Picture N2.9&lt;/p&gt;

&lt;p&gt;First shown back in 2001, the engine went on to become the foundation for one of the most immersive gaming worlds ever created. Its graphics held up impressively well for the time. The engine could render up to four million polygons on screen, far beyond what even many late-2000s games could handle. It managed large-scale environments just as well, whether enclosed interiors or open areas reaching up to two square kilometers. On top of that, it ran a full dynamic day-night cycle with lighting changes, along with weather effects like rain, wind, and fog. The dynamic lighting in particular still impresses, producing memorable shots and an atmosphere that's hard to forget even now.&lt;/p&gt;

&lt;p&gt;For physics, X-Ray relied on the open-source Open Dynamics Engine (ODE), also released in 2001. It handled rigid-body dynamics and collision detection, and worked well for simulating vehicles, creatures, and objects in a dynamic environment. On paper, the engine's integration system was stable enough that physics shouldn't have broken down without reason. In reality, players of the first S.T.A.L.K.E.R. game soon got very familiar with all kinds of physics anomalies—from flying bodies to bizarre object behavior. Those quirks eventually turned into one of the series' signature traits, inspiring memes and funny clips. Well, it simply fit the nature of the Zone—after all, anomalies are part of the setting.&lt;/p&gt;

&lt;p&gt;This engine is a textbook example of monolithic architecture (not to be confused with a unitary one). It is divided into separate parts, but those parts are tightly bound together rather than loosely coupled. Unlike systems where components operate relatively independently, X-Ray Engine functions as a tightly integrated whole, with all components deeply interconnected.&lt;/p&gt;

&lt;p&gt;Monolithic architecture (Pic. 2.10) brings both clear advantages and serious drawbacks. Tight integration across graphics, physics, and AI—and the ability to build gameplay mechanics directly off how components interact—enabled developers to use and plan resources more efficiently through custom allocators, object packing, fast message queues, and similar techniques. With no abstraction layers standing between components, interaction stayed direct and fast, which mattered a lot on the hardware available at the time. Developers could precisely control memory usage and CPU time, which proved critical for a project this demanding.&lt;/p&gt;

&lt;p&gt;This unified structure also meant the engine, the game world, development tools, and subsystems could share one coherent vision, with everything working together naturally. Removing or isolating any individual part would inevitably reduce functionality and hurt overall performance.&lt;/p&gt;

&lt;p&gt;However, the same characteristics that boost performance can also limit scalability, make individual components harder to update, and introduce various challenges, especially on multicore systems. As the system grows, maintaining it and fixing bugs becomes exponentially more difficult, since changes in one part often ripple through neighboring subsystems.&lt;/p&gt;

&lt;p&gt;In fact, complexity ended up being the biggest technical issue of the X-Ray Engine. Beyond the random crashes familiar to every fan—hello, "bug trap"—the engine also suffered from micro-freezes and brief stutters that showed up across every game in the S.T.A.L.K.E.R series. They were especially bad in Shadow of Chernobyl, where, in extreme cases, the game could turn into a slideshow while running at a normal frame rate. The root cause was architectural: the engine relied heavily on a single CPU core, a limitation that only became more painful as multicore systems took over.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2jnm5fqv6zg6c20ik5k9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2jnm5fqv6zg6c20ik5k9.png" alt=" " width="709" height="872"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Picture 2.10&lt;/p&gt;

&lt;p&gt;The last official version of the engine is X-Ray Engine 1.6.02, used in Call of Pripyat. The fan community didn't stop there, though. Enthusiasts kept refining the technology, creating unofficial engine versions that fixed many critical issues, introduced new features, and—most importantly—added support for multicore processors and multithreading.&lt;/p&gt;

&lt;p&gt;Technical problems aside, the engine remains an important milestone in the history of game development. Its strengths—dynamic lighting, advanced physics, and the A-Life simulation system—were years ahead of their time and gave players plenty of standout moments. And perhaps this monolith really did make someone's dream game a reality.&lt;/p&gt;

&lt;p&gt;Monolithic architecture—with all its strengths and weaknesses, and above all when applied deliberately—serves as an excellent example of a particular approach to building game engines. It also offers a valuable lesson for the industry as a whole. The engine's core concepts and many of the practices developed around it would later inform the 4A Engine, built by former members of the team for the Metro series.&lt;/p&gt;

&lt;h2&gt;
  
  
  Godot (Modules/Services Architecture)
&lt;/h2&gt;

&lt;p&gt;From the outside, the game development industry may seem to advance at a rapid pace: new rendering technologies, new approaches to rigging, better-looking models, voice acting, and the growing use of AI for animation. Under the hood, however, all these things still rely on well-tested, established solutions that are extremely difficult to replace or change in any fundamental way. I would even argue that, outside conference talks, game development remains highly conservative. Big studios avoid risks, while smaller teams often lack the time and resources to take them, even when they'd like to. Real architectural innovation is rare. Against this backdrop, the adoption of concepts from the web—as seen with the Godot engine and microservices architecture—becomes particularly interesting.&lt;/p&gt;

&lt;p&gt;Most architectural styles receive their names once communities or conference speakers notice a recurring pattern and start shaping it into something more defined—there is no secret council of architects deciding what the next major trend will be. More often, developers independently arrive at similar solutions as an engine's ecosystem evolves and new challenges emerge. The approaches that prove most effective at adapting to change and leveraging it eventually become recognized architectural patterns that others follow. Microservices stand out here, because when the concept first emerged, no game engine or game actually followed those principles. In fact, the term gained recognition through the article "Microservices" by Martin Fowler and James Lewis, published in 2014. In that article, the authors laid out the common traits of this relatively new architectural style. Looking at Godot, I see many of those same ideas at work. At its core, the microservices approach breaks a complex system into numerous small, autonomous services, each providing a specific set of features.&lt;/p&gt;

&lt;p&gt;In the context of game development with Godot, this idea evolves into a microservice-like architecture where the game no longer exists as a monolithic mass of code but as a system of components that talk to each other. Physics, AI, UI, and the audio system—each of these elements can exist as a separate module with a clearly defined way of interacting with the rest. I would not call this approach perfect, but it does allow developers to focus on individual aspects of the game without constantly worrying about how a change might affect the rest of the project.&lt;/p&gt;

&lt;p&gt;Godot first appeared in 2014 and has since won over many developers thanks to its simplicity and reliability. Its name comes from the character who never arrives in Samuel Beckett's play Waiting for Godot. What sets Godot apart is how it organizes game logic through a system of nodes and scenes. Imagine a construction set where every piece of the game—from a character to an interface—is a separate building block that snaps easily to others. Although Godot wasn't originally designed with microservices in mind, its node-based architecture and modularity ended up fitting that philosophy surprisingly well. The result was an architecture where different components can be developed almost entirely independently of each other.&lt;/p&gt;

&lt;p&gt;Godot's signal mechanism provides a particularly elegant way to connect these components. Imagine a player picking up an item: the inventory system emits a signal, and other systems can react to it. The UI updates, the achievement system logs progress toward whatever is being collected, and a special abilities system recalculates character stats. None of these systems needs to know how the others work—or, in some cases, even that they exist at all.&lt;/p&gt;

&lt;p&gt;In fact, the engine's core philosophy revolves around adding new features by creating new modules rather than modifying existing ones. Developers can reuse component modules across different projects, and if a module has a bug, it is less likely to affect the entire game. Of course, no game can be broken down into modules indefinitely, but this approach at least cuts the chances of the entire thing crashing due to a single faulty component.&lt;/p&gt;

&lt;p&gt;That said, this architecture is no silver bullet—though for smaller projects it often works exceptionally well: you snap together a set of component modules, wire up their signals, and—voilà—everything comes together and works as intended. As a project grows, though, the sheer number of interactions and gameplay systems tends to introduce excessive complexity and slow things down. The signal system can become a bottleneck, while modules may start competing for updates. The real takeaway is that micro-modules only make sense where they provide a clear benefit. For example, separating game logic from visual presentation almost always pays off. On the other hand, splitting tightly coupled mechanics into independent services or modules often leads to unnecessary complexity later on.&lt;/p&gt;

&lt;p&gt;Another important distinction is that, unlike traditional microservices in web development—which run as separate processes or even on different machines—all game components operate within the same process (or a small number of processes). This sidesteps a lot of communication and synchronization headaches typical of classical microservice systems, the kind of problems that would only get in the way at such a small scale.&lt;/p&gt;

&lt;p&gt;Even so, this approach has gained a dedicated following: more than a dozen relatively large games shipped on Godot in 2024 alone. That may not sound impressive compared to the thousands released on Unreal Engine or Unity, but it is a significant achievement for a project maintained primarily by a handful of core developers and its community.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Author: Sergei Kushnirenko&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sergei has over 20-year experience in coding and game development. He graduated from ITMO National Research University and began his career developing software for naval simulators, navigation systems, and network solutions. For the past fifteen years, Sergei has specialized in game development: at Electronic Arts, he worked on optimizing The Sims and SimCity BuildIt, and at Gaijin Entertainment, Sergei headed up the porting of games to the Nintendo Switch and Apple TV platforms. Sergei actively participates in open-source projects, including the ImSpinner library and the Pharaoh (1999) game restoration project.&lt;/p&gt;

&lt;h2&gt;
  
  
  All parts
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/1361/" rel="noopener noreferrer"&gt;Game++. Part 1.1: Game engine architectures&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/1375/" rel="noopener noreferrer"&gt;Game++. Part 1.2: Game engine architectures&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/1384/" rel="noopener noreferrer"&gt;Game++. Part 1.3: Game engine architectures&lt;/a&gt;&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>gamedev</category>
      <category>programming</category>
      <category>software</category>
    </item>
    <item>
      <title>Code slicing: What lies inside OrcaSlicer</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Tue, 30 Jun 2026 14:19:06 +0000</pubDate>
      <link>https://dev.to/pvsdev/code-slicing-what-lies-inside-orcaslicer-19pn</link>
      <guid>https://dev.to/pvsdev/code-slicing-what-lies-inside-orcaslicer-19pn</guid>
      <description>&lt;p&gt;How can you avoid overlooking dangerous code parts during reviews? You can use static analysis tools. Let's take as an example OrcaSlicer, a popular slicing software designed to prepare 3D models for printing. We'll take a peek under its hood and see what surprises await us.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ft8s6gvqcynm8w5ybi9e8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ft8s6gvqcynm8w5ybi9e8.png" alt="1389_OrcaSlicer/image1.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  About the project
&lt;/h2&gt;

&lt;p&gt;There's a cool project on GitHub that helps prepare 3D models for printing. It can convert a 3D model into machine instructions for creating an object layer by layer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/OrcaSlicer/OrcaSlicer" rel="noopener noreferrer"&gt;OrcaSlicer&lt;/a&gt; is one of the most popular free open-source slicers. The project is quite large, and its primary development language is C++, which is exactly why it caught my attention. Today, let's take a look under the hood and explore some of the code's highlights.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;. The purpose of this content is to promote static analysis technology, not to offend the authors of OrcaSlicer.&lt;/p&gt;

&lt;p&gt;The article simply shows that even functioning projects can contain bugs. Developers may overlook issues during code reviews or testing, and they accumulate over the years, which can ultimately lead to defects in the product. However, using additional tools, such as static analyzers, helps developers detect bugs before they reach production.&lt;/p&gt;

&lt;p&gt;If you still doubt the value of such tools, I recommend reading the article "&lt;a href="https://pvs-studio.com/en/blog/posts/0687/" rel="noopener noreferrer"&gt;Why you should choose the PVS-Studio static analyzer to integrate into your development process&lt;/a&gt;."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Let's dive in
&lt;/h2&gt;

&lt;p&gt;I analyzed this project &lt;a href="https://github.com/OrcaSlicer/OrcaSlicer/commit/a167702038496b97e8495cc79f0c62660063ac87" rel="noopener noreferrer"&gt;commit&lt;/a&gt; using &lt;a href="https://pvs-studio.com/en/" rel="noopener noreferrer"&gt;PVS-Studio&lt;/a&gt; plugin for Visual Studio 2022. The article doesn't cover every warning the analyzer issued, only the ones I found the most interesting.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note.&lt;/strong&gt; To improve readability, the code snippets have been formatted, and some have been hidden behind comments marked with &lt;code&gt;// ....&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Example N1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v655/" rel="noopener noreferrer"&gt;V655&lt;/a&gt; The strings were concatenated but are not utilized. Consider inspecting the 'message + "\n\nApplication will close."' expression. &lt;a href="https://github.com/OrcaSlicer/OrcaSlicer/blob/a167702038496b97e8495cc79f0c62660063ac87/src/slic3r/GUI/GUI_App.cpp#L7170" rel="noopener noreferrer"&gt;GUI_App.cpp 7170&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;GUI_App&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;load_language&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wxString&lt;/span&gt; &lt;span class="n"&gt;language&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;initial&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&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="n"&gt;wxLocale&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;IsAvailable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;locale_language_info&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;Language&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Loading the language dictionary failed.&lt;/span&gt;
    &lt;span class="n"&gt;wxString&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;   &lt;span class="s"&gt;"Switching Orca Slicer to language "&lt;/span&gt;
                       &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;requested_language_code&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" failed."&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="cp"&gt;#if !defined(_WIN32) &amp;amp;&amp;amp; !defined(__APPLE__)
&lt;/span&gt;      &lt;span class="c1"&gt;// likely some linux system&lt;/span&gt;
      &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;You may need to reconfigure the missing locales, "&lt;/span&gt;
                 &lt;span class="s"&gt;" likely by running the &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s"&gt;locale-gen&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s"&gt; "&lt;/span&gt;
                 &lt;span class="s"&gt;"and &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s"&gt;dpkg-reconfigure locales&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s"&gt; commands.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="cp"&gt;#endif
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;initial&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="s"&gt;Application will close."&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;              &lt;span class="c1"&gt;// &amp;lt;=&lt;/span&gt;
    &lt;span class="n"&gt;wxMessageBox&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Orca Slicer - Switching language failed"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                 &lt;span class="n"&gt;wxOK&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;wxICON_ERROR&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="n"&gt;initial&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;EXIT_FAILURE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's a typo that developers missed during code review. The code uses &lt;code&gt;+&lt;/code&gt; instead of &lt;code&gt;+=&lt;/code&gt;, so the string literal never gets appended to the &lt;code&gt;message&lt;/code&gt; variable. The code compiles, but it makes no sense. This warning reminded me of an &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1119/#ID717E26CA54" rel="noopener noreferrer"&gt;error we caught in qbEngine&lt;/a&gt;, where devs forgot to add the &lt;code&gt;#&lt;/code&gt; character in the &lt;code&gt;#else&lt;/code&gt; macro.&lt;/p&gt;

&lt;p&gt;When looking at this code, one might think: "No way! I'd never write something like this." It's simply a mistake a human can do, though. This code has been in the project for four years, yet it still hasn't been fixed. At the same time, the fragment where the &lt;code&gt;message&lt;/code&gt; string is initialized was &lt;a href="https://github.com/OrcaSlicer/OrcaSlicer/commit/8168d0a4e088748dde3d843fd1aa77eed245f528#diff-8432157ace185a6e4c63a53f838d80cd1bbae25fdd3829b94a54fc5ecf5b6dabL6443-R6466" rel="noopener noreferrer"&gt;modified&lt;/a&gt; just a couple of months ago.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example N2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v1047/" rel="noopener noreferrer"&gt;V1047&lt;/a&gt; Lifetime of the lambda is greater than lifetime of the local variable 'do_stop' captured by reference. &lt;a href="https://github.com/OrcaSlicer/OrcaSlicer/blob/a167702038496b97e8495cc79f0c62660063ac87/src/slic3r/GUI/Jobs/FillBedJob.cpp#L250" rel="noopener noreferrer"&gt;FillBedJob.cpp 250&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;FillBedJob&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Ctl&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;ctl&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
  &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;do_stop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
  &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;on_packed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;do_stop&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;ArrangePolygon&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;ap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;do_stop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bed_idx&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;ap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;priority&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The lambda expression captures the &lt;code&gt;do_stop&lt;/code&gt; local variable by reference and then stores it in &lt;code&gt;params.on_packed&lt;/code&gt;. However, &lt;code&gt;do_stop&lt;/code&gt; is destroyed when the &lt;code&gt;process&lt;/code&gt; method exits because the lifetime of the local object ends. If the lambda runs after the member function returns, it will access a destroyed object, resulting in undefined behavior.&lt;/p&gt;

&lt;p&gt;Capturing by value would solve this issue, but the lambda modifies the &lt;code&gt;do_stop&lt;/code&gt; variable, so this approach won't work. Instead, &lt;code&gt;do_stop&lt;/code&gt; can be turned into a member of the &lt;code&gt;FillBedJob&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;This pattern repeats two more times in the code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;V1047 Lifetime of the lambda is greater than lifetime of the local variable 'statustxt' captured by reference. &lt;a href="https://github.com/OrcaSlicer/OrcaSlicer/blob/a167702038496b97e8495cc79f0c62660063ac87/src/slic3r/GUI/Jobs/FillBedJob.cpp#L245" rel="noopener noreferrer"&gt;FillBedJob.cpp 245&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;V1047 Lifetime of the lambda is greater than lifetime of the local variable 'do_stop' captured by reference. &lt;a href="https://github.com/OrcaSlicer/OrcaSlicer/blob/a167702038496b97e8495cc79f0c62660063ac87/src/slic3r/GUI/Jobs/FillBedJob.cpp#L241" rel="noopener noreferrer"&gt;FillBedJob.cpp 241&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example N3&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v768/" rel="noopener noreferrer"&gt;V768&lt;/a&gt; The enumeration constant 'roPortrait' is used as a variable of a Boolean-type. &lt;a href="https://github.com/OrcaSlicer/OrcaSlicer/blob/a167702038496b97e8495cc79f0c62660063ac87/src/libslic3r/SLA/RasterBase.hpp#L81" rel="noopener noreferrer"&gt;RasterBase.hpp 81&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RasterBase&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;

  &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;Orientation&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;roLandscape&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;roPortrait&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Trafo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;mirror_x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mirror_y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;flipXY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;TMirroring&lt;/span&gt; &lt;span class="n"&gt;get_mirror&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;roPortrait&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;mirror_x&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;mirror_x&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;mirror_y&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;  &lt;span class="c1"&gt;// &amp;lt;=&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="c1"&gt;// ....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The analyzer detected an enum value being used as the first operand in a ternary operator. The &lt;code&gt;roPortrait&lt;/code&gt; constant has a value of &lt;code&gt;1&lt;/code&gt;, so the condition is always true. Most likely, the developers should've used another variable here.&lt;/p&gt;

&lt;p&gt;Alternatively, an additional parameter may have been needed to compare it with &lt;code&gt;roPortrait&lt;/code&gt;, as done in the constructor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;  &lt;span class="n"&gt;Trafo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Orientation&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;roLandscape&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;TMirroring&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;mirror&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;NoMirror&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;// XY flipping implicitly does an X mirror&lt;/span&gt;
    &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;mirror_x&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;roPortrait&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;mirror&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="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;mirror&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="c1"&gt;// &amp;lt;=&lt;/span&gt;
    &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mirror_y&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;mirror&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="c1"&gt;// Makes raster origin to be top left corner&lt;/span&gt;
    &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;flipXY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;roPortrait&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;&lt;strong&gt;Example N4&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v781/" rel="noopener noreferrer"&gt;V781&lt;/a&gt; The value of the 'm_load_slot_index' variable is checked after it was used. Perhaps there is a mistake in program logic. Check lines: 2152, 2155. &lt;a href="https://github.com/OrcaSlicer/OrcaSlicer/blob/a167702038496b97e8495cc79f0c62660063ac87/src/slic3r/GUI/Widgets/AMSItem.cpp#L2152" rel="noopener noreferrer"&gt;AMSItem.cpp 2152&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;AMSRoadUpPart&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;doRender&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wxDC&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;dc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
  &lt;span class="n"&gt;dc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SetPen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wxPen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_get_diff_clr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;m_amsinfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cans&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;m_load_slot_index&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;material_colour&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wxPENSTYLE_SOLID&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;m_amsinfo&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="n"&gt;m_load_step&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;AMSPassRoadSTEP&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;AMS_ROAD_STEP_NONE&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="n"&gt;m_amsinfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cans&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;m_load_slot_index&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;m_load_slot_index&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="n"&gt;BOOST_LOG_TRIVIAL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trace&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="s"&gt;"up road render error"&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="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These errors are especially intriguing because they seem impossible to make at first glance. The case may seem simple, but it's actually quite confusing. If we look through the &lt;a href="https://pvs-studio.com/en/blog/examples/v781/" rel="noopener noreferrer"&gt;list of issues&lt;/a&gt; the V781 diagnostic rule detected, we can see that even developers working on fairly large projects make these mistakes quite often.&lt;/p&gt;

&lt;p&gt;What's the problem, though? It's array index out of bounds. First, the &lt;code&gt;m_load_slot_index&lt;/code&gt; index is used to access an element of the &lt;code&gt;m_amsinfo.cans&lt;/code&gt; vector, only then it's checked whether the iterator is out of bounds or negative. The check exists, but it happens too late. We can fix the code by placing the check earlier or handling this case differently.&lt;/p&gt;

&lt;p&gt;The same warning:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;V781 The value of the 'i' index is checked after it was used. Perhaps there is a mistake in program logic. ViewerImpl.cpp 56&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example N5&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v783/" rel="noopener noreferrer"&gt;V783&lt;/a&gt; Iterator 'it' is checked after it was used. Perhaps there is a mistake in program logic. Check lines: 3159, 3160. &lt;a href="https://github.com/OrcaSlicer/OrcaSlicer/blob/a167702038496b97e8495cc79f0c62660063ac87/src/slic3r/GUI/Selection.cpp#L3160" rel="noopener noreferrer"&gt;Selection.cpp 3160&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Selection&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ensure_not_below_bed&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;is_any_volume&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="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;i&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m_list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;GLVolume&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;volume&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;m_volumes&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
      &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;pair&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_pair&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;volume&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;object_idx&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;volume&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;instance_idx&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
      &lt;span class="n"&gt;InstancesToZMap&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;const_iterator&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;instances_max_z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;z_shift&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SINKING_MIN_Z_THRESHOLD&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;second&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="n"&gt;it&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;instances_max_z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;z_shift&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;volume&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_volume_offset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Z&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;volume&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_volume_offset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Z&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;z_shift&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="c1"&gt;// ....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This fragment contains another check that also appears after the iterator is used. The&lt;code&gt;it&lt;/code&gt; iterator is dereferenced to calculate &lt;code&gt;z_shift&lt;/code&gt;, and the next line contains a check whether it differs from the &lt;code&gt;end()&lt;/code&gt; iterator. If the element isn't found, dereferencing the resulting iterator leads to undefined behavior.&lt;/p&gt;

&lt;p&gt;The fixed code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;instances_max_z&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;z_shift&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SINKING_MIN_Z_THRESHOLD&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;second&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="n"&gt;z_shift&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;volume&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_volume_offset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Z&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;volume&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_volume_offset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Z&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;z_shift&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;&lt;strong&gt;Example N6&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio warnings:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v595/" rel="noopener noreferrer"&gt;V595&lt;/a&gt; The 'part_plate' pointer was utilized before it was verified against nullptr. Check lines: 498, 525. &lt;a href="https://github.com/OrcaSlicer/OrcaSlicer/blob/a167702038496b97e8495cc79f0c62660063ac87/src/slic3r/GUI/ObjectDataViewModel.cpp#L498" rel="noopener noreferrer"&gt;ObjectDataViewModel.cpp 498&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v522/" rel="noopener noreferrer"&gt;V522&lt;/a&gt; Dereferencing of the null pointer 'part_plate' might take place. The null pointer is passed into 'AddPlate' function. Inspect the first argument. Check lines: 497, 535. &lt;a href="https://github.com/OrcaSlicer/OrcaSlicer/blob/a167702038496b97e8495cc79f0c62660063ac87/src/slic3r/GUI/ObjectDataViewModel.cpp#L497" rel="noopener noreferrer"&gt;ObjectDataViewModel.cpp 497&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;wxDataViewItem&lt;/span&gt; &lt;span class="n"&gt;ObjectDataViewModel&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;AddOutsidePlate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;refresh&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;wxDataViewItem&lt;/span&gt; &lt;span class="n"&gt;plate_item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;AddPlate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;nullptr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_L&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Outside"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;         &lt;span class="c1"&gt;// &amp;lt;=&lt;/span&gt;
  &lt;span class="n"&gt;m_plate_outside&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ObjectDataViewModelNode&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;plate_item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetID&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

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

&lt;span class="c1"&gt;// ....&lt;/span&gt;

&lt;span class="n"&gt;wxDataViewItem&lt;/span&gt; &lt;span class="n"&gt;ObjectDataViewModel&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;AddPlate&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;PartPlate&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;part_plate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wxString&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;refresh&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt;  &lt;span class="n"&gt;plate_idx&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;part_plate&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;part_plate&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;get_index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&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="c1"&gt;// &amp;lt;= 1 &lt;/span&gt;
  &lt;span class="n"&gt;wxString&lt;/span&gt; &lt;span class="n"&gt;plate_name&lt;/span&gt; &lt;span class="o"&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="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;empty&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; 
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;plate_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_L&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Plate"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;plate_name&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;wxString&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" %d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;plate_idx&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="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="n"&gt;part_plate&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;get_plate_name&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;                          &lt;span class="c1"&gt;// &amp;lt;= 2&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;plate_name&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;wxString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" ("&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;wxConvUTF8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;from_u8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;part_plate&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;get_plate_name&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;wxString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;")"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;wxConvUTF8&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="c1"&gt;// ....&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;obj_idx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;obj_idx&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;m_objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;obj_idx&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;obj_node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;m_objects&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;obj_idx&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="n"&gt;part_plate&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;part_plate&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;contain_instance_totally&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj_idx&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="c1"&gt;// &amp;lt;= 3&lt;/span&gt;
      &lt;span class="n"&gt;ReparentObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;plate_node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;obj_node&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;return&lt;/span&gt; &lt;span class="n"&gt;plate_item&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;This is a long snippet, so let's break it down step by step. The &lt;code&gt;AddOutsidePlate&lt;/code&gt; member function calls &lt;code&gt;AddPlate&lt;/code&gt; and passes &lt;code&gt;nullptr&lt;/code&gt; as the first argument. The &lt;code&gt;part_plate&lt;/code&gt; parameter is checked two out of the three times where it's used. If a check is missing somewhere, the compiler may assume that the other checks are unnecessary as well and remove them for optimization purposes.&lt;/p&gt;

&lt;p&gt;We can fix the code by adding the missing check. Here's a fun fact: if we look at the &lt;a href="https://github.com/OrcaSlicer/OrcaSlicer/commit/14cb2449c6b4b60ea9587e8f94ae32d554b10a8f#diff-b491cdc4033e90ac53f2919d673d1a51e1e673ca4143cf0b9887905e537f22f1L458-R463" rel="noopener noreferrer"&gt;commit history&lt;/a&gt;, we can see that &lt;a href="https://github.com/OrcaSlicer/OrcaSlicer/commit/14cb2449c6b4b60ea9587e8f94ae32d554b10a8f#diff-b491cdc4033e90ac53f2919d673d1a51e1e673ca4143cf0b9887905e537f22f1L461" rel="noopener noreferrer"&gt;there was&lt;/a&gt; originally a ternary operator there, but the code was later rewritten.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example N7&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v797/" rel="noopener noreferrer"&gt;V797&lt;/a&gt; The 'find' function is used as if it returned a bool type. The return value of the function should probably be compared with std::string::npos. &lt;a href="https://github.com/OrcaSlicer/OrcaSlicer/blob/a167702038496b97e8495cc79f0c62660063ac87/src/slic3r/GUI/HintNotification.cpp#L175" rel="noopener noreferrer"&gt;HintNotification.cpp 175&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;TagCheckResult&lt;/span&gt; &lt;span class="nf"&gt;tag_check_material&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;tag&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="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;GUI&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Tab&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;tab&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;wxGetApp&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;get_tab&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Preset&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="n"&gt;TYPE_FILAMENT&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; 
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// search PrintConfig filament_type to find if allowed tag&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wxGetApp&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;app_config&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"filament_type"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;Preset&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;preset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tab&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;m_presets&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;get_edited_preset&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="k"&gt;auto&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;opt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
        &lt;span class="n"&gt;preset&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;opt&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ConfigOptionStrings&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"filament_type"&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="n"&gt;opt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;values&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="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;TagCheckAffirmative&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;TagCheckNegative&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;TagCheckNotCompatible&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;TagCheckNotCompatible&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code contains a comment explaining that a tag needs to be found. Below is the condition where the tag is searched for. However, it doesn't work as intended:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;if the tag is found at the beginning, &lt;code&gt;find&lt;/code&gt; returns &lt;code&gt;0&lt;/code&gt;, which is converted to &lt;code&gt;false&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;if the tag is found somewhere in the middle, &lt;code&gt;find&lt;/code&gt; returns a positive number, which is converted to &lt;code&gt;true&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;if the tag is not found, &lt;code&gt;find&lt;/code&gt; returns &lt;code&gt;std::string::npos&lt;/code&gt;, which is also converted to &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As a result, the condition doesn't check whether the tag was found. Instead, it checks whether the tag isn't located at the beginning of the string. The fix may look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wxGetApp&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;app_config&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"filament_type"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;npos&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;Example N8&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v714/" rel="noopener noreferrer"&gt;V714&lt;/a&gt; Variable 'face' is not passed into foreach loop by a reference, but its value is changed inside of the loop. &lt;a href="https://github.com/OrcaSlicer/OrcaSlicer/blob/a167702038496b97e8495cc79f0c62660063ac87/src/libslic3r/MeshBoolean.cpp#L153" rel="noopener noreferrer"&gt;MeshBoolean.cpp 153&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;_Mesh&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; 
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;triangle_mesh_to_cgal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;TriangleMesh&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_Mesh&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
  &lt;span class="c1"&gt;// Number the faces because 'orient_to_bound_a_volume' &lt;/span&gt;
  &lt;span class="c1"&gt;// needs a face &amp;lt;--&amp;gt; index map&lt;/span&gt;
  &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;face&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;faces&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;          &lt;span class="c1"&gt;// &amp;lt;=&lt;/span&gt;
    &lt;span class="n"&gt;face&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;CGAL&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;SM_Face_index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The analyzer reports a suspicious range-based loop: the &lt;code&gt;face&lt;/code&gt; loop variable is declared as a copy of the current element from the &lt;code&gt;out.faces()&lt;/code&gt; range, but the code modifies it on every iteration. Most likely, the code was intended to modify elements inside the &lt;code&gt;out.faces()&lt;/code&gt; range.&lt;/p&gt;

&lt;p&gt;If you need to modify elements within a range, declare the loop variable as an lvalue reference:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;face&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;faces&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; 
  &lt;span class="n"&gt;face&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;CGAL&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;SM_Face_index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's also make sure that the elements in the &lt;code&gt;out.faces()&lt;/code&gt; range aren't the same as &lt;code&gt;std::reference_wrapper&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;
  Spoiler: no, they aren't
  &lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;I&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Iterator_range&lt;/span&gt;
  &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;pair&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;I&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;I&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="n"&gt;Iterator_range&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="n"&gt;make_range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Iterator_range&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// .... &lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SM_Index&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;// ....&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="n"&gt;boost&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="n"&gt;size_type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
&lt;span class="nl"&gt;protected:&lt;/span&gt;
  &lt;span class="n"&gt;size_type&lt;/span&gt; &lt;span class="n"&gt;idx_&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SM_Face_index&lt;/span&gt;
  &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;SM_Index&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;SM_Face_index&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* .... */&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="n"&gt;SM_Face_index&lt;/span&gt; &lt;span class="n"&gt;Face_index&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;P&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Surface_mesh&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;// ....&lt;/span&gt;
&lt;span class="nl"&gt;private:&lt;/span&gt;
  &lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;Index_&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Index_iterator&lt;/span&gt;
    &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;boost&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;iterator_facade&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;Index_iterator&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Index_&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                     &lt;span class="n"&gt;Index_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                     &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;random_access_iterator_tag&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                     &lt;span class="n"&gt;Index_&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;// ....&lt;/span&gt;
  &lt;span class="nl"&gt;private:&lt;/span&gt;
    &lt;span class="k"&gt;friend&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;boost&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;iterator_core_access&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// ....&lt;/span&gt;
    &lt;span class="n"&gt;Index_&lt;/span&gt; &lt;span class="n"&gt;dereference&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;hnd_&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;Index_&lt;/span&gt; &lt;span class="n"&gt;hnd_&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;Surface_mesh&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;mesh_&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
  &lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="n"&gt;Index_iterator&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Face_index&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Face_iterator&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="n"&gt;Iterator_range&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Face_iterator&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Face_range&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;Face_iterator&lt;/span&gt; &lt;span class="nf"&gt;faces_begin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Face_iterator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Face_index&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;/// End iterator for faces.&lt;/span&gt;
  &lt;span class="n"&gt;Face_iterator&lt;/span&gt; &lt;span class="nf"&gt;faces_end&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Face_iterator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Face_index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num_faces&lt;/span&gt;&lt;span class="p"&gt;()),&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;Face_range&lt;/span&gt; &lt;span class="nf"&gt;faces&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;make_range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;faces_begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;faces_end&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;There's a lot of code here, so let's try to break it down. In the &lt;code&gt;triangle_mesh_to_cgal&lt;/code&gt; function template, the out parameter has the &lt;code&gt;_Mesh&lt;/code&gt; template type. Most often, function template arguments are specializations of the &lt;code&gt;Surface_mesh&lt;/code&gt; class template.&lt;/p&gt;

&lt;p&gt;In the private class section, the template of the &lt;code&gt;Index_iterator&lt;/code&gt; iterator, which inherits from &lt;code&gt;[boost::iterator_facade](https://www.boost.org/doc/libs/latest/libs/iterator/doc/facade-and-adaptor.html)&lt;/code&gt;, is defined. Essentially, this class template simplifies iterator implementation. Let's take a look at its template parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;
    &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Derived&lt;/span&gt; &lt;span class="c1"&gt;// The derived iterator type being constructed&lt;/span&gt;
  &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Value&lt;/span&gt;
  &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CategoryOrTraversal&lt;/span&gt;
  &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Reference&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;
  &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Difference&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;ptrdiff_t&lt;/span&gt;
&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;iterator_facade&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The fourth template parameter specifies the &lt;code&gt;[reference](https://cppreference.com/cpp/iterator/iterator_traits)&lt;/code&gt; type, which is the value the iterator returns when dereferenced. If it isn't specified explicitly, &lt;code&gt;reference&lt;/code&gt; defaults to &lt;code&gt;Value &amp;amp;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Index_iterator&lt;/code&gt; class template we're interested in passes its own template type as the fourth argument. This means dereferencing the iterator returns a copy of &lt;code&gt;Index_&lt;/code&gt;. The &lt;code&gt;Index_&lt;/code&gt; template parameter is of the &lt;code&gt;Face_index&lt;/code&gt; type. This is an alias of the &lt;code&gt;SM_Face_index&lt;/code&gt; type, which doesn't define any data members and inherits from the &lt;code&gt;SM_Index&lt;/code&gt; class template. The &lt;code&gt;SM_Index&lt;/code&gt; class template defines a single &lt;code&gt;idx&lt;/code&gt; data member of the &lt;code&gt;boost::uint32_t&lt;/code&gt; type, which is a built-in integral type.&lt;/p&gt;



&lt;br&gt;
&lt;p&gt;&lt;/p&gt;

&lt;p&gt;I find it difficult to say what the code should've looked like here, since changing the &lt;code&gt;face&lt;/code&gt; variable type to &lt;code&gt;auto &amp;amp;&lt;/code&gt; doesn't seem like a valid fix. Moreover, the code should stop compiling because an lvalue reference can't bind to a prvalue object.&lt;/p&gt;

&lt;p&gt;When &lt;code&gt;Face_iterator&lt;/code&gt; is dereferenced, it returns a copy of the &lt;code&gt;Face_index&lt;/code&gt; object. To modify anything within the &lt;code&gt;out.faces()&lt;/code&gt; range, the iterator would need to return a reference and somehow be connected to the &lt;code&gt;Surface_mesh&lt;/code&gt; internal data. In that case, the range-based &lt;code&gt;for&lt;/code&gt; loop would work correctly. However, &lt;code&gt;Surface_mesh&lt;/code&gt; and its iterator come from a third-party library, and I doubt we can make any changes there.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example N9&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v609/" rel="noopener noreferrer"&gt;V609&lt;/a&gt; Divide by zero. Denominator range [0x0..0x7FFFFFFFFFFFFFFF]. &lt;a href="https://github.com/OrcaSlicer/OrcaSlicer/blob/a167702038496b97e8495cc79f0c62660063ac87/src/libslic3r/Support/TreeSupport3D.cpp#L811" rel="noopener noreferrer"&gt;TreeSupport3D.cpp 811&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;nodiscard&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;Polygons&lt;/span&gt; &lt;span class="nf"&gt;safe_offset_inc&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="n"&gt;distance&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;do_final_difference&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;diff&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ret&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;collision_trimmed&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
                               &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;union_&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ret&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="n"&gt;safe_step_size&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;last_step_offset_without_check&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;BOOST_LOG_TRIVIAL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;warning&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="s"&gt;"Offset increase got invalid parameter!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;tree_supports_show_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="s"&gt;"Negative offset distance... How did you manage this ?"&lt;/span&gt;&lt;span class="n"&gt;sv&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;do_final_difference&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;diff&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ret&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;collision_trimmed&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
                               &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;union_&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ret&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;coord_t&lt;/span&gt; &lt;span class="n"&gt;step_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;safe_step_size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;steps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;distance&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;last_step_offset_without_check&lt;/span&gt; 
                &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;distance&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;last_step_offset_without_check&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;step_size&lt;/span&gt;
                &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The analyzer reports a division by zero. To understand what's happening, we'll start from the end. The &lt;code&gt;step_size&lt;/code&gt; variable equals &lt;code&gt;safe_step_size&lt;/code&gt;, which means the division by zero occurs if &lt;code&gt;safe_step_size == 0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;At the beginning of the function, there's a &lt;code&gt;safe_step_size &amp;lt; 0&lt;/code&gt; check, which catches only negative values and doesn't check for zero. This means the function doesn't return early when &lt;code&gt;safe_step_size == 0&lt;/code&gt;, which eventually leads to a division by zero.&lt;/p&gt;

&lt;p&gt;This issue appears quite often and is related to using the constants 0, 1, and 2. We noticed this pattern and even wrote an article about it: "&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/0713/" rel="noopener noreferrer"&gt;Zero, one, two, Freddy's coming for you&lt;/a&gt;."&lt;/p&gt;

&lt;p&gt;Other V609 warnings:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;V609 Divide by zero. Denominator range [0..2]. GCode.hpp 303&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example N10&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v593/" rel="noopener noreferrer"&gt;V593&lt;/a&gt; Consider reviewing the expression of the 'A = B &amp;lt; C' kind. The expression is calculated as following: 'A = (B &amp;lt; C)'. &lt;a href="https://github.com/OrcaSlicer/OrcaSlicer/blob/a167702038496b97e8495cc79f0c62660063ac87/src/libslic3r/PerimeterGenerator.cpp#L1057" rel="noopener noreferrer"&gt;PerimeterGenerator.cpp 1057&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;LineType&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LinesDistancer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="n"&gt;Floating&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;conditional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;is_floating_point&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Scalar&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;::&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Scalar&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;::&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;SIGNED_DISTANCE&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Floating&lt;/span&gt; &lt;span class="n"&gt;distance_from_lines&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="cm"&gt;/*.... */&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;//....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;tuple&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ExtrusionPaths&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Polygons&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 
  &lt;span class="n"&gt;generate_extra_perimeters_over_overhangs&lt;/span&gt;&lt;span class="p"&gt;(....)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;size_t&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;0&lt;/span&gt;&lt;span class="p"&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="n"&gt;overhang_region&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;front&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;polyline&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Point&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;overhang_region&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;front&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;polyline&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;points&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;to_point&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="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
      &lt;span class="n"&gt;lower_layer_aabb_tree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;distance_from_lines&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;min_dist&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;min_dist&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;min_dist_idx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&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="c1"&gt;// ....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The devs wanted to write clean code, but overlooked operator precedence. The &lt;code&gt;&amp;lt;&lt;/code&gt; operator has higher precedence than assignment, so the &lt;code&gt;d&lt;/code&gt; variable receives the result of the comparison—a &lt;code&gt;bool&lt;/code&gt; value instead of &lt;code&gt;double&lt;/code&gt;. As a result, &lt;code&gt;min_dist&lt;/code&gt; gets either &lt;code&gt;0&lt;/code&gt; or &lt;code&gt;1&lt;/code&gt; instead of the actual distance. Here's how we can fix this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lower_layer_aabb_tree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;distance_from_lines&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;min_dist&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alternatively, we can fix it like this if the compiler supports the &lt;code&gt;if&lt;/code&gt; statement with an initializer (C++17):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lower_layer_aabb_tree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;distance_from_lines&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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;d&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;min_dist&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;Example N11&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v766/" rel="noopener noreferrer"&gt;V766&lt;/a&gt; An item with the same key '"open"' has already been added. &lt;a href="https://github.com/OrcaSlicer/OrcaSlicer/blob/a167702038496b97e8495cc79f0c62660063ac87/src/slic3r/GUI/Widgets/DialogButtons.hpp#L68-L69" rel="noopener noreferrer"&gt;DialogButtons.hpp 69&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DialogButtons&lt;/span&gt;  &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;wxPanel&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;private:&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;wxString&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wxStandardID&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;m_standardIDs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Choice&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"ok"&lt;/span&gt;         &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wxID_OK&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"yes"&lt;/span&gt;        &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wxID_YES&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"apply"&lt;/span&gt;      &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wxID_APPLY&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"confirm"&lt;/span&gt;    &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wxID_APPLY&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="c1"&gt;// no id for confirm, reusing wxID_APPLY&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"no"&lt;/span&gt;         &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wxID_NO&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"cancel"&lt;/span&gt;     &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wxID_CANCEL&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="c1"&gt;// Action&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"open"&lt;/span&gt;       &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wxID_PRINT&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;  &lt;span class="c1"&gt;// &amp;lt;=&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"open"&lt;/span&gt;       &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wxID_OPEN&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;   &lt;span class="c1"&gt;// &amp;lt;=&lt;/span&gt;
  &lt;span class="c1"&gt;//....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two elements with the &lt;code&gt;open&lt;/code&gt; key were added to the initialization of &lt;code&gt;std::map&lt;/code&gt;. A container can't store multiple elements with the same key, so the second element won't be added.&lt;/p&gt;

&lt;p&gt;Looking at the values, we can see that the key for &lt;a href="https://docs.wxwidgets.org/3.2/defs_8h.html#ac66d0a09761e7d86b2ac0b2e0c6a8cbba4d4fdc32f37cc92c05d7a4ff2bb12ee2" rel="noopener noreferrer"&gt;wxID_PRINT&lt;/a&gt; wasn't updated after copying. As a result, the wrong value will be used. The fix may look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"print"&lt;/span&gt;      &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wxID_PRINT&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"open"&lt;/span&gt;       &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wxID_OPEN&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;/p&gt;
  Other V766 warnings
  &lt;br&gt;
V766 An item with the same key '"use_firmware_retraction"' has already been added. Print.cpp 229&lt;br&gt;
V766 An item with the same key '"filament_notes"' has already been added. Print.cpp 237&lt;br&gt;
V766 An item with the same key '"nozzle_volume"' has already been added. PrintConfig.cpp 8201&lt;br&gt;
V766 An item with the same key '"inner-outer-inner wall/infill"' has already been added. PrintConfig.cpp 272



&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example N12&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v734/" rel="noopener noreferrer"&gt;V734&lt;/a&gt; An excessive expression. Examine the substrings "plus" and "xplus". &lt;a href="https://github.com/OrcaSlicer/OrcaSlicer/blob/a167702038496b97e8495cc79f0c62660063ac87/src/slic3r/Utils/QidiPrinterAgent.cpp#L375" rel="noopener noreferrer"&gt;QidiPrinterAgent.cpp 375&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;QidiPrinterAgent&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;infer_series_id&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;model_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;dev_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;   &lt;span class="p"&gt;(&lt;/span&gt;   &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"xplus"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;npos&lt;/span&gt; 
          &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"plus"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;npos&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"4"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;npos&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="s"&gt;"0"&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="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The analyzer detected an unnecessary call in the condition. If &lt;code&gt;key&lt;/code&gt; contains the &lt;code&gt;xplus&lt;/code&gt; substring, it also contains &lt;code&gt;plus&lt;/code&gt;. The second &lt;code&gt;find("plus")&lt;/code&gt; check is redundant because it will always evaluate to true whenever the first one does. Checking &lt;code&gt;plus&lt;/code&gt; is enough:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"plus"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;npos&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"4"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;npos&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code also looks rather inefficient: the string is iterated over three times.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example N13&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v583/" rel="noopener noreferrer"&gt;V583&lt;/a&gt; The '?:' operator, regardless of its conditional expression, always returns one and the same value: wxEmptyString. &lt;a href="https://github.com/OrcaSlicer/OrcaSlicer/blob/a167702038496b97e8495cc79f0c62660063ac87/src/slic3r/GUI/Search.cpp#L96" rel="noopener noreferrer"&gt;Search.cpp 96&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;Option&lt;/span&gt; &lt;span class="nf"&gt;create_option&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;opt_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;wxString&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;Preset&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;GroupAndCategory&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;gc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;wxString&lt;/span&gt; &lt;span class="n"&gt;suffix&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;wxString&lt;/span&gt; &lt;span class="n"&gt;suffix_local&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="n"&gt;gc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;category&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"Machine limits"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//suffix       = opt_key.back() == '1' ? L("Stealth") : L("Normal");&lt;/span&gt;
    &lt;span class="n"&gt;suffix&lt;/span&gt;       &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;opt_key&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;back&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sc"&gt;'1'&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;wxEmptyString&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;wxEmptyString&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;suffix_local&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;suffix&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;suffix&lt;/span&gt;       &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;suffix&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// ....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's a typo in the ternary operator: both branches return &lt;code&gt;wxEmptyString&lt;/code&gt;. The condition doesn't affect the result. The commented-out line above indicates that the code previously had different logic, but it works differently in its current form.&lt;/p&gt;

&lt;p&gt;The exact same code was copied to another snippet as well:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/docs/warnings/v583/" rel="noopener noreferrer"&gt;V583&lt;/a&gt; The '?:' operator, regardless of its conditional expression, always returns one and the same value: wxEmptyString. &lt;a href="https://github.com/OrcaSlicer/OrcaSlicer/blob/a167702038496b97e8495cc79f0c62660063ac87/src/slic3r/GUI/Search.cpp#L348" rel="noopener noreferrer"&gt;Search.cpp 348&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example N14&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio warning: &lt;a href="https://pvs-studio.com/en/docs/warnings/v670/" rel="noopener noreferrer"&gt;V670&lt;/a&gt; The uninitialized class member 'm_result' is used to initialize the 'm_options_z_corrector' member. Remember that members are initialized in the order of their declarations inside a class. &lt;a href="https://github.com/OrcaSlicer/OrcaSlicer/blob/a167702038496b97e8495cc79f0c62660063ac87/src/libslic3r/GCode/GCodeProcessor.cpp#L1671" rel="noopener noreferrer"&gt;GCodeProcessor.cpp 1671&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OptionsZCorrector&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;GCodeProcessorResult&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;m_result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="k"&gt;explicit&lt;/span&gt; &lt;span class="n"&gt;OptionsZCorrector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;GCodeProcessorResult&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m_result&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// ....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GCodeProcessor&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
  &lt;span class="n"&gt;OptionsZCorrector&lt;/span&gt; &lt;span class="n"&gt;m_options_z_corrector&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// line: 811&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
  &lt;span class="n"&gt;GCodeProcessorResult&lt;/span&gt; &lt;span class="n"&gt;m_result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// line: 843&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;GCodeProcessor&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;GCodeProcessor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;m_options_z_corrector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m_result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An uninitialized variable is used in the constructor initialization list. Class data members initialize in the order they're declared, and in this code, &lt;code&gt;m_options_z_corrector&lt;/code&gt; initializes &lt;a href="https://github.com/OrcaSlicer/OrcaSlicer/blob/a167702038496b97e8495cc79f0c62660063ac87/src/libslic3r/GCode/GCodeProcessor.hpp#L811-L843" rel="noopener noreferrer"&gt;before&lt;/a&gt; &lt;code&gt;m_result&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Currently, the code works correctly because memory for the object gets allocated before initialization begins. In this case, it's possible to retrieve the address and read it, which is exactly what happens in the &lt;code&gt;OptionsZCorrector&lt;/code&gt; constructor. However, if the constructor starts using the &lt;code&gt;m_result&lt;/code&gt; variable, it will lead to undefined behavior.&lt;/p&gt;

&lt;p&gt;To fix the code, the data member declarations in the class should be reordered so that &lt;code&gt;m_result&lt;/code&gt; appears before &lt;code&gt;m_options_z_corrector&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Looking ahead
&lt;/h2&gt;

&lt;p&gt;If you wish to minimize errors in your projects, incorporate static analysis into your &lt;a href="https://pvs-studio.com/en/blog/posts/1218/" rel="noopener noreferrer"&gt;regular workflow&lt;/a&gt;—and &lt;a href="https://pvs-studio.com/en/pvs-studio/download/?utm_source=website&amp;amp;utm_medium=devto&amp;amp;utm_campaign=article&amp;amp;utm_content=1389" rel="noopener noreferrer"&gt;PVS-Studio&lt;/a&gt; can help you do just that.&lt;/p&gt;

&lt;p&gt;You can &lt;a href="https://pvs-studio.com/en/order/" rel="noopener noreferrer"&gt;request pricing&lt;/a&gt;, a &lt;a href="https://pvs-studio.com/en/pvs-studio/try-free/?utm_source=website&amp;amp;utm_medium=devto&amp;amp;utm_campaign=article&amp;amp;utm_content=1389" rel="noopener noreferrer"&gt;trial version&lt;/a&gt;, or learn &lt;a href="https://pvs-studio.com/en/blog/posts/1286/" rel="noopener noreferrer"&gt;about free licensing terms&lt;/a&gt; in the relevant sections. &lt;/p&gt;

&lt;p&gt;Look ahead to your project's future and catch errors as early as possible :)&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>programming</category>
      <category>software</category>
      <category>tooling</category>
    </item>
    <item>
      <title>JS &amp; TS digest: ES2026 updates, runtime showdown, and PVS-Studio EAP</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Fri, 26 Jun 2026 12:32:06 +0000</pubDate>
      <link>https://dev.to/pvsdev/js-ts-digest-es2026-updates-runtime-showdown-and-pvs-studio-eap-26d0</link>
      <guid>https://dev.to/pvsdev/js-ts-digest-es2026-updates-runtime-showdown-and-pvs-studio-eap-26d0</guid>
      <description>&lt;p&gt;While frontend engineers keep rebuilding their projects around new build tools, JavaScript itself keeps getting faster, safer, and more mature. Here's the most interesting JS and TS news from the past few months.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2cmcy7dfikidq53v8ane.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2cmcy7dfikidq53v8ane.png" alt="1388_jsdigest/image1.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introducing ES2026: what's new in the standard?
&lt;/h2&gt;

&lt;p&gt;TC39 has officially approved a list of features for &lt;a href="https://socket.dev/blog/tc39-advances-temporal-to-stage-4" rel="noopener noreferrer"&gt;ECMAScript 2026&lt;/a&gt;. The language continues to shed its old "baggage":&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Explicit Resource Management&lt;/strong&gt; (the &lt;code&gt;using&lt;/code&gt; keyword). This feature has been in development for a long time in TypeScript and finally landed in native JavaScript. Now you can automatically allocate resources as soon as a variable goes out of scope.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Iterator Helpers&lt;/strong&gt; (new methods for iterators). You no longer need to convert iterators to arrays using &lt;code&gt;Array.from()&lt;/code&gt; just to call &lt;code&gt;.map()&lt;/code&gt; or &lt;code&gt;.filter()&lt;/code&gt;. Lazy evaluation is now available right out of the box.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can track the progress of these features and their status in the official &lt;a href="https://github.com/tc39/proposals" rel="noopener noreferrer"&gt;TC39 GitHub Proposals&lt;/a&gt; repository. The Explicit Resource Management and Iterator Helpers features are now in the final development stages.&lt;/p&gt;

&lt;h2&gt;
  
  
  Runtime Showdown: Node.js against the World
&lt;/h2&gt;

&lt;p&gt;In 2026, Node.js finally cemented its status as a mature development environment that successfully adopts features from newer competitors (Bun and Deno). The biggest trend this year in runtime environments is running TypeScript files without third-party tools or transpilers like ts-node, thanks to fast built-in parsers.&lt;/p&gt;

&lt;p&gt;Full details are on the &lt;a href="https://nodejs.org/en/blog" rel="noopener noreferrer"&gt;Node.js Official Blog&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;TypeScript 7.0: Beta announcement and migration to Go&lt;/p&gt;

&lt;p&gt;On April 21, 2026, Microsoft officially released TypeScript 7.0 Beta. This isn't just a minor update but a major revolution under the hood.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The tsgo compiler&lt;/strong&gt;. The development team has fully ported the existing type-checking logic from the old JS/TS engine to the new native compiler written in Go.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;10x faster&lt;/strong&gt;. Go's multithreading brings a tenfold speedup in build and type-checking times for large projects, compared to 6.0.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Architectural parity&lt;/strong&gt;. The language's semantics remained the same: Microsoft carefully moved the framework's code to ensure that existing projects wouldn't break during the transition. The new feature is already being tested extensively at Slack, Google, Figma, and Vercel.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Early Access: PVS-Studio's new JavaScript &amp;amp; TypeScript analyzers
&lt;/h2&gt;

&lt;p&gt;We've spent years specializing in C, C++, C#, and Java—but web development has grown too important to ignore. We're now building new analyzers for JavaScript and TypeScript, and we'd like you to try them!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Feisey9dqq9v8ma4le747.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Feisey9dqq9v8ma4le747.png" alt="1388_jsdigest/image2.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio is currently running the Early Access Program. As part of the EAP, our priority is to check whether the new analyzers are stable, perform well, and produce high-quality analysis on real projects. We invite you to participate in the testing: &lt;a href="https://pvs-studio.com/en/pvs-studio-eap/" rel="noopener noreferrer"&gt;sign up here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to read?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/AllThingsSmitty/typescript-tips-everyone-should-know" rel="noopener noreferrer"&gt;TypeScript Tips Everyone Should Know&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This article provides a roundup of practical TypeScript techniques for improving safety, readability, and developer experience. For example, the author advises using safe input data validation, fewer unnecessary type signatures, &lt;code&gt;satisfies&lt;/code&gt; instead of rough type casting, etc. More useful patterns are in the full article.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/js/1381/" rel="noopener noreferrer"&gt;Error that we had to ignore&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Developing a static analyzer, as with any software project, involves trade-offs. Sometimes this means dropping perfectly valid analyzer warnings to make the tool better overall. The article illustrates this trade-off through a diagnostic rule designed to detect typos caused by identical operands in binary expressions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://odysse.io/en/javascript-in-2026-a-comprehensive-guide-to-the-ecosystem-revolution/" rel="noopener noreferrer"&gt;JavaScript in 2026: A Comprehensive Guide to the Ecosystem Revolution&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you missed the Runtime Wars, you can read this comprehensive overview. You'll explore how fierce competition between Node.js, Bun, and Deno has forced all players to implement native TypeScript support and built-in security tools. This is a great long-read article for understanding where the entire industry is headed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://blog.logrocket.com/typescript-at-scale-2026/" rel="noopener noreferrer"&gt;TypeScript at scale in 2026: What senior engineers should know&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This article focuses on how to effectively use TypeScript in large codebases, monorepos, and large teams. The author points out that by 2026, the question of whether to use TypeScript is no longer relevant. The language has become the standard, shifting the focus toward efficiently managing code at the system level.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to listen?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://typescript.fm/" rel="noopener noreferrer"&gt;Podcast: TypeScript.fm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you've been searching for a focused, lively show about TypeScript, this is it. Hosts Kamran Ayub and Erik Onarheim cover the latest language updates. In a recent episode, they discussed the updates to TypeScript 7.0 Beta and debated why the new tsgo compiler sometimes uses too much memory.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://syntax.fm/" rel="noopener noreferrer"&gt;Podcast: Syntax.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Meet one of the well-known podcasts on frontend and full-stack development, hosted by Wes Bos, Scott Tolinski, and CJ Reynolds. No fluff, just a strong focus on practice and experience. Every week, they release episodes in a variety of formats: from in-depth analyses of complex bugs in JavaScript and TypeScript to relaxed Supper Club interviews with creators of top-tier libraries, frameworks, and modern automation tools like Biome, ESLint, and Vite.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrap-up
&lt;/h2&gt;

&lt;p&gt;Thank you for reading our digest! Share with us any other fresh news or events you find noteworthy. You can submit them using our &lt;a href="https://pvs-studio.com/en/about-feedback/?is_question_form_open=true" rel="noopener noreferrer"&gt;feedback form&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To stay up to date on the latest news, follow us on &lt;a href="https://x.com/pvs_studio" rel="noopener noreferrer"&gt;X&lt;/a&gt; and &lt;a href="https://www.linkedin.com/company/pvs-studio" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;. We post articles by our company's experts, video reviews featuring the latest analyzer updates, event announcements, memes, and much more. Most importantly, you're always welcome to leave comments—we'll be happy to respond!&lt;/p&gt;

&lt;p&gt;Don't forget, PVS-Studio stands guard over your codebase.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/jsdigest" rel="noopener noreferrer"&gt;Get the 30-day trial promo code here!&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>news</category>
    </item>
    <item>
      <title>We're building custom programming language—would you join us?</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Tue, 23 Jun 2026 14:24:50 +0000</pubDate>
      <link>https://dev.to/pvsdev/were-building-custom-programming-language-would-you-join-us-3afa</link>
      <guid>https://dev.to/pvsdev/were-building-custom-programming-language-would-you-join-us-3afa</guid>
      <description>&lt;p&gt;What is expertise, and what lies at its core? Expertise varies in depth, field, and purpose. But some things never change: it can't exist without theory and experience; it can't emerge in an environment free from challenges and mistakes. Every person develops their own unique expertise. Today, we'll share our experience, which may inspire you to enhance your expertise. Also, we invite you to challenge your brain and guess some unexpected inventions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwy9xzwtiryg023i8dchm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwy9xzwtiryg023i8dchm.png" alt="1387_letsmakelanguage/image1.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What gives rise to expertise in programming? In the past, it was curiosity—the urge to simplify and overcome challenges. Today, it shaped by projects that deliver business value. But even now, we can arm ourselves with those qualities and reach the outcomes we aim for. After all, code lets us stay flexible and venture down untrodden paths. Quite often, they can lead to the beginning of something much greater—and history has plenty of proof.&lt;/p&gt;

&lt;p&gt;Below, you'll find stories of such unexpected success. Can you guess what invention each one describes? Share your guesses in the comments or read the article to the end, where we'll reveal all the secrets.&lt;/p&gt;

&lt;h2&gt;
  
  
  Story #1
&lt;/h2&gt;

&lt;p&gt;A Danish-Canadian developer wanted to create a convenient tool for tracking visits to his online CV. The solution proved so useful and effective and quickly went beyond personal use. It still remains popular among web developers. What is the solution?&lt;/p&gt;

&lt;h2&gt;
  
  
  Story #2
&lt;/h2&gt;

&lt;p&gt;Two developers set out to solve the problem of migrating software from 32-bit to 64-bit systems. They created a tool to help detect errors the transition caused. Later, they realized it could also find other issues and transformed it into a full-featured tool that could handle a wider range of tasks. What the solution does?&lt;/p&gt;

&lt;h2&gt;
  
  
  Story #3
&lt;/h2&gt;

&lt;p&gt;An engineer came up with this solution to fix an issue in his online store where items would simply disappear from the cart when users navigated to another store section. Today, this discovery powers the entire world of online advertising and user tracking.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does expertise mean for PVS-Studio?
&lt;/h2&gt;

&lt;p&gt;We hope you guessed what the solutions were. Now, let's go back to the main topic: expertise. Why does our company value it? How does it reveal itself in our work? Now we'll show how our new practice helps transform our expertise into benefits for the developer community.&lt;/p&gt;

&lt;p&gt;We work closely with programming languages because our mission is to help developers write clean code. Our devs should understand languages down to their core to create algorithms that detect common and critical errors, as well as potential vulnerabilities in code. This expertise enables us to teach computers how to automatically identify issues.&lt;/p&gt;

&lt;p&gt;The number of diagnostic rules that catch issues in your projects continues to grow every year.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fe0jgiuvc46van9abx6n1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fe0jgiuvc46van9abx6n1.png" alt="1387_letsmakelanguage/image2.png" width="800" height="422"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What stands behind these rules, though? What does the analyzer rely on when searching for errors? It's the core! It powers our &lt;a href="https://pvs-studio.com/en/blog/terms/7004/" rel="noopener noreferrer"&gt;data flow analysis&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/blog/terms/6776/" rel="noopener noreferrer"&gt;signature analysis&lt;/a&gt;, &lt;a href="https://pvs-studio.com/en/blog/posts/1166/" rel="noopener noreferrer"&gt;method annotations&lt;/a&gt;, and all our other analysis technologies. Yuri, our core architect, handles this and other numerous areas. He's a great engineer, a proud cat dad, and simply a cool guy. While going through yet another epic battle with core-level challenges—it's hard to imagine what he deals with every day while diving so deep into the tool's innards—Yuri was pulled away from his usual work by an offer from his colleagues...&lt;/p&gt;

&lt;p&gt;PVS-Studio has a department called Te&amp;amp;De (Teaching &amp;amp; Development). Two wonderful employees, Inna and Yulia, handle internal trainings for our employees. They also organize live talks for managers and developers. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can find information about the upcoming and past talks &lt;a href="https://pvs-studio.com/en/webinar/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;They suggested Yuri share his knowledge in the internal training course for new employees. As the course took shape, we realized that its content would benefit not only new colleagues, but also the broader dev community. This led to the decision to make the materials publicly available for free.&lt;/p&gt;

&lt;h2&gt;
  
  
  A few words about the series of live talks
&lt;/h2&gt;

&lt;p&gt;Since Yuri isn't a fan of text storytelling, we took it upon ourselves to introduce his original series of live talks about creating a toy programming language where he explains what lies behind almost every programming language. Understanding how it works is one thing—teaching a machine to understand it is a much harder challenge.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgck6s1zpg90yn7w6sz8u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgck6s1zpg90yn7w6sz8u.png" alt="1387_letsmakelanguage/image3.png" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It all starts with &lt;a href="https://pvs-studio.com/en/blog/video/11644/" rel="noopener noreferrer"&gt;grammar&lt;/a&gt;, which describes the language structure.&lt;/p&gt;

&lt;p&gt;When it comes to parsing code, the &lt;a href="https://pvs-studio.com/en/blog/video/11665/" rel="noopener noreferrer"&gt;lexer&lt;/a&gt; takes the stage. It extracts individual words from the text and classifies them.&lt;/p&gt;

&lt;p&gt;The next step is to put words into sentences. This is where the &lt;a href="https://pvs-studio.com/en/blog/video/11709/" rel="noopener noreferrer"&gt;parser&lt;/a&gt; comes into play. It takes the words and turns them into language constructs. Then, we organize these constructs into a syntax tree, a hierarchical structure that represents relationships between individual language parts. We traverse the tree and execute or evaluate what it describes. &lt;/p&gt;

&lt;p&gt;A language also consists of different entities that we can name—and it must recognize and understand these names. This is where semantic analysis comes in. Languages also have data types that interact with each other, and determining how they work together is part of semantics.&lt;/p&gt;

&lt;p&gt;The final piece is the evaluator that traverses the syntax tree and calculates the values of variables and expressions.&lt;/p&gt;

&lt;p&gt;This is roughly what a programming language looks like in a simplified and idealized form. In the live coding sessions, Yuri walks through the entire process while viewers can ask questions and discuss the details.&lt;/p&gt;

&lt;p&gt;Our main goal isn't to teach but to satisfy the curiosity that drives many programmers to dive deeper into the topic. We also want to transform the experience gained from building a static analysis tool into exciting content and share this expertise with others. The steps Yuri goes through when creating his own language form the foundation, but it isn't as simple as it may seem. This series of sessions, designed to help developers build their own language, is one of the easiest ways to understand how programming languages actually work.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb8qh4197261o3q11kmpz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb8qh4197261o3q11kmpz.png" alt="1387_letsmakelanguage/image4.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By the way, if you'd like to join Yuri on this journey, there's still time! The next session dedicated to building an Abstract Syntax Tree is scheduled for June 26, 1:00 PM UTC+1. Sign up &lt;a href="https://pvs-studio.com/en/webinar/" rel="noopener noreferrer"&gt;here&lt;/a&gt; and don't forget to check your inbox to confirm the registration!&lt;/p&gt;

&lt;h2&gt;
  
  
  Revealing secrets
&lt;/h2&gt;

&lt;p&gt;While doing our work, we often run into challenges. If we stop, we might miss something important and truly remarkable. But when overcoming a problem, we feel proud of ourselves and gain experience and new skills. By encouraging curiosity, we explore new horizons, sometimes simplify complex things, and open doors we never even knew existed at the beginning of our journey. Now, it's time to lay our cards on the table and tell you what solutions we had in mind.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Story #1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwz8e4moo0kosk8uc4kfk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwz8e4moo0kosk8uc4kfk.png" alt="1387_letsmakelanguage/image5.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Danish-Canadian developer's name is Rasmus Lerdorf, and his invention is Personal Home Page (a set of tools for a personal homepage). It became popular and eventually evolved into PHP, a widely used programming language for websites. Lerdorf combined his scripts with a form-handling library, introducing the basic syntax, and the option to process data from HTML forms. The release of PHP 2.0 turned it into a full-fledged tool for building dynamic websites, revolutionizing web development.&lt;/p&gt;

&lt;p&gt;P.S. One of our designers created this image for another article. It's amazing how large our collection of illustrations has grown over the years, especially considering the exceptional talent of each artist. Recently, our colleague wrote an article on how our mascot, Cody the Unicorn, has changed throughout the entire company's history. Check out &lt;a href="https://pvs-studio.com/en/blog/posts/1372/" rel="noopener noreferrer"&gt;this article&lt;/a&gt; and enjoy the delightful collection of unicorn illustrations and some of the most unconventional designs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Story #2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Of course, it's PVS-Studio. We decided to praise ourselves a little :)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbkbhnn75ymryqjv5zlj8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbkbhnn75ymryqjv5zlj8.png" alt="1387_letsmakelanguage/image6.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It was 2006, the dawn of 64-bit systems. Back then, many devs were thinking about moving away from 32-bit systems, but the transition brought its own share of 64-bit bugs. As a result, two developers created a mechanism to catch such errors and later turned it into a tool that tackled a much wider range of issues. Today, PVS-Studio is a full-fledged company. Back then, it started with two recent grads who had a problem, and their solution grew into something much bigger. By the way, the company recently turned 18—they grow up so fast!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Story #3&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ff2xp3w7vbbokfe2yylb3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ff2xp3w7vbbokfe2yylb3.png" alt="1387_letsmakelanguage/image7.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In 1994, Netscape Communications engineer Lou Montulli encountered an issue: the original HTTP protocol was stateless, meaning a server couldn't track when the same user moved from page to page or made purchases. He suggested storing a small piece of text data on the user's side. These became known as &lt;em&gt;cookies&lt;/em&gt;. This invention forever changed the web industry by making it possible to create user-friendly shopping carts for online stores and enabling users to stay logged in to websites.&lt;/p&gt;

&lt;h2&gt;
  
  
  Some parting words
&lt;/h2&gt;

&lt;p&gt;The PVS-Studio team wishes you success in enhancing your expertise. It's a valuable skill that'll help you grow and reach new heights. We also invite you to join our live talks, where you can gain new insights into programming languages and find inspiration for your projects, whether personal or professional. Your curiosity may also lead to something bigger. All you need is &lt;a href="https://pvs-studio.com/en/webinar/30/" rel="noopener noreferrer"&gt;right here&lt;/a&gt;...&lt;/p&gt;

</description>
      <category>programming</category>
      <category>cpp</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>"They generate more bugs, more security holes": C++ creator takes aim at vibe coding</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Mon, 22 Jun 2026 13:46:59 +0000</pubDate>
      <link>https://dev.to/pvsdev/they-generate-more-bugs-more-security-holes-c-creator-takes-aim-at-vibe-coding-90c</link>
      <guid>https://dev.to/pvsdev/they-generate-more-bugs-more-security-holes-c-creator-takes-aim-at-vibe-coding-90c</guid>
      <description>&lt;p&gt;Bjarne Stroustrup, the creator of the C++ programming language, says modern AI tools still aren't reliable for writing high-quality, complex system code or designing programming languages. Bjarne raised the point on the &lt;a href="https://youtu.be/U46fJ2bJ-co" rel="noopener noreferrer"&gt;Ryan Peterman podcast&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"I think that in the field I'm mostly interested in code will still be written by humans, and they will use abstraction. The examples I've seen of attempts for AI to generate code in this domain has not been successful. They generate more bugs, more security holes."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Stroustrup points to validation as the real problem: AI-driven changes are harder to track because a small prompt change can spread across many parts of a project.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"All of the code that was generated knows more code generated than if it was written by humans. When a human makes a change, it will make a change that's localized. You can look for the effects of that localized change. If an AI writes it, you don't actually know where it's changed. You have to try and figure that out."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;However, the "father of C++" doesn't dismiss AI entirely. He believes that it can be useful for technical writing, provided human reviews the output.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"It's not, at least now, good at safety-critical, performance-critical code. Now, let's say that 70 or 80% of the world's code doesn't fit that pattern. But it's that 10 or 20% of the code that I'm interested in. And there, it's not there."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The podcast also touched on LLMs. Stroustrup has noted that they're trained on existing code, which means they may inherit old approaches and old habits along with it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"I find that LLM-based code is imitating old code and getting old performance and old bugs again. Maybe you can improve that. I hear rumors of Bjarne apps being written that fit my writings, but even that is problematic because I'm not saying exactly the same as I did 20 years ago."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The PVS-Studio team, which builds a static analysis tool with a strong focus on C++, also shared their perspective on the widespread adoption of AI-driven coding.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Phillip Khandeliants, Head of Static Analyzer Development at PVS-Studio&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Bjarne Stroustrup's criticism lands when a developer just copies and pastes the AI output into a project. Depending on the prompt, the model may generate code that's suboptimal, buggy and/or even insecure. We have seen evidence of this when reviewing vibe-coded projects like &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1354/?utm_source=devto&amp;amp;utm_medium=pvs&amp;amp;utm_campaign=media&amp;amp;utm_content=ai" rel="noopener noreferrer"&gt;vib-OS&lt;/a&gt; or &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1366/?utm_source=devto&amp;amp;utm_medium=pvs&amp;amp;utm_campaign=media&amp;amp;utm_content=ai" rel="noopener noreferrer"&gt;markus&lt;/a&gt;. To be fair, the authors of those projects did not aim to ship a production-level solution, but rather to run a case study.&lt;br&gt;
We shouldn't forget that AI-generated and human-written code are no longer different in one matter: they both need to be thoroughly checked. It should go through an extensive code review, static and dynamic analysis, and formal verification processes. Used that way, AI agents can significantly streamline developers' workflow by handling boilerplate, routine tests, and early hypothesis generation, ultimately boosting productivity and development speed."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Oleg Lisiy, C++ Team Lead at PVS-Studio&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Absolutely, AI is just part of how people work now, and that's not going away. The real question is how we should leverage it. Letting an AI agent generate code from an issue description and then push it straight to production has no place in enterprise-level software development. It's hard to imagine anyone actually doing this way.&lt;br&gt;
On the other hand, if criticize AI, we should criticize IDE's autocomplete too, shouldn't we? Just imagine a developer mashing random keys, hitting Tab for autocomplete, and committing the result right away. Sounds crazy, doesn't it? The hype around AI feels just as crazy to me."&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>news</category>
      <category>ai</category>
      <category>cpp</category>
    </item>
  </channel>
</rss>
