<?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>Terrible tips for a C++ developer: 2026 edition, Part 2</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Wed, 02 Dec 2026 14:27:26 +0000</pubDate>
      <link>https://dev.to/pvsdev/terrible-tips-for-a-c-developer-2026-edition-part-2-2ma0</link>
      <guid>https://dev.to/pvsdev/terrible-tips-for-a-c-developer-2026-edition-part-2-2ma0</guid>
      <description>&lt;p&gt;We're introducing an updated edition of the terrible tips for a C++ developer book. We'll be posting it on our blog in parts. The book includes a total of 60 tips.&lt;/p&gt;

&lt;p&gt;Each part will feature a list of "tips" that developers really shouldn't follow. Each tip comes with an explanation of why it's bad, as well as a breakdown of its less obvious aspects. This series is designed to help beginner programmers learn the ropes, while entertaining experienced developers with some of the more unexpected C++ quirks.&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%2Fwyhv99j5korepy5m7do9.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%2Fwyhv99j5korepy5m7do9.png" alt="1431_terrible_tips_pt2/image1.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Terrible tip N5. printf, printf, printf, printf
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;I'll call the printf function as many times as there are lines to print. We've always done it this way.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Although the previous case is unusual, developers often overuse the &lt;code&gt;printf&lt;/code&gt; function. A lot of them have written this way and continue to do so. This style also appears in AI-generated code. In other words, we taught it some bad habits.&lt;/p&gt;

&lt;p&gt;Here's an example of the generated 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="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"File System Commands:\n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"  ls                    - List all files in RAMFS\n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"  cat &amp;lt;filename&amp;gt;        - Display file contents\n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"  touch &amp;lt;file&amp;gt; &amp;lt;text&amp;gt;   - Create/update file with content\n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"  vtext &amp;lt;filename&amp;gt;      - Open text editor (F12=Save, F4=Exit)\n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"\nSystem Commands:\n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"  clear                 - Clear the screen\n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"  help                  - Show this help message\n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"  info                  - Display system information\n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"  test                  - Interactive input test\n"&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 works, but it's redundant. There's no point in calling the slow &lt;code&gt;printf&lt;/code&gt; function over and over again; you can do it more efficiently:&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="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"File System Commands:\n"&lt;/span&gt;
       &lt;span class="s2"&gt;"  ls                    - List all files in RAMFS\n"&lt;/span&gt;
       &lt;span class="s2"&gt;"  cat &amp;lt;filename&amp;gt;        - Display file contents\n"&lt;/span&gt;
       &lt;span class="s2"&gt;"  touch &amp;lt;file&amp;gt; &amp;lt;text&amp;gt;   - Create/update file with content\n"&lt;/span&gt;
       &lt;span class="s2"&gt;"  vtext &amp;lt;filename&amp;gt;      - Open text editor (F12=Save, F4=Exit)\n"&lt;/span&gt;
       &lt;span class="s2"&gt;"\nSystem Commands:\n"&lt;/span&gt;
       &lt;span class="s2"&gt;"  clear                 - Clear the screen\n"&lt;/span&gt;
       &lt;span class="s2"&gt;"  help                  - Show this help message\n"&lt;/span&gt;
       &lt;span class="s2"&gt;"  info                  - Display system information\n"&lt;/span&gt;
       &lt;span class="s2"&gt;"  test                  - Interactive input test\n"&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 is now shorter and faster.&lt;/p&gt;

&lt;p&gt;Of course, optimization isn't required when printing informational text. However, in some cases, you can achieve significant performance improvements by reducing the number of calls to formatting functions within loops.&lt;/p&gt;

&lt;p&gt;However, there's more to it. The code above is easy to break when someone modifies it. You might accidentally include an escape sequence such as &lt;code&gt;%i&lt;/code&gt;. This can happen when a character is part of the text and you simply don't think to escape it.&lt;/p&gt;

&lt;p&gt;We'll discuss security vulnerabilities associated with the &lt;code&gt;printf&lt;/code&gt; function in one of the following chapters. One more improvement is worth making for now: use the &lt;code&gt;puts&lt;/code&gt; function instead. It's short, fast, and safe:&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="nb"&gt;puts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"File System Commands:\n"&lt;/span&gt;
     &lt;span class="s2"&gt;"  ls                    - List all files in RAMFS\n"&lt;/span&gt;
     &lt;span class="s2"&gt;"  cat &amp;lt;filename&amp;gt;        - Display file contents\n"&lt;/span&gt;
     &lt;span class="s2"&gt;"  touch &amp;lt;file&amp;gt; &amp;lt;text&amp;gt;   - Create/update file with content\n"&lt;/span&gt;
     &lt;span class="s2"&gt;"  vtext &amp;lt;filename&amp;gt;      - Open text editor (F12=Save, F4=Exit)\n"&lt;/span&gt;
     &lt;span class="s2"&gt;"\nSystem Commands:\n"&lt;/span&gt;
     &lt;span class="s2"&gt;"  clear                 - Clear the screen\n"&lt;/span&gt;
     &lt;span class="s2"&gt;"  help                  - Show this help message\n"&lt;/span&gt;
     &lt;span class="s2"&gt;"  info                  - Display system information\n"&lt;/span&gt;
     &lt;span class="s2"&gt;"  test                  - Interactive input test"&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 only thing to keep in mind is that &lt;code&gt;puts&lt;/code&gt; appends a newline character to the end of the string. So, to make sure the printed output matches the previous version, I removed one &lt;code&gt;\n&lt;/code&gt; from the text.&lt;/p&gt;

&lt;h2&gt;
  
  
  Terrible tip N6. Macros are cool
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Use macros everywhere, especially nested ones. It's a good way to shorten code, and you'll free up hard drive space. It'll also entertain your colleagues while they're debugging it.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;C++ offers plenty of ways to avoid macros. I strongly recommend using them as sparingly as possible.&lt;/p&gt;

&lt;p&gt;Let me be clear: I'm not a zealot, and I'm not suggesting we abandon macros for idealistic reasons. For example, macros can be perfectly acceptable when you need to generate repetitive code. I also have no problem with macros in legacy applications built with MFC. There's no point in fighting 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="no"&gt;BEGIN_MESSAGE_MAP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;efcDialog&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;EFCDIALOG_PARENT&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;//{{AFX_MSG_MAP(efcDialog)&lt;/span&gt;
  &lt;span class="no"&gt;ON_WM_CREATE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="no"&gt;ON_WM_DESTROY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;//}}AFX_MSG_MAP&lt;/span&gt;
&lt;span class="no"&gt;END_MESSAGE_MAP&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;Some macros are just part of the landscape, and that's fine. They really were designed to make programming easier.&lt;/p&gt;

&lt;p&gt;I'm talking about a different kind of macro: one that developers use to avoid creating a proper function or to shorten an existing one. Let's look at a few reasons to avoid such macros.&lt;/p&gt;

&lt;h3&gt;
  
  
  First: code that uses macros often attracts bugs
&lt;/h3&gt;

&lt;p&gt;I can't explain why this happens from a philosophical standpoint, but it does. Furthermore, bugs involving macros are often difficult to spot during &lt;a href="https://pvs-studio.com/en/blog/terms/0073/" rel="noopener noreferrer"&gt;code reviews&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I've described several such cases in my articles. For example, in the Midnight Commander project, I found that the &lt;code&gt;isspace&lt;/code&gt; function had been replaced with the following macro:&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="cp"&gt;#define isspace(c) ((c)==' ' || (c) == '\t')&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This macro caused a loop condition to behave incorrectly. The developers who used &lt;code&gt;isspace&lt;/code&gt; assumed they were calling the actual function that treats as whitespace spaces, tabs, and other characters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;form feed (0x0c, '\f');&lt;/li&gt;
&lt;li&gt;line feed (LF, 0x0a, '\n');&lt;/li&gt;
&lt;li&gt;carriage return (CR, 0x0d, '\r');&lt;/li&gt;
&lt;li&gt;vertical tab (0x0b, '\v').&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What about this way of shortening &lt;code&gt;std::printf&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="cp"&gt;#define sprintf std::printf&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Somebody got carried away with macros in the StarEngine project to shorten function calls from the standard library:&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="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="cp"&gt;#define scout std::cout&lt;/span&gt;
&lt;span class="cp"&gt;#define sstringstream std::stringstream&lt;/span&gt;
&lt;span class="cp"&gt;#define sofstream std::ofstream&lt;/span&gt;
&lt;span class="cp"&gt;#define sifstream std::ifstream&lt;/span&gt;
&lt;span class="cp"&gt;#define sfstream std::fstream&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;Another developer walked right into this carefully laid trap. I think they intended to call the standard &lt;code&gt;sprintf&lt;/code&gt; function here to print to the buffer:&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="nf"&gt;sprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"%d"&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="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead, they called the &lt;code&gt;std::printf&lt;/code&gt; function. The icing on the cake is a format string (the first argument) that is an uninitialized buffer designed for writing, not for reading.&lt;/p&gt;

&lt;p&gt;One might argue that the programmers are to blame for these errors, not the macros. That's right. Of course, programmers are always responsible for their mistakes. Although...&lt;/p&gt;

&lt;p&gt;The key issue is that macros cause errors. So, macros should be used with extreme caution—or not at all. Now, let's look at a couple more examples to drive the point home.&lt;/p&gt;

&lt;p&gt;The ATL library offers string conversion macros, including &lt;code&gt;A2W&lt;/code&gt;, &lt;code&gt;T2W&lt;/code&gt;, etc. However, few developers realize how dangerous it is to use them in loops. These macros call the &lt;code&gt;alloca&lt;/code&gt; function, meaning that each loop iteration allocates more memory on the stack. Although the program may appear to work correctly, it can run out of stack space at any moment if it starts processing longer strings or the number of loop iterations increases.&lt;/p&gt;

&lt;p&gt;Macros like &lt;code&gt;A2W&lt;/code&gt; look innocent only on the surface. They look like functions but actually have side effects that are difficult to detect.&lt;/p&gt;

&lt;p&gt;Also, I can't ignore such attempts to shorten code using macros:&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;initialize_sanitizer_builtins&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&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;span class="cp"&gt;#define DEF_SANITIZER_BUILTIN(ENUM, NAME, TYPE, ATTRS) \&lt;/span&gt;
  &lt;span class="n"&gt;decl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;add_builtin_function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"__builtin_"&lt;/span&gt; &lt;span class="no"&gt;NAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;TYPE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;ENUM&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; \
             &lt;span class="no"&gt;BUILT_IN_NORMAL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;NAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;NULL_TREE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  \
  &lt;span class="n"&gt;set_call_expr_flags&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;decl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;ATTRS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          \
  &lt;span class="n"&gt;set_builtin_decl&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;ENUM&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;decl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="cp"&gt;#include "sanitizer.def"&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;flag_sanitize&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="no"&gt;SANITIZE_OBJECT_SIZE&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="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;builtin_decl_implicit_p&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;BUILT_IN_OBJECT_SIZE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="no"&gt;DEF_SANITIZER_BUILTIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;BUILT_IN_OBJECT_SIZE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"object_size"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="no"&gt;BT_FN_SIZE_CONST_PTR_INT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="no"&gt;ATTR_PURE_NOTHROW_LEAF_LIST&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;Only the first line in the macro belongs to the &lt;code&gt;if&lt;/code&gt; statement. The remaining lines execute regardless of the condition. You could call this a C-style bug since I found it in the GCC compiler using PVS-Studio. GCC is written primarily in C, a language where it's hard to get by without macros. However, a proper function would have worked just as well here.&lt;/p&gt;

&lt;h3&gt;
  
  
  Second: macros make code harder to read
&lt;/h3&gt;

&lt;p&gt;If you've ever worked on a project full of macros that were built from other macros, then you know how difficult it can be to understand such code. If you haven't, take my word for it: it's not a pleasant experience. The GCC compiler I mentioned earlier is one example of code that's particularly hard to follow.&lt;/p&gt;

&lt;p&gt;According to a popular story, Apple invested in LLVM as an alternative to GCC because GCC's code had become too complex due to its heavy reliance on macros. I can't remember where I read this, though, so I can't provide any sources.&lt;/p&gt;

&lt;h3&gt;
  
  
  Third: writing macros is difficult
&lt;/h3&gt;

&lt;p&gt;It's easy to write a bad macro. I often encounter bugs in code that result from poorly written macros. However, writing a good, reliable macro is often harder than implementing an equivalent function.&lt;/p&gt;

&lt;p&gt;Unlike a function, a macro can't be treated as a standalone entity, which is why it's different. A developer has to consider it in the context of every possible use case right from the start. Otherwise, they may easily encounter an issue 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="cp"&gt;#define MIN(X, Y) (((X) &amp;lt; (Y)) ? (X) : (Y))&lt;/span&gt;
&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;MIN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ArrayA&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="nc"&gt;ArrayB&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&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="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;i&lt;/code&gt; and &lt;code&gt;j&lt;/code&gt; variables will be incremented not once, but twice.&lt;/p&gt;

&lt;p&gt;Of course, workarounds for such cases have long been invented, so the macro can be implemented safely:&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="cp"&gt;#define MAX(a,b) \&lt;/span&gt;
   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;__typeof__&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;_a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; \
      &lt;span class="n"&gt;__typeof__&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;_b&lt;/span&gt; &lt;span class="o"&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="p"&gt;;&lt;/span&gt; \
     &lt;span class="n"&gt;_a&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_b&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;_a&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="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;However, here's the question: do we really need all of this in C++? No, C++ has templates and other ways to write efficient code. So, why do I keep seeing such macros in C++ programs?&lt;/p&gt;

&lt;h3&gt;
  
  
  Fourth: debugging becomes more difficult
&lt;/h3&gt;

&lt;p&gt;Some people believe debugging is for the weak. You should write bug-free code from the start instead of hunting for bugs with a debugger. Blah, blah, blah... That's certainly an interesting topic to discuss, but in real projects, debugging is useful and helps developers find bugs. Macros complicate the process and definitely slow down bug hunting.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fifth: false positives of static code analyzers
&lt;/h3&gt;

&lt;p&gt;Due to the way they work, many macros generate numerous false positives in static analyzers. I can confidently say that most false positives in C and C++ code analysis stem from macros. Analyzers simply can't distinguish between clever, correct code and buggy one.&lt;/p&gt;

&lt;h3&gt;
  
  
  What can we do?
&lt;/h3&gt;

&lt;p&gt;Let's avoid using macros in C++ programs unless absolutely necessary.&lt;/p&gt;

&lt;p&gt;C++ offers a rich set of tools, including function templates, automatic type deduction (&lt;code&gt;auto&lt;/code&gt;, &lt;code&gt;decltype&lt;/code&gt;), and constexpr functions.&lt;/p&gt;

&lt;p&gt;In almost every case, you can write a function instead of a macro. Developers often avoid doing so out of sheer laziness. Such laziness does more harm than good, and we should fight it. The extra time you spend writing a proper function will pay off many times over. Your code will be easier to read and maintain. You'll also be less likely to make mistakes, and compilers and static analyzers will produce fewer false positives.&lt;/p&gt;

&lt;p&gt;Someone might argue that code that uses a function is less efficient. That's just another excuse.&lt;/p&gt;

&lt;p&gt;Modern compilers do an excellent job of inlining code, even if you don't explicitly use the &lt;code&gt;inline&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;Evaluating expressions at compile time doesn't require macros as well. In fact, they can cause more problems than they solve. Using &lt;code&gt;constexpr&lt;/code&gt; is a much better and safer way to achieve the same result.&lt;/p&gt;

&lt;p&gt;Let me show you an example. My colleague found this classic macro error in the FreeBSD kernel:&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="cp"&gt;#define ICB2400_VPOPT_WRITE_SIZE 20&lt;/span&gt;

&lt;span class="cp"&gt;#define  ICB2400_VPINFO_PORT_OFF(chan) \&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;ICB2400_VPINFO_OFF&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;                \
   &lt;span class="k"&gt;sizeof&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isp_icb_2400_vpinfo_t&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;chan&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="no"&gt;ICB2400_VPOPT_WRITE_SIZE&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="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
&lt;span class="nf"&gt;isp_fibre_init_2400&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ispsoftc_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;isp&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;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;ISP_CAP_VP0&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;off&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="no"&gt;ICB2400_VPINFO_PORT_OFF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chan&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="n"&gt;off&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="no"&gt;ICB2400_VPINFO_PORT_OFF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chan&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="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="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 &lt;code&gt;chan&lt;/code&gt; argument is used in the macro without being wrapped in parentheses. As a result, the &lt;code&gt;ICB2400_VPOPT_WRITE_SIZE&lt;/code&gt; constant is multiplied not by the expression &lt;code&gt;(chan - 1)&lt;/code&gt;, but only by one.&lt;/p&gt;

&lt;p&gt;Here's the expanded macro:&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;off&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;ICB2400_VPINFO_OFF&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isp_icb_2400_vpinfo_t&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;chan&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="no"&gt;ICB2400_VPOPT_WRITE_SIZE&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 error wouldn't have occurred if a regular function had been written instead of a macro:&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;size_t&lt;/span&gt; &lt;span class="no"&gt;ICB2400_VPINFO_PORT_OFF&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;chan&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="no"&gt;ICB2400_VPINFO_OFF&lt;/span&gt;
         &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isp_icb_2400_vpinfo_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
         &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;chan&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="no"&gt;ICB2400_VPOPT_WRITE_SIZE&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;Modern C and C++ compilers will most likely inline the function automatically, making the code just as efficient as its macro-based equivalent.&lt;/p&gt;

&lt;p&gt;At the same time, the code becomes easier to read and no longer suffers from the issue described earlier.&lt;/p&gt;

&lt;p&gt;If you know that the input value is always a constant, you can add &lt;code&gt;constexpr&lt;/code&gt; to ensure the compiler performs all calculations at compile time. The FreeBSD kernel is written in C, but let's assume we're working with C++ and &lt;code&gt;chan&lt;/code&gt; is a constant. In that case, it's best to declare the &lt;code&gt;ICB2400_VPINFO_PORT_OFF&lt;/code&gt; function as follows:&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="kd"&gt;constexpr&lt;/span&gt; &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="no"&gt;ICB2400_VPINFO_PORT_OFF&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;chan&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="no"&gt;ICB2400_VPINFO_OFF&lt;/span&gt;
         &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isp_icb_2400_vpinfo_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
         &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;chan&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="no"&gt;ICB2400_VPOPT_WRITE_SIZE&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;I hope I've managed to convince you. Wishing you as few macros as possible in your code!&lt;/p&gt;

&lt;h2&gt;
  
  
  Terrible tip N7. Disable warnings
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Disable compiler warnings. They distract you from your work, making it more difficult to write concise code.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Developers know that compiler warnings are their friends. They help catch bugs at compile time. It's much easier and faster to fix an error based on a compiler warning than to debug code that doesn't work.&lt;/p&gt;

&lt;p&gt;However, I've encountered a situation where some components in a large project were compiled with all warnings disabled.&lt;/p&gt;

&lt;p&gt;After writing some new code and running the application, I saw that it wasn't behaving as expected. When I went back over my code, I spotted an error and quickly fixed it. But I was surprised that the compiler hadn't issued a warning. It was a serious error, similar to using an uninitialized variable. I knew for sure that the compiler should have issued a warning about code like that, but it didn't.&lt;/p&gt;

&lt;p&gt;A brief investigation revealed that warnings had been disabled entirely in the DLL module being compiled and in several others. Then, I brought in some senior colleagues to investigate further what had happened.&lt;/p&gt;

&lt;p&gt;For several years, these modules had been compiled with warnings disabled. At some point, one of the developers was assigned to migrate the build to a more recent compiler version. And that's exactly what they did. The updated compiler began issuing new warnings, especially in the legacy code these modules use.&lt;/p&gt;

&lt;p&gt;The reasons behind the developer's actions remain unclear, but it appears they simply disabled warnings in some modules. Maybe they planned to do this temporarily so that warnings wouldn't interfere with fixing compilation errors, but then they forgot to enable them. Unfortunately, we couldn't determine what exactly happened because the developer had already left the company.&lt;/p&gt;

&lt;p&gt;After re-enabling warnings, I spent some time refactoring the code so the compiler would stop issuing them. It wasn't too demanding, though. The whole job took just one day. While working through the warnings, I also fixed several other bugs that had gone unnoticed until then. Learn to appreciate warnings from the compiler and static analyzer!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A useful tip&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Try to keep your project completely free of compiler warnings. Otherwise, you'll see the &lt;a href="https://en.wikipedia.org/wiki/Broken_windows_theory" rel="noopener noreferrer"&gt;broken windows theory&lt;/a&gt; in action. If your code always produces 10 warnings when compiled, an 11th one won't seem like a big deal. Even if you don't write code that triggers warnings, your colleagues might. If everyone treats this as acceptable, the situation quickly spirals out of control. The more warnings the compiler issues, the less attention people pay to new ones.&lt;/p&gt;

&lt;p&gt;Moreover, warnings will gradually lose their significance. If you're used to the compiler constantly issuing warnings, you might miss a new one pointing to a real error in your latest code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;-Werror&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here's another useful tip: set the compiler to treat warnings as errors. This creates a zero-tolerance policy for warnings across your team. You will either fix warnings or explicitly suppress them. Otherwise, the code simply won't compile. This approach can do wonders for code quality.&lt;/p&gt;

&lt;p&gt;However, there's a downside: whenever you update the compiler or any of your dependencies, you'll have to spend extra time fixing warnings that have become compilation errors.&lt;/p&gt;

&lt;p&gt;A compiler update may introduce new diagnostic rules or improve existing ones. As a result, your code may stop compiling, forcing you to refactor it immediately. The same applies to libraries, which may alter their interfaces, macros, and other components.&lt;/p&gt;

&lt;p&gt;Is this a reason to avoid &lt;code&gt;-Werror&lt;/code&gt;? No, but it's best to be prepared for these potential issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Terrible tip N8. The shorter the variable name, the better
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Use one or two letters to name variables. This way you'll fit a more complex expression on one line on the screen.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This approach does allow you to write shorter code. But it'll be just as short as it's incomprehensible when you come back to it later. Without meaningful variable names, such code becomes write-only. You can write it and even debug it right away, while you still remember what each variable stands for, but once some time has passed, making sense of that code will be extremely difficult.&lt;/p&gt;

&lt;p&gt;Another way to ruin code is to use abbreviations instead of meaningful variable names. For example, writing &lt;code&gt;AC&lt;/code&gt; instead of &lt;code&gt;ArrayCapacity&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the first case, it's clear that we're talking about capacity, or the amount of memory reserved for a container. In the second, we have to guess what the mysterious &lt;code&gt;AC&lt;/code&gt; stands for.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Should you always avoid short names? No.&lt;/strong&gt; Use common sense. It's perfectly fine to use names like &lt;code&gt;i&lt;/code&gt;, &lt;code&gt;j&lt;/code&gt;, and &lt;code&gt;k&lt;/code&gt; for loop counters. This is a well-established convention, so any programmer can easily understand code that uses these names.&lt;/p&gt;

&lt;p&gt;Abbreviations can also make sense in some cases, such as code that implements numerical methods, simulates processes, and so on. Such code simply performs calculations based on specific formulas described in comments or documentation. If a variable in a formula is called &lt;code&gt;SC0&lt;/code&gt;, it makes sense to use the same name in your code.&lt;/p&gt;

&lt;p&gt;For example, take a look at variable declarations in the COVID-19 CovidSim Model 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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;             &lt;span class="cm"&gt;/**&amp;lt; number of people in cell */&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;S&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;L&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;I&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;R&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;D&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="cm"&gt;/**&amp;lt; S, L, I, R, D are numbers of Susceptible,
                    Latently infected, Infectious,
                    Recovered and Dead people in cell */&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These variable names are valid. Their meanings are explained in the comments. Such naming conventions enable compact notation of formulas:&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="nc"&gt;Cells&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="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;S&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cells&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="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;Cells&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="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;L&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cells&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="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;I&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cells&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="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;R&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cells&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="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cumTC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cells&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="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;D&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="nc"&gt;Cells&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="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;infected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cells&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="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;latent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cells&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="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;susceptible&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;Cells&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="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;S&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'm not saying this is a good approach or coding style, but sometimes short names are the most practical choice. Think carefully about every recommendation, rule, and methodology. Understand when it's worth making an exception and when it isn't.&lt;/p&gt;

&lt;p&gt;"Code Complete" is a useful book that offers insights into naming variables, classes, and functions. I highly recommend reading the relevant chapter, as well as the entire book.&lt;/p&gt;

&lt;h2&gt;
  
  
  Terrible tip N9. Magic numbers
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Use strange numbers. This way, your program code will look smarter and more impressive. Doesn't this code line look hardcore: qw = ty / 65 - 29 * s;?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Numbers with no obvious purpose in code are called magic numbers. Using them is considered poor programming practice because it makes the code difficult to understand.&lt;/p&gt;

&lt;p&gt;A magic number has no meaning on its own and can cause confusion when it appears in code without any context or an explanatory comment. Changing it to another number, even a similar one, can have unpredictable consequences. Magic numbers decrease code portability across platforms and increase the overall cost of project maintenance and development.&lt;/p&gt;

&lt;p&gt;Using named constants and enums instead of raw numbers is better. However, it doesn't mean that every constant needs a name. Firstly, some constants, such as &lt;code&gt;0&lt;/code&gt; or &lt;code&gt;1&lt;/code&gt;, have an obvious purpose. Secondly, naming every numeric constant in programs that perform mathematical calculations may actually make them harder to read. In such cases, comments explaining the formulas are a better choice.&lt;/p&gt;

&lt;p&gt;Unfortunately, it's impossible to cover all the techniques for writing clean, readable code in a single chapter. So, I once again encourage you to read "Code Complete", the book I mentioned in the previous chapter.&lt;/p&gt;

&lt;p&gt;Sometimes, it's easy to guess what a particular number in the code represents. In that case, you wouldn't really call it a magic number. However, these numbers can still cause problems. Let's look at a few examples.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;first example&lt;/strong&gt; is from a game developers' article. I read it about ten years ago, so I don't remember the title or who posted it. The article described how a company developed a custom static analyzer for in-house use that could detect issues specific to game code. One of the diagnostic rules looked for constants such as 1024, 1600, and so on. These numbers represent screen resolutions in pixels. A developer might quickly test something by using these values, then forget to replace them with calls to the appropriate functions.&lt;/p&gt;

&lt;p&gt;The issue with these numbers isn't that they're difficult to identify. A developer will quickly realize that a number is related to the number of pixels on the screen. The real issue is that the defect is visible only during testing when different screen resolutions are used.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;second example&lt;/strong&gt; is pi. One day, PVS-Studio static analyzer prompted us to take a closer look at this code in the Bullet Physics SDK:&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;float&lt;/span&gt; &lt;span class="n"&gt;yScale&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;1.0&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nf"&gt;tan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;3.141592538&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;180.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;fov&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It didn't like the number &lt;code&gt;3.141592538&lt;/code&gt;, which is the one and only mathematical constant Pi. What's wrong with it? Does the analyzer report every number?&lt;/p&gt;

&lt;p&gt;No. Here's the catch: this number just looks like Pi. In reality, it's not Pi at all. The number is incorrect.&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="nc"&gt;This&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;what&lt;/span&gt;&lt;span class="s1"&gt;'s written in the code: 3.141592 53 8
This is the real Pi: 3.14159265358
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;6&lt;/code&gt; and &lt;code&gt;5&lt;/code&gt; are missing. Sure, it's not a big deal. This error has little effect on calculation accuracy. However, it would be better if the code author didn't try to show off their knowledge by writing Pi from memory, since the &lt;code&gt;M_PI&lt;/code&gt; named constant already exists.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;third example&lt;/strong&gt; is type sizes. In my articles, I've often described the errors that occur when porting applications, for example, from 32-bit to 64-bit architecture. When it comes to magic numbers, we're talking about errors in buffer size calculations or incorrect bitwise 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="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;ARRAY_SIZE&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;memset&lt;/span&gt;&lt;span class="p"&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="no"&gt;ARRAY_SIZE&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;4&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 classic error: a programmer assumed that &lt;a href="https://pvs-studio.com/en/blog/terms/0044/" rel="noopener noreferrer"&gt;&lt;code&gt;size_t&lt;/code&gt;&lt;/a&gt; was 4 bytes. That was true in a 32-bit program, but in a 64-bit one, &lt;code&gt;size_t&lt;/code&gt; is 8 bytes. As a result, only half the buffer is filled with zeroes.&lt;/p&gt;

&lt;p&gt;We'll continue discussing data types in the next chapter, but for now, here's another example of a bit-related error. I encountered the following code once:&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;// constant '1111..110000'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="no"&gt;M&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xFFFFFFF0&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Judging by the comment, the programmer expects the &lt;code&gt;M&lt;/code&gt; constant to have all bits set to one, except for the four least significant bits. However, when building a 64-bit application, the result will differ from what they expect.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;0xFFFFFFF0u&lt;/code&gt; constant has the &lt;code&gt;unsigned int&lt;/code&gt; type. When expanded to &lt;code&gt;size_t&lt;/code&gt;, which is an eight-byte unsigned type, the most significant bits are filled with zeros.&lt;/p&gt;

&lt;p&gt;In other words, the constant has the following binary representation:&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="mi"&gt;00000000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;00000000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;00000000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;00000000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;11111111&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;11111111&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;11111111&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;11110000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, writing the fixed code still requires using some constant:&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;// constant '1111..110000'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="no"&gt;M&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="k"&gt;static_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&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="mh"&gt;0xF&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;In any case, forewarned is forearmed. Be careful with code that contains explicit numeric constants.&lt;/p&gt;

&lt;p&gt;P.S. This code can actually be shortened, but it isn't necessary:&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="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="no"&gt;M&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="mh"&gt;0xF&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;0xF&lt;/code&gt; constant is of the &lt;code&gt;int&lt;/code&gt; type. So, after bit inversion, the &lt;code&gt;0xFFFFFFF0&lt;/code&gt; value will become signed. When converted to &lt;code&gt;size_t&lt;/code&gt;, the value first expanded to the &lt;code&gt;0xFFFFFFFFFFFFFFF0&lt;/code&gt; signed value of the &lt;a href="https://pvs-studio.com/en/blog/terms/0041/" rel="noopener noreferrer"&gt;&lt;code&gt;ptrdiff_t&lt;/code&gt;&lt;/a&gt; type. Only then does it become the required unsigned value, &lt;code&gt;0xFFFFFFFFFFFFFFF0u&lt;/code&gt;. The approach involves so many unobvious factors that it's better to avoid it. I'd go with the first option: it's longer, but much more straightforward.&lt;/p&gt;

&lt;h2&gt;
  
  
  To be continued
&lt;/h2&gt;

&lt;p&gt;In upcoming posts, you'll find more terrible tips for C++ programmers. Make sure you subscribe to &lt;a href="https://pvs-studio.com/en/subscribe/" rel="noopener noreferrer"&gt;our blog newsletter&lt;/a&gt; to stay up to date with all our latest content!&lt;/p&gt;

&lt;p&gt;Follow Andrey Karpov on &lt;a href="https://uz.linkedin.com/in/andrey-karpov-pvs-studio" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; and &lt;a href="https://x.com/Code_Analysis" rel="noopener noreferrer"&gt;X&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>programming</category>
      <category>softwaredevelopment</category>
      <category>learning</category>
    </item>
    <item>
      <title>How PVS-Studio for Java scored 77% on the OWASP Benchmark</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Fri, 27 Nov 2026 13:44:12 +0000</pubDate>
      <link>https://dev.to/pvsdev/how-pvs-studio-for-java-scored-77-on-the-owasp-benchmark-4jko</link>
      <guid>https://dev.to/pvsdev/how-pvs-studio-for-java-scored-77-on-the-owasp-benchmark-4jko</guid>
      <description>&lt;p&gt;How well does PVS-Studio for Java detect security defects? We put it to the test using the OWASP Benchmark, an independent test suite designed to evaluate SAST tools. Read on to see how PVS-Studio performed, what the results revealed, and which enhancements we made to the analyzer along the way.&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%2Fykm01eicypqtgv4oqp13.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%2Fykm01eicypqtgv4oqp13.png" alt="1430_owasp_benchmark/image1.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the OWASP Benchmark?
&lt;/h2&gt;

&lt;p&gt;OWASP is a community focused on application security. One of its projects, &lt;a href="https://github.com/OWASP-Benchmark/BenchmarkJava" rel="noopener noreferrer"&gt;OWASP Benchmark&lt;/a&gt;, is an independent test suite for evaluating how effective vulnerability detection tools are, SAST analyzers among them.&lt;/p&gt;

&lt;p&gt;A test like this is particularly useful. We have &lt;a href="https://pvs-studio.com/en/blog/posts/java/1287/" rel="noopener noreferrer"&gt;claimed&lt;/a&gt; that PVS-Studio detects vulnerabilities in 9 of the 10 OWASP Top Ten categories (another OWASP project that lists the 10 most common types of security risks to web applications). However, it's hard for an outside observer to verify this claim without running tests themselves.&lt;/p&gt;

&lt;p&gt;With Java, we got lucky. Unlike many other programming languages, Java has its own independent OWASP benchmark in the form of a dedicated application, complete with everything needed for testing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A set of Java files containing known vulnerabilities (SQLi, XSS, Path Traversal, the use of weak encryption algorithms, and others).&lt;/li&gt;
&lt;li&gt;A matching set of files with negative cases to check that the analyzer reports issues only where it should and doesn't produce false positives.&lt;/li&gt;
&lt;li&gt;A utility that automatically evaluates a generated SARIF report and builds a coverage table showing the results (TP, FP, overall score).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Our support team has been asked about our benchmark results from time to time, so it was only a matter of time before we ran the tests ourselves.&lt;/p&gt;

&lt;p&gt;Now we have the results: PVS-Studio 8.02 performed strongly, outperforming open-source tools and is close to the performance of other commercial analyzers.&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%2Fr2hcw3tirfhpeh9vvkm8.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%2Fr2hcw3tirfhpeh9vvkm8.png" alt="1430_owasp_benchmark/image2.png" width="800" height="396"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Read on to learn what it took to achieve these results and how we improved the analyzer along the way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting started
&lt;/h2&gt;

&lt;p&gt;Our first benchmark run did not go well. But the numbers don't tell the whole story. Let's start by looking at PVS-Studio 8.00's score:&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%2Fvzp6xjps8ln6umm43t1a.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%2Fvzp6xjps8ln6umm43t1a.png" alt="1430_owasp_benchmark/image3.png" width="800" height="396"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For comparison, here is the result for the free &lt;a href="https://github.com/find-sec-bugs/find-sec-bugs" rel="noopener noreferrer"&gt;FBwFindSecBugs&lt;/a&gt; v1.4.6:&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%2Fx7okvedy7yy82sxptrzv.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%2Fx7okvedy7yy82sxptrzv.png" alt="1430_owasp_benchmark/image4.png" width="800" height="396"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The comparison was disappointing, to put it mildly. The analyzer engine was only part of the problem (more on that below). A few smaller issues also dragged down the score significantly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;OWASP uses a configuration for an older version of Java, which doesn't work particularly well with the JDT compiler that underlies our analyzer.&lt;/li&gt;
&lt;li&gt;Some methods and classes lacked annotations that help the analyzer understand the code it's analyzing. In this case, the annotations identify sources of tainted data. Unfortunately, we sometimes miss parts of the standard library or popular third-party libraries when adding annotations. Here, the outdated API made things even worse. For cases like this, among other things, we provide a way to add &lt;a href="https://pvs-studio.com/en/blog/posts/java/1274/" rel="noopener noreferrer"&gt;user annotations&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;The analyzer's default settings were too conservative, a result of our approach to general analysis. It often prioritizes minimizing false positives. With SAST, however, the priorities are different, as our C++ colleagues have also noted in their &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1302/" rel="noopener noreferrer"&gt;article&lt;/a&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The analysis engine really did need several improvements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dead code elimination. The analyzer produced many false positives on code that could never be executed.&lt;/li&gt;
&lt;li&gt;More precise collection analysis. Previously, once external data entered a collection, we treated the entire collection as tainted. We needed to take this a step further and track taint at the level of individual elements.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We also identified several security issues that weren't yet covered by the analyzer's diagnostic rules. We'll cover those and the improvements to the analysis engine in the next sections.&lt;/p&gt;

&lt;h2&gt;
  
  
  Analyzer enhancements
&lt;/h2&gt;

&lt;p&gt;We were able to close these smaller gaps fairly quickly. The entire process took just a couple of weeks. We added an extra configuration setting and adjusted the reporting threshold to favor reporting a warning unless the analyzer could rule out a vulnerability. &lt;/p&gt;

&lt;h3&gt;
  
  
  New diagnostic rules
&lt;/h3&gt;

&lt;p&gt;There were a few rules the analyzer didn't have yet, so we added them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://pvs-studio.com/en/docs/warnings/v5341/" rel="noopener noreferrer"&gt;V5341&lt;/a&gt;. OWASP. A cookie was created without enabling the 'Secure' attribute. This may lead to the exposure of sensitive data.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://pvs-studio.com/en/docs/warnings/v5342/" rel="noopener noreferrer"&gt;V5342&lt;/a&gt;. OWASP. Potentially tainted data is stored in an object that can be used as a source of trusted data.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://pvs-studio.com/en/docs/warnings/v5343/" rel="noopener noreferrer"&gt;V5343&lt;/a&gt;. OWASP. A cryptographically weak random number is used for sensitive data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They map to three OWASP Top Ten 2025 categories: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://owasp.org/Top10/2025/A02_2025-Security_Misconfiguration/" rel="noopener noreferrer"&gt;A02:2025 Security Misconfiguration.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://owasp.org/Top10/2025/A06_2025-Insecure_Design/" rel="noopener noreferrer"&gt;A06:2025 Insecure Design&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://owasp.org/Top10/2025/A04_2025-Cryptographic_Failures/" rel="noopener noreferrer"&gt;A04:2025 Cryptographic Failures&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This also helped us identify areas where we could expand the analyzer's coverage of the OWASP ASVS standard and detect more vulnerabilities covered by the OWASP Top Ten 2025.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dead code elimination
&lt;/h3&gt;

&lt;p&gt;Next, we turned to improvements to the analysis engine. Let's start with a 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;var&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// SQLi&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Execution will never reach &lt;code&gt;// SQLi&lt;/code&gt;, because &lt;code&gt;b&lt;/code&gt; equals &lt;code&gt;10&lt;/code&gt;, which is not greater than &lt;code&gt;10&lt;/code&gt;. The analyzer used to issue an SQL injection warning here. &lt;/p&gt;

&lt;p&gt;The OWASP test suite also showed us a different kind of case:&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;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;106&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;bar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;"This_should_always_happen"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;param&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;sql&lt;/span&gt; 
  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"SELECT * from USERS where USERNAME='foo' and PASSWORD='"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;bar&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"'"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// sqli&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://github.com/OWASP-Benchmark/BenchmarkJava/blob/10dfcc54ad02dcfcb358b0ba4a0fef8c919a6d17/src/main/java/org/owasp/benchmark/testcode/BenchmarkTest00105.java#L69" rel="noopener noreferrer"&gt;BenchmarkTest00105.java:69&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The example is still contrived, but it challenged our assumption that reporting a vulnerability was justified simply because potentially dangerous code was present. We'd encountered this kind of test more than once, so we decided it was time to address the issue.&lt;/p&gt;

&lt;p&gt;Fortunately, the technique we needed had been around for a long time: conditional constant propagation. In simple terms, before analyzing the code, we need to calculate and keep track of values that can be determined at compile time. In our first example, these include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;a&lt;/code&gt;, which equals 5;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;b&lt;/code&gt;, which equals 10;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;b &amp;gt; 10&lt;/code&gt;, which evaluates to &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's called propagation because we don't just calculate &lt;code&gt;2 * 5&lt;/code&gt;. We also need to remember the values we've already computed so they can be used in subsequent calculations. For example, we need to retain the value of &lt;code&gt;a&lt;/code&gt; to calculate &lt;code&gt;b&lt;/code&gt;. And it's conditional because we evaluate conditions as well.&lt;/p&gt;

&lt;p&gt;We calculate all these values using the familiar &lt;a href="https://en.wikipedia.org/w/index.php?title=Reaching_definition&amp;amp;oldid=1372291348#Worklist_algorithm" rel="noopener noreferrer"&gt;worklist&lt;/a&gt; algorithm, which iteratively updates the values in a method until they stop changing (that is, until a fixed point is reached).&lt;/p&gt;

&lt;p&gt;Once we have these values, we can simply remove unreachable branches from the control flow graph (I covered CFG in an earlier &lt;a href="https://pvs-studio.com/en/blog/posts/java/1198/#IDA377AAB4A5" rel="noopener noreferrer"&gt;article&lt;/a&gt; on SAST, and recently &lt;a href="https://pvs-studio.com/en/blog/posts/js/1418/" rel="noopener noreferrer"&gt;wrote&lt;/a&gt; adding it to our JavaScript/TypeScript analyzer). The analyzer can then proceed with its usual analysis, without reporting vulnerabilities in code that can never execute.&lt;/p&gt;

&lt;p&gt;These improvements have been available since PVS-Studio 8.01. Here's how the results looked at the time of its release:&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%2Fkowy7zc3gksi3e5p56bc.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%2Fkowy7zc3gksi3e5p56bc.png" alt="1430_owasp_benchmark/image5.png" width="800" height="388"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see, the score improved substantially, with the overall Score surpassing that of FindSecBugs mentioned above. We were still slightly behind some of our competitors, though, so there was more work to do.&lt;/p&gt;

&lt;p&gt;At that point, we still had a few individual cases to address and some engine bugs to fix. Since we hadn't previously calculated values across the control flow graph, we hadn't noticed some inaccuracies in its representation, such as the incorrect handling of ternary operators. Also, resolving the most challenging issue took quite a bit longer. That's what we'll look at next.&lt;/p&gt;

&lt;h3&gt;
  
  
  Working with collections
&lt;/h3&gt;

&lt;p&gt;Let's look at one of the benchmark tests:&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="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;bar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"safe!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;map21657&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;java&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;util&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;&amp;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;map21657&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"keyA-21657"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"a_Value"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// put some stuff in the collection&lt;/span&gt;
&lt;span class="n"&gt;map21657&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"keyB-21657"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;param&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// put it in a collection&lt;/span&gt;
&lt;span class="n"&gt;map21657&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"keyC"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"another_Value"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// put some stuff in the collection&lt;/span&gt;
&lt;span class="n"&gt;bar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;map21657&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"keyB-21657"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// get it back out&lt;/span&gt;
&lt;span class="n"&gt;bar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;map21657&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"keyA-21657"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// get safe value back out&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;sql&lt;/span&gt; 
  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"INSERT INTO users (username, password) VALUES ('foo','"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;bar&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"')"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// sqli&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://github.com/OWASP-Benchmark/BenchmarkJava/blob/10dfcc54ad02dcfcb358b0ba4a0fef8c919a6d17/src/main/java/org/owasp/benchmark/testcode/BenchmarkTest00113.java#L66" rel="noopener noreferrer"&gt;BenchmarkTest00113.java:66&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Here, &lt;code&gt;param&lt;/code&gt; is tainted data received from a &lt;code&gt;cookie&lt;/code&gt;. It never makes it into the &lt;code&gt;sql&lt;/code&gt; string, though.&lt;/p&gt;

&lt;p&gt;To handle cases like this, we had to extend the engine with several new techniques and improve existing ones:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Heap abstraction. This is a fairly broad topic in static analysis. Previously, we could treat a collection as just another variable within a method. Now we needed to distinguish between its individual elements. To do that, we introduced a model of the relevant memory regions.&lt;/li&gt;
&lt;li&gt;k-limiting. Collections can contain other collections, which can contain objects, which can in turn contain more collections, and so on. Tracking all this state would be extremely expensive and, in some cases, impossible. A common way to address this problem is k-limiting: restricting the nesting depth to a fixed constant, k. In our case, we set this limit to four levels.&lt;/li&gt;
&lt;li&gt;Reverse postorder. In the &lt;a href="https://pvs-studio.com/en/blog/posts/java/1198/" rel="noopener noreferrer"&gt;article&lt;/a&gt; mentioned above, I explained that our taint analysis runs from the end of the control flow graph to the beginning. This meant we also had to learn to compute values in reverse order. The tricky part is that we start by assuming the collection may contain any value, even though we don't yet know its actual contents. The advantage is that this approach reduces the number of repeated computations since it's closely related to topological sorting.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Implementing these techniques took the most time. We had to deal with problems that surfaced along the way, too, fit everything into the existing engine, and work through the complexity of the approach itself. In the end, though, we made collection analysis more precise and eliminated a whole range of false positives.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final results
&lt;/h2&gt;

&lt;p&gt;Collection analysis and other minor fixes will ship in the PVS-Studio 8.02 release, due in a few weeks. Here are the latest results, once again:&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%2Fr2hcw3tirfhpeh9vvkm8.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%2Fr2hcw3tirfhpeh9vvkm8.png" alt="1430_owasp_benchmark/image2.png" width="800" height="396"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Over the course of two to three months, we managed to improve our score from 18% to 77%, achieving results comparable to those of established commercial SAST tools. This benchmark gave us an opportunity to put the analyzer to the test, identify its weaknesses, and improve its detection capabilities. And the results speak for themselves.&lt;/p&gt;

&lt;p&gt;You can always find the latest OWASP Benchmark coverage data on the corresponding &lt;a href="https://pvs-studio.com/en/owasp-benchmark/" rel="noopener noreferrer"&gt;page&lt;/a&gt; of our website. It provides more details about our testing methodology and explains how to verify the results yourself.&lt;/p&gt;

&lt;p&gt;That's all from me for now. If you'd like to read more about programming and static analysis, you can follow us or subscribe here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PVS-Studio on &lt;a href="https://x.com/pvs_studio" rel="noopener noreferrer"&gt;X&lt;/a&gt;;&lt;/li&gt;
&lt;li&gt;our monthly article &lt;a href="https://pvs-studio.com/en/subscribe/" rel="noopener noreferrer"&gt;digest&lt;/a&gt;;&lt;/li&gt;
&lt;li&gt;Author's personal &lt;a href="https://twitter.com/kvolokhovskii" rel="noopener noreferrer"&gt;blog&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>owasp</category>
      <category>security</category>
    </item>
    <item>
      <title>Let's check Scilab</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Wed, 25 Nov 2026 13:35:23 +0000</pubDate>
      <link>https://dev.to/pvsdev/lets-check-scilab-626</link>
      <guid>https://dev.to/pvsdev/lets-check-scilab-626</guid>
      <description>&lt;p&gt;What powerful tools do you know for complex mathematical computations? One example is the open-source project Scilab. We analyzed its source code to see what errors we could uncover. In this article, we'll show how static analysis can help detect them.&lt;/p&gt;

&lt;p&gt;What's our topic today?&lt;br&gt;
Scilab is an open-source, cross-platform project designed for scientific and engineering computing. It's widely used in manufacturing, automotive engineering, medicine, and other fields for solving complex mathematical problems. The project's wide range of uses requires the highest standards for the quality and reliability of its code.&lt;/p&gt;

&lt;p&gt;We used PVS-Studio static analyzer to check the Scilab source code. In this article, we highlighted the most interesting errors we found. Our goal is to show developers how static analysis can help make the projects even better.&lt;/p&gt;

&lt;p&gt;A few words about PVS-Studio. PVS-Studio is a static analyzer that has been helping developers detect errors in C, C++, C#, Java, Go, JavaScript, and TypeScript code for over 18 years. We also provide excellent technical support.&lt;/p&gt;

&lt;p&gt;Static analysis is a step in the development process that complements other quality assurance methods, such as testing and code reviews. It helps developers detect bugs and potential vulnerabilities early in the development process. This enables teams to devote more time to implementing new features and less time to fixing code issues.&lt;/p&gt;

&lt;p&gt;This article doesn't cover all the issues we detected. Instead, we've chosen the examples we found most intriguing. Try PVS-Studio for free to view the full report or check your own project.&lt;/p&gt;

&lt;p&gt;Note. We checked the project at the time of the 30a3695a commit using PVS-Studio 8.00.&lt;/p&gt;

&lt;p&gt;Errors&lt;br&gt;
Handling pointers&lt;br&gt;
Snippet 1&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning: V611 [CWE-762, CERT-MEM51-CPP, SEC-MEMORY] The memory was allocated using the 'operator new' but was released using the 'operator delete[]'. The 'delete ptr;' statement should be used instead. fromws_c.cpp 335&lt;/p&gt;

&lt;p&gt;SCICOS_BLOCKS_IMPEXP void fromws_c(scicos_block* block, int flag)&lt;br&gt;
{&lt;br&gt;
  // ....&lt;br&gt;
  fromwork_struct** work = (fromwork_struct*&lt;em&gt;) block-&amp;gt;work;&lt;br&gt;
  fromwork_struct&lt;/em&gt; ptr = nullptr;&lt;br&gt;
  // ....&lt;br&gt;
  *work = new fromwork_struct(); // &amp;lt;=&lt;br&gt;
  ptr = *work;                   // &amp;lt;=&lt;br&gt;
  // ....&lt;/p&gt;

&lt;p&gt;if (ptr_T[j] &amp;gt; ptr_T[j + 1])&lt;br&gt;
  {&lt;br&gt;
      Coserror(_("The time vector should be an increasing vector.\n"));&lt;br&gt;
      *work = nullptr;&lt;br&gt;
      delete[] ptr-&amp;gt;workt;&lt;br&gt;
      FREE(ptr-&amp;gt;work);&lt;br&gt;
      delete[] ptr;           // &amp;lt;=&lt;br&gt;
      return;&lt;br&gt;
  }&lt;br&gt;
  // ....&lt;br&gt;
}&lt;br&gt;
The issue is clear: the developers used an incorrect deallocation method. Memory for the element that ptr points to is allocated using new and deallocated using delete[]. What potential issues could arise from this?&lt;/p&gt;

&lt;p&gt;According to the C++ standard, if we allocate memory for a single object but free it using delete[], the program's behavior is undefined. In reality, delete[] expects metadata before the memory block that stores the number of elements allocated by new T[N]. If the metadata isn't there, the operator may read garbage. As a result, it may call the wrong number of destructors or attempt to free an incorrect address.&lt;/p&gt;

&lt;p&gt;If you still believe that undefined behavior is just a myth, this article might change your mind.&lt;/p&gt;

&lt;p&gt;To fix the code, we need to remove [] when clearing the ptr pointer:&lt;/p&gt;

&lt;p&gt;delete ptr;&lt;br&gt;
Snippet 2&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning: V595 [CWE-476, CERT-EXP12-C, SEC-NULL] The 'values' pointer was utilized before it was verified against nullptr. Check lines: 465, 466. sundials_hashmap.c 465&lt;/p&gt;

&lt;p&gt;SUNErrCode SUNHashMap_Values&lt;br&gt;
  (SUNHashMap map, void*** values, int64_t value_size)&lt;br&gt;
{&lt;br&gt;
  int count = 0;&lt;/p&gt;

&lt;p&gt;if (!map) { return SUN_ERR_ARG_CORRUPT; }&lt;/p&gt;

&lt;p&gt;&lt;em&gt;values = (void&lt;/em&gt;*)malloc(SUNHashMap_Capacity(map) * value_size);&lt;br&gt;
  if (!values) { return SUN_ERR_MALLOC_FAIL; }&lt;br&gt;
  // ....&lt;br&gt;
}&lt;br&gt;
Manual memory management can lead to various errors. Here, the values pointer is passed as a function parameter. We dereference it and check for NULL on the next line. If the pointer is null, dereferencing it will result in undefined behavior; so, the check below is meaningless. In reality, the compiler can simply remove the entire if (!values) block. It sees that the pointer is already in use, so it assumes it's valid and that the check is redundant.&lt;/p&gt;

&lt;p&gt;The developers might have wanted to check the result of the malloc function, which returns NULL if memory allocation fails. However, the typo triggers the wrong check, and dereferencing happens further down in the code:&lt;/p&gt;

&lt;p&gt;for (int64_t i = 0; i &amp;lt; SUNHashMap_Capacity(map); i++)&lt;br&gt;
{&lt;br&gt;
  SUNHashMapKeyValue kvp = &lt;br&gt;
    &lt;em&gt;SUNStlVector_SUNHashMapKeyValue_At(map-&amp;gt;buckets, i);&lt;br&gt;
  if (kvp) { (*values)[count++] = kvp-&amp;gt;value; }  // &amp;lt;=&lt;br&gt;
}&lt;br&gt;
The PVS-Studio warning: V522 [CWE-690, CERT-MEM52-CPP, SEC-NULL] There might be dereferencing of a potential null pointer '&lt;/em&gt; values'. Check lines: 472, 465. sundials_hashmap.c 472&lt;/p&gt;

&lt;p&gt;To fix the code, we'll add a check for the values pointer and verify that memory is allocated at *values:&lt;/p&gt;

&lt;p&gt;if (!map || !values) { return SUN_ERR_ARG_CORRUPT; }&lt;/p&gt;

&lt;p&gt;&lt;em&gt;values = (void&lt;/em&gt;*)malloc(SUNHashMap_Capacity(map) * value_size);&lt;br&gt;
if (!*values) { return SUN_ERR_MALLOC_FAIL; }&lt;br&gt;
Snippet 3&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning: V558 [CWE-562, CERT-DCL30-C, SEC-MEMORY] Function returns the reference to temporary local object: tmp. matrixiterator.hxx 527&lt;/p&gt;

&lt;p&gt;template struct Coords : Coords2DIterator&lt;br&gt;
{&lt;br&gt;
  // ....&lt;br&gt;
  Coords&amp;amp; operator++(int)&lt;br&gt;
  {&lt;br&gt;
    Coords tmp(*this);&lt;br&gt;
    ++(*this);&lt;br&gt;
    return tmp;&lt;br&gt;
  }&lt;br&gt;
  // ....&lt;br&gt;
}&lt;br&gt;
It looks like the developers copied the prefix increment implementation and used it for the postfix increment by simply adding a dummy parameter of the int type.&lt;/p&gt;

&lt;p&gt;That said, the best approach when implementing such operators is to use semantics similar to those of built-in types. A postfix increment stores the previous value, produces a side effect, and returns a prvalue with the previous value. Here, an lvalue reference to the tmp local object is returned. Using it in the caller function results in undefined behavior.&lt;/p&gt;

&lt;p&gt;To avoid this, we need to return the object by value:&lt;/p&gt;

&lt;p&gt;Coords operator++(int)&lt;br&gt;
{ /* .... */ }&lt;br&gt;
The code below contains an explicit specialization of this structure where the operator is implemented correctly.&lt;/p&gt;

&lt;p&gt;Snippet 4&lt;/p&gt;

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

&lt;p&gt;V773 [CWE-401, CERT-MEM31-C, CERT-MEM51-CPP, SEC-LEAKS] The function was exited without releasing the 'temp_real' pointer. A memory leak is possible. sci_fft.cpp 212&lt;/p&gt;

&lt;p&gt;V773 [CWE-401, CERT-MEM31-C, CERT-MEM51-CPP, SEC-LEAKS] The function was exited without releasing the 'temp_imaginary' pointer. A memory leak is possible. sci_fft.cpp 212&lt;/p&gt;

&lt;h1&gt;
  
  
  define MALLOC malloc
&lt;/h1&gt;

&lt;p&gt;// ....&lt;br&gt;
int fft_2dim(double *signal_real, double *signal_imaginary, int signal_rows,&lt;br&gt;
  int signal_cols, int inverse, int *buffer_data, int buffer_size)&lt;br&gt;
{&lt;br&gt;
  // ....&lt;br&gt;
  temp_real = (double *)MALLOC(signal_cols * sizeof(double));&lt;br&gt;
  temp_imaginary = (double *)MALLOC(signal_cols * sizeof(double));&lt;/p&gt;

&lt;p&gt;if (temp_real == NULL || temp_imaginary == NULL)&lt;br&gt;
  {&lt;br&gt;
      return 1;&lt;br&gt;
  }&lt;br&gt;
  // ....&lt;br&gt;
}&lt;br&gt;
Two memory blocks are allocated, followed by a check whether the operation was successful. If memory has been allocated for the first buffer but not for the second one, an early exit occurs, and the first buffer remains unallocated. This is a memory leak. We can avoid it by releasing both pointers before exiting.&lt;/p&gt;

&lt;p&gt;Here's the fixed code:&lt;/p&gt;

&lt;h1&gt;
  
  
  define FREE free
&lt;/h1&gt;

&lt;p&gt;// ....&lt;br&gt;
temp_real = (double *)MALLOC(signal_cols * sizeof(double));&lt;br&gt;
temp_imaginary = (double *)MALLOC(signal_cols * sizeof(double));&lt;br&gt;
if (temp_real == NULL || temp_imaginary == NULL)&lt;br&gt;
{&lt;br&gt;
  FREE(temp_real);&lt;br&gt;
  FREE(temp_imaginary);&lt;br&gt;
  return 1;&lt;br&gt;
}&lt;br&gt;
Strange code&lt;br&gt;
Snippet 5&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning: V1061 [CERT-DCL58-CPP] Extending the 'std' namespace may result in undefined behavior. sparseOp.hxx 39&lt;/p&gt;

&lt;p&gt;namespace std&lt;br&gt;
{&lt;br&gt;
/* std namespace does not implement complex ordering: &lt;br&gt;
   we use the lexicographical order   */&lt;br&gt;
typedef double (*cplxFun_t)(std::complex const&amp;amp;);&lt;/p&gt;

&lt;h1&gt;
  
  
  ifndef SCI_CPLX_ORDER_SEP21
&lt;/h1&gt;

&lt;p&gt;/* on abs() and arg() according to current practise */&lt;br&gt;
cplxFun_t const mostSignificant (&amp;amp;std::abs);&lt;br&gt;
cplxFun_t const leastSignificant(&amp;amp;std::arg);&lt;/p&gt;

&lt;h1&gt;
  
  
  else
&lt;/h1&gt;

&lt;p&gt;/*  on real and imaginary parts as per SEP 21 recommendation.  */&lt;br&gt;
cplxFun_t const mostSignificant (&amp;amp;std::real);&lt;br&gt;
cplxFun_t const leastSignificant(&amp;amp;std::imag);&lt;/p&gt;

&lt;h1&gt;
  
  
  endif
&lt;/h1&gt;

&lt;p&gt;template&amp;lt;&amp;gt; struct less&lt;a&gt;std::complex&amp;lt;double&lt;/a&gt; &amp;gt;&lt;br&gt;
{&lt;br&gt;
  bool operator()(std::complex const&amp;amp; op1,&lt;br&gt;
                  std::complex const&amp;amp; op2) const&lt;br&gt;
  {&lt;br&gt;
    if (mostSignificant(op1) &amp;lt; mostSignificant(op2))&lt;br&gt;
    {&lt;br&gt;
      return true;&lt;br&gt;
    }&lt;br&gt;
    if (mostSignificant(op2) &amp;lt; mostSignificant(op1))&lt;br&gt;
    {&lt;br&gt;
      return false;&lt;br&gt;
    }&lt;br&gt;
    return leastSignificant(op1) &amp;lt; leastSignificant(op2);&lt;br&gt;
  }&lt;br&gt;
};&lt;br&gt;
  // ....&lt;br&gt;
}&lt;br&gt;
The standard library doesn't set an order for complex numbers, so the developers came up with their own. The issue is that the std namespace can't be extended. According to the standard, the program behavior is undefined. The only exception to this rule is when a standard library class template is specialized, if the declaration depends on a custom type. However, std::complex isn't one of them.&lt;/p&gt;

&lt;p&gt;How can we fix this? We have two options:&lt;/p&gt;

&lt;p&gt;we can create our own comparator and use it in the appropriate contexts;&lt;br&gt;
we can create our own wrapper class for std::complex and then specialize std::less for it.&lt;br&gt;
1429_Scilab/image2.png&lt;/p&gt;

&lt;p&gt;Another issue worth noting is taking the addresses of standard library functions. Pay attention to how the mostSignificant and leastSignificant pointers are initialized in both branches. Since C++20, the behavior is well-defined when a program tries to take the address of an addressable function. Otherwise, the behavior is unspecified. The std::abs, std::arg, std::real, and std::imag overloads for complex numbers aren't addressable functions.&lt;/p&gt;

&lt;p&gt;What can we do about this? We can wrap the calls to these functions in a lambda:&lt;/p&gt;

&lt;h1&gt;
  
  
  ifndef SCI_CPLX_ORDER_SEP21
&lt;/h1&gt;

&lt;p&gt;/* on abs() and arg() according to current practise */&lt;br&gt;
const auto mostSignificant =&lt;br&gt;
  &lt;a&gt;&lt;/a&gt; { return std::abs(val); };&lt;/p&gt;

&lt;p&gt;const auto leastSignificant = &lt;br&gt;
  &lt;a&gt;&lt;/a&gt; { return std::arg(val); };&lt;/p&gt;

&lt;h1&gt;
  
  
  else
&lt;/h1&gt;

&lt;p&gt;/*  on real and imaginary parts as per SEP 21 recommendation.  */&lt;br&gt;
const auto mostSignificant =&lt;br&gt;
  &lt;a&gt;&lt;/a&gt; { return std::real(val); };&lt;/p&gt;

&lt;p&gt;const auto leastSignificant =&lt;br&gt;
  &lt;a&gt;&lt;/a&gt; { return std::imag(val); };&lt;/p&gt;

&lt;h1&gt;
  
  
  endif
&lt;/h1&gt;

&lt;p&gt;Although Scilab is compiled with the -std=c++17 flag, preparing the codebase for migration to newer standards is highly recommended.&lt;/p&gt;

&lt;p&gt;Snippet 6&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning: V628 [CWE-670, CERT-EXP19-C] It's possible that the line was commented out improperly, thus altering the program's operation logics. LinkAdapter.cpp 1140&lt;/p&gt;

&lt;p&gt;// delete all information related to the block&lt;br&gt;
void LinkAdapter::remove_partial_links_information(ScicosID uid)&lt;br&gt;
{&lt;br&gt;
    auto it = partial_links.find(uid);&lt;br&gt;
    if (it != partial_links.end())&lt;br&gt;
       //DEBUG std::cerr &amp;lt;&amp;lt; "erase " &amp;lt;&amp;lt; uid &amp;lt;&amp;lt; " partials " &lt;br&gt;
       //&amp;lt;&amp;lt; to_string(it-&amp;gt;second) &amp;lt;&amp;lt; "\n";&lt;br&gt;
    partial_links.erase(uid);&lt;br&gt;
}&lt;br&gt;
The analyzer detected an issue where the code's formatting doesn't reflect the program's actual behavior.&lt;/p&gt;

&lt;p&gt;The if statement is missing braces, and there's a commented line required for debugging between the condition and the erase call. Everything works as intended as long as it's commented: erase is called only if the element is found. Otherwise, the debug statement is included in the condition body, and erase is always called. This code is misleading.&lt;/p&gt;

&lt;p&gt;Out of curiosity, I checked the revision history to see what the code looked like before. Originally, the member function looked like this:&lt;/p&gt;

&lt;p&gt;// delete all information related to the block&lt;br&gt;
void LinkAdapter::remove_partial_links_information(ScicosID uid)&lt;br&gt;
{&lt;br&gt;
    partial_links.erase(uid);&lt;br&gt;
}&lt;br&gt;
Then, the search by key and debug output were added.&lt;/p&gt;

&lt;p&gt;We can make the code work as expected by simply adding braces. We can also simplify the deletion process a bit:&lt;/p&gt;

&lt;p&gt;void LinkAdapter::remove_partial_links_information(ScicosID uid)&lt;br&gt;
{&lt;br&gt;
  auto it = partial_links.find(uid);&lt;br&gt;
  if (it != partial_links.end())&lt;br&gt;
  {&lt;br&gt;
    //DEBUG std::cerr &amp;lt;&amp;lt; "erase " &amp;lt;&amp;lt; uid &amp;lt;&amp;lt; " partials "&lt;br&gt;
    // &amp;lt;&amp;lt; to_string(it-&amp;gt;second) &amp;lt;&amp;lt; "\n";&lt;br&gt;
    partial_links.erase(it);&lt;br&gt;
  }&lt;br&gt;
}&lt;br&gt;
Now the erase member function immediately receives the iterator found using find; no extra search by key is performed.&lt;/p&gt;

&lt;p&gt;Snippet 7&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning: V502 [CWE-783, CERT-EXP00-C] Perhaps the '?:' operator works in a different way than it was expected. The '?:' operator has a lower priority than the '==' operator. sci_sparse.cpp 51&lt;/p&gt;

&lt;p&gt;types::Function::ReturnValue sci_sparse&lt;br&gt;
  (types::typed_list &amp;amp;in, int _piRetCount, types::typed_list &amp;amp;out)&lt;br&gt;
{&lt;br&gt;
  bool isValid = true;&lt;br&gt;
  // ....&lt;br&gt;
  for (int i = 0 ; isValid &amp;amp;&amp;amp; i &amp;lt; in.size() ; i++)&lt;br&gt;
  {&lt;br&gt;
    // ....&lt;br&gt;
    case types::InternalType::ScilabBool :&lt;br&gt;
    case types::InternalType::ScilabSparseBool :&lt;br&gt;
    {&lt;br&gt;
      isValid = (i == (in.size() &amp;gt; 1) ? 1 : 0);&lt;br&gt;
    }&lt;br&gt;
  // ....&lt;br&gt;
}&lt;br&gt;
Here's an amusing code snippet containing a precedence error that doesn't affect the result. The conditional operator has lower precedence than the comparison operator, so the expression is parsed as follows: (i == (in.size() &amp;gt; 1)) ? 1 : 0. First, the compiler compares i to the in.size() &amp;gt; 1 result, then, it applies the conditional operator. Perhaps the parentheses should've been placed differently:&lt;/p&gt;

&lt;p&gt;isValid = i == ((in.size() &amp;gt; 1) ? 1 : 0);&lt;br&gt;
However, the result is the same anyway. The curious thing is that the ternary operator doesn't change the value: the comparison result is already either 0 or 1, so the operator is redundant regardless of how the parentheses are arranged, and isValid gets the same value.&lt;/p&gt;

&lt;p&gt;To avoid misleading developers, it's best to remove the unnecessary operation:&lt;/p&gt;

&lt;p&gt;isValid = i == (in.size() &amp;gt; 1);&lt;br&gt;
Typos and copypaste&lt;br&gt;
Snippet 8&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning: V792 [CWE-691] The 'isComplex' function located to the right of the operator '|' will be called regardless of the value of the left operand. Perhaps, it is better to use '||'. sci_mesh2di.cpp 42&lt;/p&gt;

&lt;p&gt;types::Function::ReturnValue sci_mesh2di&lt;br&gt;
  (types::typed_list &amp;amp;in, int _iRetCount, types::typed_list &amp;amp;out)&lt;br&gt;
{&lt;br&gt;
  // ....&lt;br&gt;&lt;br&gt;
  for (int i = 0; i &amp;lt; in.size(); i++)&lt;br&gt;
  {&lt;br&gt;
    if (!in[i]-&amp;gt;isDouble() | in[i]-&amp;gt;getAs&lt;a&gt;types::Double&lt;/a&gt;()-&amp;gt;isComplex())&lt;br&gt;
    {&lt;br&gt;
      Scierror(999, _&lt;br&gt;
        ("%s: Wrong type for argument #%d: Real matrix expected.\n"), &lt;br&gt;
          "mesh2di", i);&lt;br&gt;
      return types::Function::Error;&lt;br&gt;
    }&lt;br&gt;
  }&lt;br&gt;
  // ....&lt;br&gt;
}&lt;br&gt;
The loop checks whether all input arguments are real, non-complex numbers of the Double type. However, it uses the bitwise | instead of the logical ||. The bitwise operator doesn't have a short-circuit evaluation, so it always evaluates the right operand, even if the left operand has already established that the argument isn't a Double.&lt;/p&gt;

&lt;p&gt;The getAs function is implemented as static_cast(this) without any type checking:&lt;/p&gt;

&lt;p&gt;template&lt;br&gt;
inline T* getAs(void)&lt;br&gt;
{&lt;br&gt;
   return static_cast(this);&lt;br&gt;
}&lt;br&gt;
Casting an object of a dynamic type that is neither a Double nor a subclass of a Double results in undefined behavior. Here's the fixed code:&lt;/p&gt;

&lt;p&gt;if (!in[i]-&amp;gt;isDouble() || in[i]-&amp;gt;getAs&lt;a&gt;types::Double&lt;/a&gt;()-&amp;gt;isComplex())&lt;br&gt;
Snippet 9&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning: V501 [CWE-570] There are identical sub-expressions to the left and to the right of the '!=' operator: pDblx-&amp;gt;getSize() != pDblx-&amp;gt;getSize() sci_mesh2di.cpp 52&lt;/p&gt;

&lt;p&gt;types::Function::ReturnValue sci_mesh2di&lt;br&gt;
  (types::typed_list &amp;amp;in, int _iRetCount, types::typed_list &amp;amp;out)&lt;br&gt;
{&lt;br&gt;
  // ....&lt;br&gt;
  types::Double *pDblx = in[0]-&amp;gt;getAs&lt;a&gt;types::Double&lt;/a&gt;();&lt;br&gt;
  types::Double *pDbly = in[1]-&amp;gt;getAs&lt;a&gt;types::Double&lt;/a&gt;();&lt;/p&gt;

&lt;p&gt;if (pDblx-&amp;gt;getSize() != pDblx-&amp;gt;getSize())             // &amp;lt;=&lt;br&gt;
  {&lt;br&gt;
    Scierror(999, _("%s: Arguments #%d and #%d must have the same sizes.\n")&lt;br&gt;
             , "mesh2di", 1,2);&lt;br&gt;
    return types::Function::Error;&lt;br&gt;
  }&lt;br&gt;
  // ....&lt;br&gt;
}&lt;br&gt;
The function takes two arrays as input and checks whether they have the same size before proceeding. If their sizes differ, it prints an error message and exits. However, in the pDblx-&amp;gt;getSize() != pDblx-&amp;gt;getSize() condition, the code compares the pDblx size to itself.&lt;/p&gt;

&lt;p&gt;This is a classic copy-paste typo. The developers forgot to replace pDblx with pDbly in the second part of the condition. The expression is always false, so the check never triggers. This causes issues further in the code:&lt;/p&gt;

&lt;p&gt;int iNbNodes = pDblx-&amp;gt;getSize();&lt;br&gt;
// ....&lt;br&gt;
for (int i=0; i &amp;lt; iNbNodes; i++)&lt;br&gt;
{&lt;br&gt;
  pDblxy-&amp;gt;set(0,i,pDblx-&amp;gt;get(i));&lt;br&gt;
  pDblxy-&amp;gt;set(1,i,pDbly-&amp;gt;get(i));&lt;br&gt;
}&lt;br&gt;
If one array is shorter than the other, the loop goes out of bounds. Instead of receiving a clear message, the user may encounter a crash, an incorrect result, or anything else at all, because the behavior in such a case is undefined. Here's the fixed code:&lt;/p&gt;

&lt;p&gt;if (pDblx-&amp;gt;getSize() != pDbly-&amp;gt;getSize())&lt;br&gt;
Snippet 10&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning: V517 [CWE-570, CERT-MSC01-C] The use of 'if (A) {...} else if (A) {...}' pattern was detected. There is a probability of logical error presence. Check lines: 701, 785. fromws_c.cpp 701&lt;/p&gt;

&lt;p&gt;typedef enum&lt;br&gt;
{&lt;br&gt;
  SCSREAL_N = 10,&lt;br&gt;
  SCSCOMPLEX_N = 11,&lt;br&gt;
  SCSINT_N = 80,&lt;br&gt;
  SCSINT8_N = 81,&lt;br&gt;
  SCSINT16_N = 82,&lt;br&gt;
  SCSINT32_N = 84,&lt;br&gt;
  SCSUINT_N = 800,&lt;br&gt;
  SCSUINT8_N = 811,&lt;br&gt;
  SCSUINT16_N = 812,&lt;br&gt;
  SCSUINT32_N = 814,&lt;br&gt;
  SCSUNKNOW_N = -1&lt;br&gt;
} scicos_datatype_number;&lt;/p&gt;

&lt;p&gt;SCICOS_BLOCKS_IMPEXP void fromws_c(scicos_block* block, int flag)&lt;br&gt;
{&lt;br&gt;
  //....&lt;br&gt;
  else if (ytype == SCSINT32_N)&lt;br&gt;
  {&lt;br&gt;
    /* --------------------- int32 long ---------------------&lt;em&gt;/&lt;br&gt;
    y_l = Getint32OutPortPtrs(block, 1);&lt;br&gt;
    // ....&lt;br&gt;
  }&lt;br&gt;
  // ....&lt;br&gt;
  else if (ytype == SCSINT32_N)&lt;br&gt;
  {&lt;br&gt;
    /&lt;/em&gt; --------------------- uint32 ulong ---------------------*/&lt;br&gt;
    y_ul = Getuint32OutPortPtrs(block, 1);&lt;br&gt;
    // ....&lt;br&gt;
  } &lt;br&gt;
  // ....&lt;br&gt;
}&lt;br&gt;
In a long chain of if ... else if statements, the ytype variable is compared to values of the scicos_datatype_number enumeration. The analyzer detected that the ytype == SCSINT32_N condition appears twice, on lines 701 and 785. The second check never executes.&lt;/p&gt;

&lt;p&gt;Judging by the comment in the second uint32 ulong branch, the ytype == SCSUINT32_N check should've been there. The enumeration does contain this constant, and its value is 814. This is another typical copy-paste mistake: the developers copied the int32 branch, changed its body, but forgot to update the condition. As a result, the branch for uint32 is missing.&lt;/p&gt;

&lt;p&gt;Here's the fixed code:&lt;/p&gt;

&lt;p&gt;else if (ytype == SCSUINT32_N)&lt;br&gt;
Snippet 11&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning: V766 [CWE-462] An item with the same key '"H5T_VAX_F64"' has already been added. H5Type.hxx 241&lt;/p&gt;

&lt;p&gt;static std::map initMap()&lt;br&gt;
{&lt;br&gt;
  std::map nameToType;&lt;br&gt;
  // ....&lt;/p&gt;

&lt;p&gt;nameToType.insert&lt;br&gt;
    (std::pair("H5T_IEEE_F64LE", H5T_IEEE_F64LE));&lt;br&gt;
  nameToType.insert&lt;br&gt;
    (std::pair("H5T_VAX_F32", H5T_VAX_F32));    // &amp;lt;=&lt;br&gt;
  nameToType.insert&lt;br&gt;
    (std::pair("H5T_VAX_F64", H5T_VAX_F64));    // &amp;lt;=&lt;br&gt;
  nameToType.insert&lt;br&gt;
    (std::pair("H5T_NATIVE_FLOAT", H5T_NATIVE_FLOAT));&lt;br&gt;
  // ....&lt;br&gt;
  nameToType.insert&lt;br&gt;
    (std::pair("H5T_VAX_F32", H5T_VAX_F32));    // &amp;lt;=&lt;br&gt;
  nameToType.insert&lt;br&gt;
    (std::pair("H5T_VAX_F64", H5T_VAX_F64));    // &amp;lt;=&lt;br&gt;
  nameToType.insert&lt;br&gt;
    (std::pair("H5T_MIPS_F32", H5T_MIPS_F32));&lt;br&gt;
  // ....&lt;br&gt;
}&lt;br&gt;
The initMap function populates a mapping table that associates string type names with their IDs. The analyzer detected that the "H5T_VAX_F32" and "H5T_VAX_F64" pairs are added more than once. The developers might have copied the same lines, accidentally adding them to the list twice.&lt;/p&gt;

&lt;p&gt;The duplicates are harmless for now because the values match. However, if one of them contained a typo, then a value might have been omitted. Manually finding such a mistake in 124 lines of code would take a lot of time. Static analysis is really useful in situations like this.&lt;/p&gt;

&lt;p&gt;Here are the similar warnings:&lt;/p&gt;

&lt;p&gt;V766 [CWE-462] An item with the same key '"H5T_VAX_F32"' has already been added. H5Type.hxx 240&lt;br&gt;
V766 [CWE-462] An item with the same key 'L"mustBeMember"' has already been added. arguments.cpp 1098&lt;br&gt;
Snippet 12&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning: V519 [CWE-563, CERT-MSC13-C] The 'iy' variable is assigned values twice successively. Perhaps this is a mistake. Check lines: 53, 55. int2db.c 55&lt;/p&gt;

&lt;p&gt;int C2F(int2db)(int *n, int *dx, int *incx, double *dy, int *incy)&lt;br&gt;
{&lt;br&gt;
  int ix = 0, iy = 0;&lt;br&gt;
  ....&lt;br&gt;
  ix = 1;&lt;br&gt;
  iy = 1;&lt;/p&gt;

&lt;p&gt;if (*incx &amp;lt; 0)&lt;br&gt;
  {&lt;br&gt;
      ix = (-(*n) + 1) * *incx + 1;&lt;br&gt;
  }&lt;br&gt;
  if (*incy &amp;lt; 0)&lt;br&gt;
  {&lt;br&gt;
      iy = (-(*n) + 1) * *incy + 1;&lt;br&gt;
  }&lt;br&gt;
  iy = (-(*n) + 1) * *incy + 1;&lt;br&gt;
  ....&lt;br&gt;
}&lt;br&gt;
In the function, the ix and iy variables get the 1 value. Then, if either incx or incy is negative, the corresponding index is recalculated using a formula. Right after that, the same formula is applied again to iy, but this time without the condition. This makes the if (*incy &amp;lt; 0) block pointless. The extra line most likely got into the code by mistake and should be removed.&lt;/p&gt;

&lt;p&gt;It's curious that this error isn't new. Looking through the commit history reveals that the extra line was present in earlier versions. At that time, the conditions appeared on a single line, while the redundant assignment sat below them, indented as a part of the preceding if statement:&lt;/p&gt;

&lt;p&gt;1429_Scilab/image3.png&lt;/p&gt;

&lt;p&gt;The developers later reformatted the code and added braces but didn't remove or replace the line:&lt;/p&gt;

&lt;p&gt;1429_Scilab/image4.png&lt;/p&gt;

&lt;p&gt;This example shows that an error may go unnoticed for years, but it can be detected, for example, using static code analysis.&lt;/p&gt;

&lt;p&gt;Those AI trainers&lt;br&gt;
We recently published an article on our blog titled "Bloated C++ code," which covered an AI-generated project. Its main issue was code bloat. AI-generated code often contains lots of repetitive blocks and pointless checks. While reviewing the Scilab analysis report, I got the impression that the AI model had learned from examples similar to those found in real-world projects. Here are some of them.&lt;/p&gt;

&lt;p&gt;Snippet 13&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning: V571 [CWE-571] Recurring check. The 'if (m_prob_mem != __null)' condition was already verified in line 80. IDAManager.hxx 82&lt;/p&gt;

&lt;p&gt;// ....&lt;br&gt;
~IDAManager() {&lt;br&gt;
  if (m_prob_mem != NULL)&lt;br&gt;
  {&lt;br&gt;
     if (m_prob_mem != NULL) IDAFree(&amp;amp;m_prob_mem);&lt;br&gt;
      m_prob_mem = NULL;&lt;br&gt;
  }&lt;br&gt;
  ....&lt;br&gt;
}&lt;br&gt;
First, the program checks whether the m_prob_mem pointer is not null, and then repeats this check in the very next line. But there's more: the IDAFree function contains another check for the same pointer:&lt;/p&gt;

&lt;p&gt;void IDAFree(void** ida_mem)&lt;br&gt;
{&lt;br&gt;
  IDAMem IDA_mem;&lt;/p&gt;

&lt;p&gt;if (*ida_mem == NULL) { return; }&lt;br&gt;
  // ....&lt;br&gt;
  free(*ida_mem);&lt;br&gt;
  *ida_mem = NULL;&lt;br&gt;
}&lt;br&gt;
At the end of the function, the memory is released and the pointer is set to NULL, so we can replace the entire code with a single line:&lt;/p&gt;

&lt;p&gt;~IDAManager() {&lt;br&gt;
  IDAFree(&amp;amp;m_prob_mem);&lt;br&gt;
  // ....&lt;br&gt;
}&lt;br&gt;
Snippet 14&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning: V519 The 'tmp-&amp;gt;sn_size' variable is assigned values twice successively. Perhaps this is a mistake. Check lines: 544, 550. taucs_scilab.c 550&lt;/p&gt;

&lt;p&gt;static supernodal_frontal_matrix*&lt;br&gt;
supernodal_frontal_create(int* firstcol_in_supernode,&lt;br&gt;
                          int sn_size,&lt;br&gt;
                          int n,&lt;br&gt;
                          int* rowind)&lt;br&gt;
{&lt;br&gt;
  ....&lt;br&gt;
  tmp-&amp;gt;sn_size = sn_size;&lt;br&gt;
  tmp-&amp;gt;n = n;&lt;/p&gt;

&lt;p&gt;tmp-&amp;gt;rowind = rowind;&lt;/p&gt;

&lt;p&gt;tmp-&amp;gt;n = n;&lt;br&gt;
  tmp-&amp;gt;sn_size = sn_size;&lt;br&gt;
  ....&lt;br&gt;
}&lt;br&gt;
Unless you've seen how things work in real projects, you might think that assigning a value just once is enough. First, they assign the values one way, then they do it all over again in reverse.&lt;/p&gt;

&lt;p&gt;First, values are assigned to the sn_size and n data members, then to rowind. Finally, the same values are assigned to n and sn_size a second time, but in reverse order. The developers have to compare both blocks every time to make sure they're identical.&lt;/p&gt;

&lt;p&gt;Snippet 15&lt;/p&gt;

&lt;p&gt;The PVS-Studio warning: V547 [CWE-570] Expression 'i &amp;gt;= m_iSize' is always false. cell.cpp 254&lt;/p&gt;

&lt;p&gt;Cell* Cell::set(InternalType** _pIT)&lt;br&gt;
{&lt;br&gt;
  // ....&lt;br&gt;
  for (int i = 0; i &amp;lt; m_iSize; i++)&lt;br&gt;
  {&lt;br&gt;
    if (i &amp;gt;= m_iSize)&lt;br&gt;
    {&lt;br&gt;
      return NULL;&lt;br&gt;
    }&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (m_pRealData[i] != NULL)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;// ....&lt;br&gt;
}&lt;br&gt;
The i &amp;lt; m_iSize loop condition guarantees that the i &amp;gt;= m_iSize check within the loop will never execute. The developers might have copied this check from the neighboring overload, Cell::set(int _iIndex, InternalType* _pIT). There, the index comes from outside the function, so the _iIndex &amp;gt;= m_iSize check prevents out-of-bounds access. However, in this overload, the loop keeps the index within bounds, making the check redundant. We can remove it to avoid confusing developers and cluttering the code.&lt;/p&gt;

&lt;p&gt;Let's sum-up&lt;br&gt;
Static analysis helps detect errors that may have been lurking in a project for years. You can check your own project using PVS-Studio analyzer right now. We've covered all the necessary steps—from installation to your first report—in the article "Get started with PVS-Studio static analyzer".&lt;/p&gt;

&lt;p&gt;Try PVS-Studio for free&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>debugging</category>
    </item>
    <item>
      <title>English instead of code</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Fri, 20 Nov 2026 12:47:28 +0000</pubDate>
      <link>https://dev.to/pvsdev/english-instead-of-code-b39</link>
      <guid>https://dev.to/pvsdev/english-instead-of-code-b39</guid>
      <description>&lt;p&gt;A language invented so that programmers would no longer be needed kickstarted a profession that is still around today and pays quite well. Another language was called "structured English", and people thought any accountant would be able to use it. It also spawned a new specialty with its own salary band. The attempt to deliver "programming without programmers by the year 2000" proved unsuccessful, too.&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%2F84vbn0aaadacmxnyk69v.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%2F84vbn0aaadacmxnyk69v.png" alt="1428_english_instead_of_code/image1.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We published and translated this article with the copyright holder's permission. The author is &lt;a href="https://www.linkedin.com/in/dalerank" rel="noopener noreferrer"&gt;Sergey Kushnirenko&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You might've recognized COBOL, which is still used daily and, by various estimates, sits behind 80% of ordinary (in-store) banking transactions, while its codebase runs to a couple of hundred billion lines and feeds tens, if not hundreds, of thousands of developers. Although, if you dig into those pretty numbers, almost all of them trace back to a single survey from the late nineties that was then endlessly re-extrapolated onto the whole world, so it's more fair to treat them as estimates rather than precise data.&lt;/p&gt;

&lt;p&gt;SQL spawned the peculiar caste of DBAs (Database Administrators) and Data Engineers, and today a person who writes "simple queries" can easily earn a mid-level salary. Meanwhile, the language itself has become so complex that modern dialects (PostgreSQL, Oracle) are full-fledged programming languages with procedural logic, where you can write absolutely anything, from a fractal generator to a game engine.&lt;/p&gt;

&lt;p&gt;The 4GL family was a "golden cage" that worked beautifully for building typical "input-output" forms. However, as soon as non-standard business logic or an integration with an external service was required, the tool hit its limits. Developers then had to bolt on "crutches" in low-level languages, turning development into a hellish cocktail of visual design and dirty hacks. And instead of programmers disappearing, 4GL created "enterprise system architects" (SAP ABAP, for example) who became insanely expensive experts. It didn't eliminate programming either but moved it out of the zone of "general-purpose languages" and into the zone of "expensive and temperamental tools" that lock a company to a particular vendor.&lt;/p&gt;

&lt;p&gt;Every ten to fifteen years the industry solemnly announces that the need for manual coding will soon disappear, offering a new way to "explain tasks to machines in plain human language.". And every few years, we can see the number of programmers going up rather than down, but they just get a new name. I would venture that right now we're living through another turn of this cycle, and the new name for such people will be "vibe coders". I also think AI is destined to meet the same fate as the previous attempts.&lt;/p&gt;

&lt;p&gt;The idea of "talking to a machine in human language" is older than the very notion of a computer. Science fiction from the first half of the twentieth century is full of it: from Fritz Lang's "Metropolis", which will turn a hundred years old any minute now, with its mechanical double of a human being, to stories where the machine is given orders by voice and obediently carries them out. But as soon as real computing machines appeared in the forties, it became clear that the problem was not the machine.&lt;/p&gt;

&lt;p&gt;Konrad Zuse, who built the Z3 in 1941 and developed one of the first high-level languages (Plankalkül), framed the problem like this: "The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." I know many developers who really treat programming languages as poor substitutes for English. I won't argue with them... They aren't "bad versions of English," but rather tools with fundamentally different properties, no worse than natural language and no better; they're simply different.&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%2Fsjxgn2zzx8uvujx1dv7z.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%2Fsjxgn2zzx8uvujx1dv7z.png" alt="1428_english_instead_of_code/image2.png" width="800" height="596"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What a programming language actually does
&lt;/h2&gt;

&lt;p&gt;What does the phrase "calculate the quarterly bonuses for the employees" mean to an accountant? Early in my career, I worked at a small company that contributed to the entropy of the world by developing something like a payroll engine, so I got a little taste of that stuff. For a living human being that single phrase would be enough to ask a dozen of questions: who exactly gets a bonus? What formula to use? What about employees who joined midway through the quarter? What to do with those who quit? Should the figures be rounded, and if so, in which direction? What currency to use? What to do if no data is available for an employee? Natural language works precisely because the listener fills in those gaps with context, common sense and general knowledge, of which they have plenty, because they work in that field.&lt;/p&gt;

&lt;p&gt;A program and code... can't do that. Each of those decisions has to be pinned down somewhere, propped up, spelled out as a procedure. Otherwise, the result will be non-deterministic, and non-deterministic accounting isn't accounting; it's grounds for a conversation with the IRS. Often, even experienced accountants can't take everything into account at once, so a short phrase in natural language unfolds into 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="s2"&gt;"calculate the quarterly bonuses"&lt;/span&gt;
    ▼
&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;which&lt;/span&gt; &lt;span class="n"&gt;quarter&lt;/span&gt; &lt;span class="n"&gt;exactly&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;calendar&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;fiscal&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;counted&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;hire&lt;/span&gt; &lt;span class="n"&gt;date&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;who&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;included&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;staff&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;part&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;timers&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;those&lt;/span&gt; &lt;span class="n"&gt;who&lt;/span&gt; &lt;span class="n"&gt;quit&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;quarter&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;the&lt;/span&gt; &lt;span class="n"&gt;accrual&lt;/span&gt; &lt;span class="n"&gt;base&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;base&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;overtime&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;average&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;period&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;the&lt;/span&gt; &lt;span class="n"&gt;formula&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;percentage&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;fixed&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;progressive&lt;/span&gt; &lt;span class="n"&gt;scale&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;proration&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;partial&lt;/span&gt; &lt;span class="n"&gt;quarter&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;by&lt;/span&gt; &lt;span class="n"&gt;days&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;by&lt;/span&gt; &lt;span class="n"&gt;months&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;none&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="n"&gt;all&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;rounding&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;half&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;down&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;dollar&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;cent&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;currency&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;exchange&lt;/span&gt; &lt;span class="n"&gt;rate&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;as&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;which&lt;/span&gt; &lt;span class="n"&gt;date&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="n"&gt;we&lt;/span&gt; &lt;span class="n"&gt;take&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;rate&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;missing&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;skip&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;treat&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="n"&gt;as&lt;/span&gt; &lt;span class="n"&gt;zero&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;crash&lt;/span&gt; &lt;span class="n"&gt;with&lt;/span&gt; &lt;span class="n"&gt;an&lt;/span&gt; &lt;span class="n"&gt;error&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;Eight questions out of one, and I was being lazy at that. Every question spawns even more questions, and once it stops doing so, you can describe it in the terms of programming languages.&lt;/p&gt;

&lt;p&gt;A programming language exists for this tedious precision, not in spite of it. It provides a syntax where ambiguity is physically inexpressible, and semantics in which the same program always means the same thing. A programmer who "translates" the requests of a manager and an accountant into code isn't translating a spec into code. Rather, they take on those eight or more questions and assume responsibility for the answers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Just say it in plain...
&lt;/h2&gt;

&lt;p&gt;...doesn't work. There are reasons why the idea of "natural language instead of code" hits its limits, and they are about us... humans, not about the tools, compilers, standard libraries, or the limitations of existing programming languages. And the main one is the inherent ambiguity of natural languages.&lt;/p&gt;

&lt;p&gt;What would you do if you were asked to "delete all files older than 30 days in this folder"? Most likely you would just select the files by modification date and delete them... It looks like a simple action, but you have just used your experience to answer 90% of the sub-questions. And there are quite a few of those sub-questions once you start articulating the main one:&lt;/p&gt;

&lt;p&gt;— Older by creation date or by modification date?&lt;/p&gt;

&lt;p&gt;— Do we include subfolders and the files in them?&lt;/p&gt;

&lt;p&gt;— Do we delete hidden files?&lt;/p&gt;

&lt;p&gt;— What about symlinks? Do we delete the link or the file itself?&lt;/p&gt;

&lt;p&gt;... and so on.&lt;/p&gt;

&lt;p&gt;To remove the ambiguity you either have to ask a series of clarifying questions, effectively writing the program's code in words, or make the decisions on behalf the person who asked, possibly doing other than what they wanted. Programming languages resolve those sub-questions with brute logic, because every construct has exactly one meaning, and &lt;code&gt;mtime&lt;/code&gt; never suddenly turns into &lt;code&gt;ctime&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;However, even having answered every question and every sub-question precisely, we still run into Rice's theorem, a rather boring mathematical treatise from 1953 with a joyless real-world implication for us humans. It goes like this: "No non-trivial semantic property of a program can be checked algorithmically in the general case". That is, you can't automatically guarantee that the generated code does exactly what was asked, which means somebody has to check what came out. To check the code, you have to read it; and to read the code, you need a formal language. We've come full circle.&lt;/p&gt;

&lt;p&gt;Another property indirectly comes to light, no longer about Rice but about natural language itself. The more complex a composition we want to build, the worse it maps onto words. In plain terms, it sounds like this: a small function can be described in words, and it'll even come out decent and clear, but a million-line system can't be described in words. The problem isn't a lack of words; natural language simply doesn't compose. Meanings can overlap and flow from one sentence to the next or depend on neighboring sentences, capturing paragraphs and sometimes even larger stretches of the text.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What follows is a difficult block. If linguistics isn't your thing, you can scroll to the next section, where the idea is repeated through things and comparisons a developer will understand.&lt;/p&gt;

&lt;p&gt;However, if you're curious where this "irreducibility to words" comes from at all, linguists described it long ago and in detail, long before any AI. They tried to formally define and compute the meanings of an ordinary human phrase, and discovered that the meaning of a sentence can't be computed by looking at the sentence alone. No text in natural language can be carried over as is into other forms of representation without losing context or the meaning of individual parts. The process of carrying it over requires splitting the shared context into parts, which means assessing the essential complexity of the task.&lt;/p&gt;

&lt;p&gt;A programmer is in this state before writing any code; it's called... drum roll... analysis paralysis (sometimes also called Brooks' stupor, as the process of overcoming that very essential complexity). The idea of "just tell the machine in words" stumbles over this state, and so does the top-down "here is a spec in words, go write a program in...", so it's worth taking a look at what has been dug up over forty years.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  About Rice, Kamp, and DRT
&lt;/h2&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%2Fxn20gpwnp2wwq4sgv77z.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%2Fxn20gpwnp2wwq4sgv77z.png" alt="1428_english_instead_of_code/image3.png" width="800" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But natural language has another affliction, and this one isn't about Rice. The sentences themselves may be built illogically, tripled up, smudged and back-to-front invented, twisted and tangled, cut inside out and woven every which way, so that there's more than one path in that garden of words, and the paths there fork right in the middle of the phrase, and you realize it only once you have read to the end, or even reread from the beginning. Not that I was ever a great linguist, but at the end of 2010s I had to extend a profanity filter in one of our apps, and in the great and mighty Russian tongue one could tell someone off in a great many ways, so I had to touch on this topic a little as well. But let's get back to our muttons... that is, our words.&lt;/p&gt;

&lt;p&gt;As long as we speak in small phrases like "delete the file", everything is fine: the whole phrase fits into your head and doesn't drag a tail of context behind it. But when we try to describe something more complicated in words, they start to "get in each other's way" or "depend on each other" across sentences, whereas a variable in a normal programming language should depend only on the precise context where it's used.&lt;/p&gt;

&lt;p&gt;The classic phrase "every farmer who owns a donkey beats it" is clear to any English speaker and usually reads without a hitch. Now try to break it into parts and preserve the meaning of each part separately, the way a compiler does with an expression.&lt;/p&gt;

&lt;p&gt;"Who owns a donkey" now merely asserts the existence of a donkey belonging to some farmer, while "beats it", taken apart from everything else, gives no clue at all as to who is being beaten, because "it" shows up after "donkey" has already flickered by and formally gone out of scope.&lt;/p&gt;

&lt;p&gt;A human doesn't notice this, because they hold the whole chunk in their head and can reconstruct that connection on the fly. However, a function that computes the meaning of a sentence piece by piece from left to right, the way any normal parser does, will either break or be so complex that it's not worth discussing its practical application.&lt;/p&gt;

&lt;p&gt;For a programmer, this would be a variable declared inside one function and yet visible in the next one too, without being passed explicitly and without global, because somewhere the usage context got snagged. No language allows that, and scopes were invented precisely so that such a thing would be impossible by design. Natural languages, on the other hand, live on this very concept: its "donkey" leaks across the sentence boundary, and that counts as normal behavior.&lt;/p&gt;

&lt;p&gt;The context leak was described by linguists long before any AI: in the early eighties Hans Kamp did so in his work on dynamic semantics and, almost simultaneously, Irene Heim in her dissertation on file change semantics. They both came to the same conclusion independently: the meaning of a sentence can't be computed without dragging along what was said earlier.&lt;/p&gt;

&lt;p&gt;That is how discourse representation theory, DRT, was born. It's single practical conclusion for us programmers sounds boring and inapplicable, because in its "natural habitat" it works outside DRT. It's impossible to formally guarantee that a piece of text can be understood in isolation from the neighboring pieces. So, it's impossible to expect a language used to explain a task to a machine to behave like code while remaining a natural language.&lt;/p&gt;

&lt;p&gt;A programmer's natural habitat is Code, and Code is free of this disease, and the &lt;code&gt;calculateAge(1985, 2026)&lt;/code&gt; call means the same thing whether you stick it into a game loop or into a report for the IRS. It drags no invisible tail of previous lines that change its meaning.&lt;/p&gt;

&lt;p&gt;A programmer's job is to assemble a Big System out of independent pieces, which is possible only if the pieces don't change their meaning depending on where you put them. Natural language is fundamentally incapable of that: the state of a small sentence drifts with the size of the context you shove it into. Hardly anyone would be pleased if the result of &lt;code&gt;sum(a + b)&lt;/code&gt; suddenly started depending on the length of the calling function.&lt;/p&gt;

&lt;p&gt;I hope I haven't worn you out too much with this discourse.&lt;/p&gt;

&lt;h2&gt;
  
  
  If you are still curious about DRT
&lt;/h2&gt;

&lt;p&gt;To start with, you can look at the Stanford Encyclopedia of Philosophy articles: "Discourse Representation Theory" and "Dynamic Semantics". They're free, proofread by experts, and written in accessible language, unlike Kamp's original.&lt;/p&gt;

&lt;p&gt;The next level is the primary sources, the paper "A Theory of Truth and Semantic Representation" and Irene Heim's dissertation "The Semantics of Definite and Indefinite Noun Phrases". It's worth at least leafing through them to see that they aren't esoteric but rather an attempt to build that very "function over context" in the pre-AI era.&lt;/p&gt;

&lt;p&gt;All this literature really is hard, and it looks like watery water in a bucket of water to a non-specialist, but note what it took to formally describe just one level of the Discourse Representation Structure (DRS) of natural-language semantics, and even that doesn't close out the theory of language as a whole.&lt;/p&gt;

&lt;p&gt;Modern large language models aren't built on DRT, they're statistical transformers rather than Kamp's theory. So, this isn't the foundation of AI but the story of why functions over context run into such complexity and terabytes of RAM just so you can ask a model for Lenin's birthday. Also, to understand why the idea of making natural language a precise specification language looks naive. That's what vibe coders are selling right now as the dream of "English instead of 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%2F1lu2vu847v8robgzaclb.jpeg" 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%2F1lu2vu847v8robgzaclb.jpeg" alt="1428_english_instead_of_code/image4.jpeg" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  This has been tried before. More than once...
&lt;/h2&gt;

&lt;p&gt;Throughout history, there have been several attempts to create a programming language that resembles English to eliminate the need for programmers. Funnily enough, every one of them produced the same side effect: the old programmers stayed, but new professions were born, giving work to a new generation of programmers.&lt;/p&gt;

&lt;p&gt;COBOL (1959) was created with the goal of letting managers and business analysts read and write programs. That is why the syntax was deliberately designed to look like English sentences: ADD SALARY TO TOTAL GIVING NEW-TOTAL reads like an almost normal sentence. The result was the COBOL programmer profession, which is still alive today and runs many banking and government systems, as I said at the beginning of the article. However, managers never started using in it. They continued to write reports by hand or in Word and create tables and diagrams. They also started delegating assignments to COBOL developers.&lt;/p&gt;

&lt;p&gt;Then, the era of SQL came (1974). It was originally called SEQUEL—Structured English Query Language —because it was designed to enable not tech-savvy people ask a "program" questions in almost-English. Later, it was renamed to SQL for advertising and money reasons, but the intent of "English for queries" stayed sewn right into the name. Today, an SQL developer is a separate profession, and the "non-technical manager person" who runs into &lt;code&gt;LEFT JOIN&lt;/code&gt; with three subqueries and a window function usually calls a technical one who understands this complex construction.&lt;/p&gt;

&lt;p&gt;Then, there were 4GL and CASE (Computer-Aided Software Engineering, the eighties), which are considered fourth-generation "natural programming languages". Once again, they promised "programming without programmers". By the end of the 1980s, advertising was so powerful that many people were genuinely afraid of the "empty office" effect. In this scenario, most applications would be created without writing code, which would cause developers to switch to other professions. But again, by the year two thousand, the industry had not only failed to get rid of programmers, it had also acquired a plethora of new, highly paid specialists in SAP, Oracle, and proprietary frameworks.&lt;/p&gt;

&lt;p&gt;As it turned out, the "naturalness" of 4GL systems was just another illusion, like COBOL and SQL before it. It merely hid a monstrous configuration complexity and unpredictable system behavior behind a facade of visual blocks. The more the CASE tools tried to automate writing code, the more time programmers spent fighting the limitations of those very tools, turning from creators of logic into "architects of system integrations". Eventually, instead of developers disappearing, we got a shortage of them, while "programming without programmers" simply moved to a different plane. Instead of writing code in C++ or Pascal, you now have to learn a particular vendor's "secret knowledge," turning the visual builder into yet another expensive programming tool.&lt;/p&gt;

&lt;p&gt;And it's really expensive, because the salary of a SAP developer easily exceeds $100k, not to mention the cost of training and annual exams. On top of that, there's training at $Xk and mandatory yearly recertification. Without the latter, the certificate simply expires, and all of it is tied to the vendor's own paid subscription.&lt;/p&gt;

&lt;p&gt;Then came the turn of UML and Model-Driven Architecture (the nineties and the noughties) with the beautiful idea of drawing diagrams that would generate code. In reality, UML survived as a documentation and diagram tool for developers who then write the code manually anyway, and sometimes draw the diagrams from code that has already been written, which sort of hints at something. Then funding for UML dried up significantly, and in the early 2010s, the people who had been promoting Model-Driven Architecture flocked en masse to no-code and low-code startups and companies of all types and sizes—which also seems to hint at something.&lt;/p&gt;

&lt;p&gt;Bubble, Webflow, OutSystems, and Mendix began marketing to anyone who wanted it, claiming that "now any business can build its own app." In reality, it came down to standard tasks, forms, CRUD interfaces, and automations, with their own coaches, schools, tools, and support. However, as soon as non-standard logic, integration, or performance is needed, you have to build crutches out of blocks within the platform or hire a developer who knows the platform well enough to write "normal" code in C++, Java, Swift, or whatever language you prefer. So, surprise, surprise... yet another profession emerged. It turned out that the industry selling the dream of "you don't need a developer anymore" was also selling certificates saying "I am a developer for this thing" on the side for a modest profit. And then came the LLMs' turn...&lt;/p&gt;

&lt;p&gt;You're here, and this time things are really different from the previous ones: for the first time, the tool started working with arbitrary natural language rather than with a limited DSL made of blocks, diagrams, and special words. That is a very big leap comparable to COBOL/SQL/UML and the rest, and I say that without any irony. However, the fundamental limitations of ambiguity, verifiability, and reproducibility remain because they're inherent to the nature of the task, not the tool. Somebody still has to state the task precisely and in small portions, read the generated code, and answer for what that code is going to do in production at three in the morning.&lt;/p&gt;

&lt;p&gt;If you look back at this list, you'll see that every wave really did kill off some layer of manual work; but every wave also created a new layer of work on top of itself.&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%2Fexyt6lvh6jjt3vj1nbyx.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%2Fexyt6lvh6jjt3vj1nbyx.png" alt="1428_english_instead_of_code/image5.png" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Jevons paradox
&lt;/h2&gt;

&lt;p&gt;In 1865, William Stanley Jevons noticed that as steam engines became more efficient and required less coal to produce a unit of work, more coal was consumed overall. The now cheaper unit of work opened up many new applications, increasing the total volume of work needed.&lt;/p&gt;

&lt;p&gt;Every time a tool makes writing code cheaper, demand for software grows faster than its production cost falls. That is how the number of programmers grew from a few thousand in the early fifties to hundreds of thousands in the nineties, from millions in the two thousands to tens of millions today, despite the fact that a modern developer has a framework, an IDE, a package manager, and search engines...&lt;/p&gt;

&lt;p&gt;Sometimes, they accidentally ship in a single day as much as their colleague from the last century would write over days and months because the productivity of an average programmer has grown by orders of magnitude. Their workload has grown as well because cheap code has made worthwhile projects that businesses wouldn't have dared to start before. So, if your intuition tells you, "The tool does the work for me, so there 'll be less work", Jevons has some bad news for you, and it's a hundred and sixty years old.&lt;/p&gt;

&lt;h2&gt;
  
  
  What actually changes
&lt;/h2&gt;

&lt;p&gt;However, none of the above means that nothing changes—it does, and quite a lot, just not in the direction we can see. The bottom of the market is going away: boilerplate code, simple CRUD systems, markup, and boilerplate generation—all the work where the solution is already there and just needs to be typed out. This is where AI truly shines.&lt;/p&gt;

&lt;p&gt;But the top is growing: designing systems, understanding the domain, debugging, performance optimization (one of the areas where even the almighty Claude floats like an axe and sh*tclaudes away, so you barely keep up rejecting the commits), security, and embedding components into systems that must work predictably. Somebody has to check what the generator's generator generated with a generator, right? And that somebody has to understand the System—no, not even the Code...—more deeply than the one who generated it, because the one who generated it runs into Rice's theorem and Kamp's work.&lt;/p&gt;

&lt;p&gt;You could say that the limits of what machines can do are shifting quickly, but the limits of what businesses need are shifting even faster. Programmers don't disappear in this environment, but the composition of their work changes: there's less manual typing now, more thinking, and you're still responsible for everything.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is programming immortal?
&lt;/h2&gt;

&lt;p&gt;What keeps programming languages afloat isn't syntax, developers' habits, or the industry's resistance to change—although there's plenty of resistance, too. Programming languages solve tasks that natural languages (regardless of the tool used) are fundamentally unsuited for neither as diagrams, nor as md files, plans, or dialogues with Claude. A programming language unambiguously describes the behavior of systems that must work predictably.&lt;/p&gt;

&lt;p&gt;As long as there are systems for which "approximately right" means losing profit, time, or taking damage—and that includes most banks, all of medicine, all of aviation, and possibly most ordinary businesses—a formal way to describe exactly what they must do will be needed. That formal way is the programming language, however it may look by 2050, whether it's text, voice, or a thought channeled straight out of your head.&lt;/p&gt;

&lt;p&gt;A machine that accepts "I want a million dollars" and magically delivers is still a science-fiction plot from the beginning of the last century rather than engineering reality, because somebody will have to tell the machine where that money comes from, in which currency, which account to transfer it to, and what to do if, instead of money, some lads in short jackets turn up. That somebody is the programmer, except for the case with the lads, regardless of whether they manually type &lt;code&gt;transfer(amount, account)&lt;/code&gt;, chat with a bot, or narrate it to an assistant by voice.&lt;/p&gt;

&lt;p&gt;I'm leading you to a conclusion about what has always been the foundation of the programming profession. Vibe coding didn't eliminate the need to state the task precisely; it merely shifted the point at which you pay the price for imprecise wording. So, once again, the problem hasn't been solved, and it can't be solved at all. However, as usual, we can push it up to the level of a technical person, only now they will be called something else: Senior Opus Engineer, for example. In about ten years, someone on Habr will write an article about the origin of this new profession and its salary band. Agreed?&lt;/p&gt;

&lt;h2&gt;
  
  
  Want to learn more?
&lt;/h2&gt;

&lt;p&gt;The PVS-Studio team loves to publish valuable content for developers. &lt;a href="https://pvs-studio.com/en/subscribe/" rel="noopener noreferrer"&gt;Subscribe to our newsletter&lt;/a&gt; to follow the articles from the PVS-Studio website.&lt;/p&gt;

&lt;p&gt;Here are some articles:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1423/" rel="noopener noreferrer"&gt;Pitfalls of bit fields in C++&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/java/1417/" rel="noopener noreferrer"&gt;I found some bugs in Create mod and checked them in Minecraft&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/csharp/1411/" rel="noopener noreferrer"&gt;What popular ORMs for C# hide: Let's check RepoDB and SqlSugar&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/js/1418/" rel="noopener noreferrer"&gt;How we made JavaScript analyzer understand control flow&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/go/1419/" rel="noopener noreferrer"&gt;When copy-paste goes wrong: Bugs in structurally isomorphic Go code&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>softwaredevelopment</category>
      <category>vibecoding</category>
    </item>
    <item>
      <title>Classic C and errors of GmSSL project</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Tue, 17 Nov 2026 13:31:45 +0000</pubDate>
      <link>https://dev.to/pvsdev/classic-c-and-errors-of-gmssl-project-36d2</link>
      <guid>https://dev.to/pvsdev/classic-c-and-errors-of-gmssl-project-36d2</guid>
      <description>&lt;p&gt;While AI is discovering new ways to make mistakes, human developers stay true to the classics: typos and memory errors. Let's take a look at 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%2Felnfrv9vc3gsscmx8xgd.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%2Felnfrv9vc3gsscmx8xgd.png" alt="1427_GmSSL/image1.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One of our readers suggested that we check the &lt;a href="https://github.com/guanzhi/GmSSL" rel="noopener noreferrer"&gt;GmSSL&lt;/a&gt; project. Since we rarely cover projects from China, we decided to spend some time exploring this one.&lt;/p&gt;

&lt;p&gt;GmSSL is an open-source cryptographic library written in C and developed by Peking University. It fully implements China's enterprise cryptographic standards (SM2/SM3/SM4/SM9) and secure communication protocols (TLCP, TLS 1.2/1.3). The library is extremely lightweight and even works in resource-constrained embedded systems that don't support dynamic memory allocation. It also supports SDF/SKF hardware security modules.&lt;/p&gt;

&lt;p&gt;We didn't find any particularly critical or unusual bugs there. Instead, we encountered some of the most common and familiar types of errors. However, even though they're "standard," bugs are still bugs. Let's take a look.&lt;/p&gt;

&lt;h2&gt;
  
  
  Potential null pointer dereference
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;tls12_select_parameters&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;TLS_CONNECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;common_cipher_suites&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;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;cert_chains&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;cert_chains&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;cert_chains_len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;cert_chains_len&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;cert_chain_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="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;common_cipher_suites&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;common_cipher_suites_cnt&lt;/span&gt;
    &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;common_supported_groups&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;common_supported_groups_cnt&lt;/span&gt;
    &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;common_signature_algorithms&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;common_signature_algorithms_cnt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;error_print&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="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="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/v595/" rel="noopener noreferrer"&gt;V595&lt;/a&gt; The 'conn' pointer was utilized before it was verified against nullptr. Check lines: 807, 811. tls12.c 807&lt;/p&gt;

&lt;p&gt;Because of the &lt;code&gt;conn&lt;/code&gt; pointer check, we can assume that this pointer may be &lt;code&gt;NULL&lt;/code&gt;. However, the pointer is dereferenced before the check:&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="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;cert_chains&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;cert_chains&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="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;conn&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The input argument check should be placed at the beginning of the function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Potential memory leaks
&lt;/h2&gt;

&lt;p&gt;In C, manual memory management often leads to cases where developers forget to free memory in internal error handlers. GmSSL is not unique here.&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="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;test_sm4_ctr_test_vectors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&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;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;plaintext&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="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;plaintext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
         &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nb"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tests&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="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;plaintext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;error_print&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="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="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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ciphertext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
         &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nb"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tests&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="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ciphertext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;error_print&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="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="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/v773/" rel="noopener noreferrer"&gt;V773&lt;/a&gt; The function was exited without releasing the 'plaintext' pointer. A memory leak is possible. sm4_ctrtest.c 101&lt;/p&gt;

&lt;p&gt;If the second call to &lt;code&gt;malloc&lt;/code&gt; fails, the function exits without first freeing the memory that &lt;code&gt;plaintext&lt;/code&gt; points to. In other words, the memory previously allocated for another buffer isn't freed.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;
  Here are some other code snippets where memory leaks may occur:
  &lt;ol&gt;
&lt;li&gt;V773 The function was exited without releasing the 'aad' pointer. A memory leak is possible. sm4_ccmtest.c 128&lt;/li&gt;
&lt;li&gt;V773 The function was exited without releasing the 'encrypted' pointer. A memory leak is possible. sm4_ctrtest.c 115&lt;/li&gt;
&lt;li&gt;V773 The function was exited without releasing the 'decrypted' pointer. A memory leak is possible. sm4_ctrtest.c 123&lt;/li&gt;
&lt;li&gt;V773 The function was exited without releasing the 'plaintext' pointer. A memory leak is possible. sm4_cbctest.c 124&lt;/li&gt;
&lt;li&gt;V773 The function was exited without releasing the 'plaintext' pointer. A memory leak is possible. sm4_ofbtest.c 113&lt;/li&gt;
&lt;li&gt;V773 The function was exited without releasing the 'encrypted' pointer. A memory leak is possible. sm4_ofbtest.c 127&lt;/li&gt;
&lt;li&gt;V773 The function was exited without releasing the 'decrypted' pointer. A memory leak is possible. sm4_ofbtest.c 135&lt;/li&gt;
&lt;li&gt;V773 The function was exited without releasing the 'plaintext' pointer. A memory leak is possible. sm4_cfbtest.c 122&lt;/li&gt;
&lt;li&gt;V773 The function was exited without releasing the 'plaintext' pointer. A memory leak is possible. sm4_xtstest.c 115&lt;/li&gt;
&lt;li&gt;V773 The function was exited without releasing the 'encrypted' pointer. A memory leak is possible. sm4_xtstest.c 129&lt;/li&gt;
&lt;li&gt;V773 The function was exited without releasing the 'decrypted' pointer. A memory leak is possible. sm4_xtstest.c 137&lt;/li&gt;
&lt;li&gt;V773 The function was exited without releasing the 'plaintext' pointer. A memory leak is possible. sm4_ccmtest.c 132&lt;/li&gt;
&lt;li&gt;V773 The function was exited without releasing the 'encrypted' pointer. A memory leak is possible. sm4_ccmtest.c 148&lt;/li&gt;
&lt;li&gt;V773 The function was exited without releasing the 'decrypted' pointer. A memory leak is possible. sm4_ccmtest.c 155&lt;/li&gt;
&lt;/ol&gt;



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

&lt;h2&gt;
  
  
  Typos
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;skfutil_main&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;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;argv&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;ret&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="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="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="nf"&gt;strcmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"-lib"&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;// &amp;lt;=&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;argc&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;goto&lt;/span&gt; &lt;span class="n"&gt;bad&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;lib&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;argv&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="nf"&gt;strcmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"-lib"&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;// &amp;lt;=&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;argc&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;goto&lt;/span&gt; &lt;span class="n"&gt;bad&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;devname&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;argv&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="nf"&gt;strcmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"-devinfo"&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;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/v517/" rel="noopener noreferrer"&gt;V517&lt;/a&gt; The use of 'if (A) {...} else if (A) {...}' pattern was detected. There is a probability of logical error presence. Check lines: 85, 88. skfutil.c 85&lt;/p&gt;

&lt;p&gt;The two branches that check for the &lt;code&gt;-lib&lt;/code&gt; flag perform different actions. This is clearly an error because the second branch can never execute. Instead, it should check for the &lt;code&gt;-dev&lt;/code&gt; flag.&lt;/p&gt;

&lt;p&gt;Let's look at another typo: a duplicated &lt;code&gt;goto&lt;/code&gt; statement.&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;int&lt;/span&gt; &lt;span class="nf"&gt;sdfencrypt_main&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;argc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;argv&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;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="nf"&gt;strcmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"-help"&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="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"usage: gmssl %s %s\n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;prog&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;usage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"%s\n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&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;ret&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;goto&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;goto&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;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="nf"&gt;strcmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"-lib"&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;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/v779/" rel="noopener noreferrer"&gt;V779&lt;/a&gt; Unreachable code detected. It is possible that an error is present. sdfencrypt.c 86&lt;/p&gt;

&lt;p&gt;In this case, the typo has no consequences. However, this doesn't mean such typos are always harmless: CVE-2014-1266 — &lt;a href="https://queue.acm.org/doi/10.1145/2620660.2620662" rel="noopener noreferrer"&gt;Finding More Than One Worm in the Apple: If you see something, say something&lt;/a&gt; comes to mind.&lt;/p&gt;

&lt;p&gt;Here's another analyzer warning: V779 Unreachable code detected. It is possible that an error is present. sdfdecrypt.c 86&lt;/p&gt;

&lt;h2&gt;
  
  
  The failure to erase sensitive data
&lt;/h2&gt;

&lt;p&gt;Cryptographic libraries usually delete data after using it to prevent sensitive information from being compromised. Developers often use &lt;code&gt;memset&lt;/code&gt; for this purpose, which is a mistake. An optimizing compiler may determine that the program never uses the memory again after the &lt;code&gt;memset&lt;/code&gt; call and remove the call entirely. Learn more here: &lt;a href="https://cwe.mitre.org/data/definitions/14.html" rel="noopener noreferrer"&gt;CWE-14: Compiler Removal of Code to Clear Buffers&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let's look at the shortest possible function that contains this error:&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="nf"&gt;sha224_finish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;SHA224_CTX&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ctx&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;char&lt;/span&gt; &lt;span class="n"&gt;dgst&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;SHA224_DIGEST_SIZE&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="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;SHA256_DIGEST_SIZE&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;sha256_finish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;SHA256_CTX&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nb"&gt;memcpy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dgst&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SHA224_DIGEST_SIZE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nb"&gt;memset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buf&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;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buf&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/v597/" rel="noopener noreferrer"&gt;V597&lt;/a&gt; The compiler could delete the 'memset' function call, which is used to flush 'buf' buffer. The memset_s() function should be used to erase the private data. sha2.c 196&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;
  Here are other functions that incorrectly clear memory:
  &lt;ol&gt;
&lt;li&gt;V597 The compiler could delete the 'memset' function call, which is used to flush 'buf' buffer. The memset_s() function should be used to erase the private data. sha2.c 380&lt;/li&gt;
&lt;li&gt;V597 The compiler could delete the 'memset' function call, which is used to flush 'buf' buffer. The memset_s() function should be used to erase the private data. sha2.c 402&lt;/li&gt;
&lt;li&gt;V597 The compiler could delete the 'memset' function call, which is used to flush 'buf' buffer. The memset_s() function should be used to erase the private data. sha2.c 423&lt;/li&gt;
&lt;li&gt;V597 The compiler could delete the 'memset' function call, which is used to flush 'dgst' buffer. The memset_s() function should be used to erase the private data. sm3_kdf.c 52&lt;/li&gt;
&lt;li&gt;V597 The compiler could delete the 'memset' function call, which is used to flush 'ctx' object. The memset_s() function should be used to erase the private data. sm3_pbkdf2.c 142&lt;/li&gt;
&lt;li&gt;V597 The compiler could delete the 'memset' function call, which is used to flush 'key_block' buffer. The memset_s() function should be used to erase the private data. sm3_pbkdf2.c 143&lt;/li&gt;
&lt;li&gt;V597 The compiler could delete the 'memset' function call, which is used to flush 'tmp_block' buffer. The memset_s() function should be used to erase the private data. sm3_pbkdf2.c 144&lt;/li&gt;
&lt;li&gt;V597 The compiler could delete the 'memset' function call, which is used to flush 'ctx' object. The memset_s() function should be used to erase the private data. sm2_enc.c 56&lt;/li&gt;
&lt;li&gt;V597 The compiler could delete the 'memset' function call, which is used to flush 'dgst' buffer. The memset_s() function should be used to erase the private data. sm2_enc.c 57&lt;/li&gt;
&lt;li&gt;V597 The compiler could delete the 'memset' function call, which is used to flush 'buf' buffer. The memset_s() function should be used to erase the private data. sm3.c 210&lt;/li&gt;
&lt;li&gt;V597 The compiler could delete the 'memset' function call, which is used to flush 'sm3_ctx' object. The memset_s() function should be used to erase the private data. sm3.c 213&lt;/li&gt;
&lt;li&gt;V597 The compiler could delete the 'memset' function call, which is used to flush 'buf' buffer. The memset_s() function should be used to erase the private data. sm3_hmac.c 164&lt;/li&gt;
&lt;li&gt;V597 The compiler could delete the 'memset' function call, which is used to flush 'buf' buffer. The memset_s() function should be used to erase the private data. ghash.c 178&lt;/li&gt;
&lt;li&gt;V597 The compiler could delete the 'memset' function call, which is used to flush 'ghash_ctx' object. The memset_s() function should be used to erase the private data. ghash.c 181&lt;/li&gt;
&lt;li&gt;V597 The compiler could delete the 'memset' function call, which is used to flush 'buf' buffer. The memset_s() function should be used to erase the private data. sdfdigest.c 244&lt;/li&gt;
&lt;li&gt;V597 The compiler could delete the 'memset' function call, which is used to flush 'sm3_ctx' object. The memset_s() function should be used to erase the private data. sm3_kdf.c 51&lt;/li&gt;
&lt;/ol&gt;



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

&lt;h2&gt;
  
  
  The error of creating a 64-bit mask
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;sm2_z256_get_booth&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;sm2_z256_t&lt;/span&gt; &lt;span class="n"&gt;a&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;window_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;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;mask&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;window_size&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="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/v629/" rel="noopener noreferrer"&gt;V629&lt;/a&gt; Consider inspecting the '1 &amp;lt;&amp;lt; window_size' expression. Bit shifting of the 32-bit value with a subsequent expansion to the 64-bit type. sm2_z256.c 336&lt;/p&gt;

&lt;p&gt;The function generates a 64-bit mask. However, it's impossible to set the most significant bits since the &lt;code&gt;1&lt;/code&gt; numeric literal is of the &lt;code&gt;int&lt;/code&gt; type. An overflow will occur. 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="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;mask&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1ui64&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;window_size&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;Here's a similar warning: V629 Consider inspecting the '1 &amp;lt;&amp;lt; window_size' expression. Bit shifting of the 32-bit value with a subsequent expansion to the 64-bit type. sm9_z256.c 344&lt;/p&gt;

&lt;h2&gt;
  
  
  A logic error
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;tls13_process_client_pre_shared_key_from_ticket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="no"&gt;TLS_CONNECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ext_data&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;ext_datalen&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;ret&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="k"&gt;for&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;identitieslen&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="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;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;ret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;tls13_psk_binder_verify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;pre_shared_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pre_shared_key&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;amp;&lt;/span&gt;&lt;span class="n"&gt;dgst_ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;binder&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;binderlen&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="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="nf"&gt;error_print&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="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="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="n"&gt;ret&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;continue&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;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/v547/" rel="noopener noreferrer"&gt;V547&lt;/a&gt; Expression 'ret == 0' is always false. tls_psk.c 1572&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;continue&lt;/code&gt; statement is never executed because the &lt;code&gt;ret&lt;/code&gt; variable always equals &lt;code&gt;1&lt;/code&gt; here. The code has been shortened for readability:&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;foo&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="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="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;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="n"&gt;ret&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;// ret equals 1 here&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code contains a logic error or a typo.&lt;/p&gt;

&lt;p&gt;There are two other similar errors:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;V547 Expression 'ret == 0' is always false. tls_psk.c 1897&lt;/li&gt;
&lt;li&gt;V547 Expression 'ret == 0' is always false. tlcp.c 2042&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  An uninitialized variable is used
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;tls12_cert_chains_select&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;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="k"&gt;for&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;cert_chains_len&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="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;cert_chain&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;cert_chain_len&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;sig_alg&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;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="nf"&gt;tls_uint24array_from_bytes&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;cert_chain&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;cert_chain_len&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;cert_chains&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;cert_chains_len&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="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;error_print&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="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="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;certs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;certs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cert_chain&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;certs_len&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;certs_len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cert_chain_len&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;certs_idx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;certs_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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prefered_sig_alg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;prefered_sig_alg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sig_alg&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// &amp;lt;=&lt;/span&gt;
    &lt;span class="k"&gt;return&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="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/v614/" rel="noopener noreferrer"&gt;V614&lt;/a&gt; Uninitialized variable 'sig_alg' used. tls_cert.c 623&lt;/p&gt;

&lt;p&gt;The code uses the &lt;code&gt;sig_alg&lt;/code&gt; uninitialized variable. It looks like the logic necessary to update this variable's value is missing.&lt;/p&gt;

&lt;p&gt;P.S. GmSSL's unit tests often use buffers containing uninitialized data. In other words, they perform various operations on garbage data. I won't judge whether this is a sound approach, since the developers clearly chose it deliberately. Still, such unit-test code "smells", so I decided to mention it in the article.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;
  This is an example that shows how to carefully use an uninitialized array:
  &lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define GETU32(p) \&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint32_t&lt;/span&gt;&lt;span class="p"&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="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;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; \
   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint32_t&lt;/span&gt;&lt;span class="p"&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="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="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; \
   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint32_t&lt;/span&gt;&lt;span class="p"&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="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;  &lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; \
   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint32_t&lt;/span&gt;&lt;span class="p"&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="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="cp"&gt;#define PUTU32(p,V) \&lt;/span&gt;
  &lt;span class="p"&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="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="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint8_t&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="no"&gt;V&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; \
   &lt;span class="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="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="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint8_t&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="no"&gt;V&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;16&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;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint8_t&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="no"&gt;V&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;  &lt;span class="mi"&gt;8&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;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;V&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sm4_encrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;SM4_KEY&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="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;16&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="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="no"&gt;X0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;X1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;X2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;X3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;X4&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;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="no"&gt;X0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;GETU32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;in&lt;/span&gt;     &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="no"&gt;X1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;GETU32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;  &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="no"&gt;X2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;GETU32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;in&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="p"&gt;;&lt;/span&gt;
  &lt;span class="no"&gt;X3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;GETU32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;12&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="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="mi"&gt;32&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="no"&gt;X4&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;X1&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="no"&gt;X2&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="no"&gt;X3&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;rk&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="p"&gt;;&lt;/span&gt;
    &lt;span class="no"&gt;X4&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;S32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;X4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="no"&gt;X4&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;X0&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="no"&gt;L32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;X4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="no"&gt;X0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;X1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="no"&gt;X1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;X2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="no"&gt;X2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;X3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="no"&gt;X3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;X4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="no"&gt;PUTU32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;out&lt;/span&gt;     &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;X3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="no"&gt;PUTU32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;  &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;X2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="no"&gt;PUTU32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;out&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="no"&gt;X1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="no"&gt;PUTU32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;X0&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="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;speed_sm4_encrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="no"&gt;SM4_KEY&lt;/span&gt; &lt;span class="n"&gt;sm4_key&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;16&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="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="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;]&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;nbytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;clock_t&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;end&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;seconds&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="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;sm4_set_encrypt_key&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;sm4_key&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="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;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;nbytes&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buf&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="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;sm4_encrypt&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;sm4_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;buf&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;= buf is uninitialized&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: V614 Uninitialized buffer 'buf' used. Consider checking the second actual argument of the 'sm4_encrypt' function. sm4test.c 199&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;V614 Uninitialized buffer 'data' used. Consider checking the third actual argument of the 'tls_record_set_application_data' function. tlstest.c 404&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'verify_data' used. Consider checking the third actual argument of the 'tls_record_set_handshake_finished' function. tlstest.c 342&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'sig' used. Consider checking the third actual argument of the 'tls_record_set_handshake_certificate_verify' function. tlstest.c 320&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'random' used. Consider checking the fourth actual argument of the 'tls_record_set_handshake_server_hello' function. tlstest.c 254&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'random' used. Consider checking the fourth actual argument of the 'tls_record_set_handshake_client_hello' function. tlstest.c 229&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'iv' used. Consider checking the third actual argument of the 'zuc_init' function. zuctest.c 508&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'key' used. Consider checking the second actual argument of the 'zuc_init' function. zuctest.c 508&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'iv' used. Consider checking the third actual argument of the 'zuc_init' function. zuctest.c 480&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'key' used. Consider checking the second actual argument of the 'zuc_init' function. zuctest.c 480&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'blocks' used. Consider checking the second actual argument of the 'ghash_update' function. ghashtest.c 169&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'msg' used. Consider checking the second actual argument of the 'hss_sign_update' function. lmstest.c 895&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'msg' used. Consider checking the second actual argument of the 'hss_sign_update' function. lmstest.c 845&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'msg' used. Consider checking the second actual argument of the 'hss_sign_update' function. lmstest.c 796&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'msg' used. Consider checking the second actual argument of the 'lms_sign_update' function. lmstest.c 417&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'serial_buf' used. Consider checking the eighth actual argument of the 'cms_signer_infos_add_signer_info' function. cmstest.c 494&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'sig_buf' used. Consider checking the tenth actual argument of the 'cms_signer_info_to_der' function. cmstest.c 331&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'serial_buf' used. Consider checking the fourth actual argument of the 'cms_signer_info_to_der' function. cmstest.c 327&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'c3' used. Consider checking the fourth actual argument of the 'sm9_ciphertext_to_der' function. sm9test.c 858&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'c2' used. Consider checking the second actual argument of the 'sm9_ciphertext_to_der' function. sm9test.c 858&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer '(uint8_t *) buf' used. Consider checking the sixth actual argument of the 'sm4_ccm_encrypt' function. sm4_ccmtest.c 382&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'aad' used. Consider checking the fourth actual argument of the 'sm4_ccm_encrypt' function. sm4_ccmtest.c 382&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'iv' used. Consider checking the second actual argument of the 'sm4_ccm_encrypt' function. sm4_ccmtest.c 382&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'msg' used. Consider checking the second actual argument of the 'x509_sign_update' function. x509_keytest.c 434&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'attrs' used. Consider checking the fifth actual argument of the 'x509_req_sign_to_der' function. x509_reqtest.c 161&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'attrs_buf' used. Consider checking the fifth actual argument of the 'x509_request_info_to_der' function. x509_reqtest.c 52&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'iv' used. Consider checking the seventh actual argument of the 'pbes2_algor_to_der' function. pkcs8test.c 234&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'iv' used. Consider checking the seventh actual argument of the 'pbes2_params_to_der' function. pkcs8test.c 185&lt;/li&gt;
&lt;li&gt;V614 Uninitialized variable 'saltlen' used. Consider checking the sixth actual argument of the 'format_bytes' function. pkcs8test.c 115&lt;/li&gt;
&lt;li&gt;V614 Uninitialized pointer 'psalt' used. Consider checking the fifth actual argument of the 'format_bytes' function. pkcs8test.c 115&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'plaintext' used. Consider checking the second actual argument of the 'sm2_encrypt_update' function. sm2_enctest.c 338&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'msg' used. Consider checking the second actual argument of the 'sm2_sign_update' function. sm2_signtest.c 196&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer '(uint8_t *) buf' used. Consider checking the sixth actual argument of the 'sm4_gcm_encrypt' function. sm4_gcmtest.c 547&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'aad' used. Consider checking the fourth actual argument of the 'sm4_gcm_encrypt' function. sm4_gcmtest.c 547&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'iv' used. Consider checking the second actual argument of the 'sm4_gcm_encrypt' function. sm4_gcmtest.c 547&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'blocks' used. Consider checking the second actual argument of the 'sm3_update' function. sm3test.c 189&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'buf1' used. Consider checking the third actual argument of the 'sm4_cbc_padding_encrypt' function. sm4_cbctest.c 169&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer '(uint8_t *) buf' used. Consider checking the third actual argument of the 'sm4_ctr32_encrypt_blocks' function. sm4test.c 336&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer '(uint8_t *) buf' used. Consider checking the third actual argument of the 'sm4_ctr_encrypt_blocks' function. sm4test.c 308&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer '(uint8_t *) buf' used. Consider checking the third actual argument of the 'sm4_cbc_decrypt_blocks' function. sm4test.c 280&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer '(uint8_t *) buf' used. Consider checking the third actual argument of the 'sm4_cbc_encrypt_blocks' function. sm4test.c 253&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer '(uint8_t *) buf' used. Consider checking the second actual argument of the 'sm4_encrypt_blocks' function. sm4test.c 225&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'attrs' used. Consider checking the fifth actual argument of the 'x509_req_sign_to_der' function. reqgen.c 245&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'data' used. Consider checking the second actual argument of the 'cms_content_info_to_der' function. cms.c 2451&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'data' used. Consider checking the second actual argument of the 'cms_content_info_to_der' function. cms.c 2349&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'data' used. Consider checking the second actual argument of the 'cms_content_info_to_der' function. cms.c 2250&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'data' used. Consider checking the second actual argument of the 'cms_content_info_to_der' function. cms.c 2167&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'data' used. Consider checking the second actual argument of the 'cms_content_info_to_der' function. cms.c 2099&lt;/li&gt;
&lt;li&gt;V614 Uninitialized buffer 'data' used. Consider checking the second actual argument of the 'cms_content_info_to_der' function. cms.c 2064&lt;/li&gt;
&lt;/ol&gt;



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

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

&lt;p&gt;We already know how people make mistakes. Download and use &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=1427" rel="noopener noreferrer"&gt;PVS-Studio&lt;/a&gt; to catch such errors and potential vulnerabilities as early as possible.&lt;/p&gt;

&lt;p&gt;We're &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1402/" rel="noopener noreferrer"&gt;still learning&lt;/a&gt; how AI makes mistakes, but we're starting to see where to take our analyzer next. One promising direction, for example, is detecting duplicate and similar code. Either way, PVS-Studio can already help you check AI-generated code.&lt;/p&gt;

</description>
      <category>c</category>
      <category>programming</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Terrible tips for a C++ developer: 2026 edition, Part 1</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Fri, 13 Nov 2026 13:46:34 +0000</pubDate>
      <link>https://dev.to/pvsdev/terrible-tips-for-a-c-developer-2026-edition-part-1-507c</link>
      <guid>https://dev.to/pvsdev/terrible-tips-for-a-c-developer-2026-edition-part-1-507c</guid>
      <description>&lt;p&gt;We're introducing an updated edition of the terrible tips for a C++ developer book. We'll be posting it on our blog in parts. The book includes a total of 60 tips.&lt;/p&gt;

&lt;p&gt;Each part will feature a list of "tips" that developers really shouldn't follow. Each tip comes with an explanation of why it's bad, as well as a breakdown of its less obvious aspects. This series is designed to help beginner programmers learn the ropes, while entertaining experienced developers with some of the more unexpected C++ quirks.&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%2Fedcae0f8vk5763cl9hfj.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%2Fedcae0f8vk5763cl9hfj.png" alt="1426_terrible_tips_pt1/image1.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;The first edition of the book came out in 2023. Three years later, I decided that the time had come for an updated edition. I've fixed errors, expanded or rewritten some chapters, and added new topics I'd like to cover, such as secure software development (SSD) and AI-generated code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How did the book appear?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Originally, I wanted to write a book about techniques for creating high-quality code. I wanted to cover topics such as code formatting and what to look for during code reviews. I also wished to discuss which new C++ features developers should definitely adopt and which ones might be questionable. However, I got caught up in another topic by accident and decided to go with the flow.&lt;/p&gt;

&lt;p&gt;It all started with a humorous post on Habr, where I came up with a few pieces of bad advice and invited readers to contribute their own. The community liked the idea and shared lots of suggestions for writing truly terrible code. I picked some of those tips, added my own, and posted them in an article. I also explained some tips that beginners might not realise could be harmful. That's how the idea took shape and evolved into this book.&lt;/p&gt;

&lt;p&gt;I enjoyed writing about what not to do rather than how to do things right. It was a nice change of pace, if nothing else. I'm tired of writing traditional educational materials, particularly those on secure software development. Sometimes, I just want to let loose, add some humour and see where my thoughts take me.&lt;/p&gt;

&lt;p&gt;I've spent so many years studying software bugs and writing about how to avoid them. This is closely linked to my work on developing and promoting PVS-Studio, a static code analyzer.&lt;/p&gt;

&lt;p&gt;Static analyzers detect code fragments that are likely to contain bugs or violate a project's coding standards. PVS-Studio is one such tool. The tool's mascot, Cody the unicorn, will make a few more appearances throughout the book.&lt;/p&gt;

&lt;p&gt;I use open-source projects to show the types of bugs and potential vulnerabilities this methodology can uncover. Even more importantly, though, I try to explain how to write code that's free of such errors. In other words, I keep coming back to the same topics: how to properly format code, what makes a good comment, why code reviews matter, and so on.&lt;/p&gt;

&lt;p&gt;So, I decided to expand on the concept of terrible tips and turn it into a book. However, very little remains from the original list of bad advice, aside from the idea itself. I removed or replaced anything that was too obvious or uninteresting, and I added a lot of theory that will benefit both beginners and seasoned developers. The second edition also covers a wider range of topics.&lt;/p&gt;

&lt;p&gt;As you might expect, I learned what good and bad code looks like by developing PVS-Studio and studying the bugs it detected. This is why the analyzer will occasionally appear in various chapters. I've spent so much time working with static analysis that I immediately think of real-world bugs when I hear about certain topics. I can't resist sharing these examples to make my points more convincing. If they help illustrate the point, why not mention them?&lt;/p&gt;

&lt;p&gt;I wrote this book with love and respect for my readers, and I didn't use AI to generate the text. I hope you enjoy reading it!&lt;/p&gt;

&lt;h2&gt;
  
  
  Terrible tip N1. Only C++
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Real developers code only in C++!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There's nothing wrong with writing code in C++. There are many projects in the world written in C++. Take a look at &lt;a href="https://www.stroustrup.com/applications.html" rel="noopener noreferrer"&gt;this list from Bjarne Stroustrup's homepage&lt;/a&gt;, for example.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Here is a list of systems, applications, and libraries that are completely or mostly written in C++. Naturally, this is not intended to be a complete list. In fact, I couldn't list a 1000th of all major C++ programs if I tried, and this list holds maybe 1000th of the ones I have heard of. It is a list of systems, applications, and libraries that a reader might have some familiarity with, that might give a novice an idea what is being done with C++, or that I simply thought "cool".&lt;/p&gt;

&lt;p&gt;Bjarne Stroustrup&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It's bad when developers use C++ just because it's "cool" or because it's the only language the team knows.&lt;/p&gt;

&lt;p&gt;The variety of programming languages reflects the wide range of tasks software developers encounter. Different languages help developers solve different types of problems.&lt;/p&gt;

&lt;p&gt;C++ is a versatile programming language. However, being versatile doesn't guarantee that particular applications will be implemented quickly and easily. Some languages may be better suited to certain projects than others.&lt;/p&gt;

&lt;p&gt;It's fine for a team to develop a small utility in C++, even if another language would be more efficient. The time and effort required to learn a new language may be greater than the benefits it provides.&lt;/p&gt;

&lt;p&gt;However, it's a different story when the team needs to build a large project. At that point, it's worth taking a step back and asking: Is C++, a language we know well, really the most efficient choice for this project? Would another language be a better fit?&lt;/p&gt;

&lt;p&gt;If using another language would clearly be more efficient, it may make sense for the team to invest time in learning it. In the long run, this could greatly reduce development and maintenance costs. As an alternative, the team could hand the project over to another group that uses a more suitable language for the task.&lt;/p&gt;

&lt;p&gt;So, when should you choose C++? C and C++ have been the most successful languages for system programming tasks, such as operating systems, drivers, compilers, and DBMSs. They're also well-suited for environments with limited resources, such as memory and CPU time.&lt;/p&gt;

&lt;p&gt;When we talk about resource constraints, microcontrollers are probably the first thing that comes to mind. Yes, C++ is widely used in embedded systems and the IoT (Internet of Things).&lt;/p&gt;

&lt;p&gt;However, resource constraints can also take other forms. C++ is also used to develop applications for high-performance computing clusters with enormous amounts of memory. So, where's the catch? In this case, the constraints are the cost of the necessary hardware and the electricity required to power the data centers. Even a small improvement in code efficiency—just 5–20%—can result in significant savings.&lt;/p&gt;

&lt;p&gt;Another story that comes to mind is how Disney Studios reduced rendering time by developing its own C++ algorithms that used SIMD and specialized data structures.&lt;/p&gt;

&lt;p&gt;To sum up, C++ is an excellent language for building highly efficient applications. The trade-off is a steep learning curve and the necessity of strict discipline when writing code. Choose C++ when you genuinely need it.&lt;/p&gt;

&lt;p&gt;You might be tempted to argue that developers no longer need an in-depth knowledge of the language because generative AI (GenAI) can now write code for them. So, why not choose C++ and start generating fast code right away? With that, we come to our next terrible tip.&lt;/p&gt;

&lt;h2&gt;
  
  
  Terrible tip N2. Don't learn C++; command it with vibe coding
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;You no longer need an in-depth knowledge of programming languages. Thanks to GenAI, you can write efficient C++ applications without having to spend time learning the language.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Writing about GenAI is a thankless task. AI-driven development paradigms are evolving and changing so rapidly that something may already be outdated by the time I finish writing this book. And by the time it's printed, I'll probably wish I'd written this chapter differently. Still, I'll try to express an important idea that will survive the AI gold rush.&lt;/p&gt;

&lt;p&gt;Without understanding how the generated code works, developers risk falling into two traps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;They won't be able to tell efficient code from inefficient one.&lt;/li&gt;
&lt;li&gt;They'll encounter an error they can't fix. They may try to fix it by repeatedly regenerating the code, but that might not work either.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;While these problems can arise when working with any programming language, they're particularly pronounced in C++. C++'s high performance comes at the price of its complexity. Writing inefficient C++ code is like using a microscope to hammer a nail.&lt;/p&gt;

&lt;p&gt;In this chapter, we'll focus on the first point. The next chapter will take a closer look at errors and dangers of placing too much trust in AI-generated code.&lt;/p&gt;

&lt;p&gt;GenAI can create code that looks elegant and optimized. Is it really, though? Only a C++ expert with an in-depth understanding of the language is qualified to verify the soundness of the project's architecture, as well as the efficiency of its algorithms and code. Without such expertise, the project can quickly go off track.&lt;/p&gt;

&lt;p&gt;For example, let's look at a small function generated with Claude Opus. The comment above &lt;code&gt;IsSpanBlank&lt;/code&gt; promises that the code is structured to make the compiler's job easier when vectorizing it, for example, by using SSE instructions.&lt;/p&gt;

&lt;p&gt;The following example comes from the article "&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1366/" rel="noopener noreferrer"&gt;Let's check vibe code that acts like optimized C++ but is actually a mess&lt;/a&gt;". The article also provides other examples that demonstrate why developers need to manually check AI-generated code. Otherwise, code will become complex, bloated, and slow. I recommend taking a few minutes to read through it: it's entertaining and educational.&lt;/p&gt;

&lt;p&gt;The key to the optimization is that it handles multiple elements at once to see if they have space characters:&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;// Check if a span contains only blank characters&lt;/span&gt;
&lt;span class="c1"&gt;// (space, tab, \r, \n)&lt;/span&gt;
&lt;span class="c1"&gt;// SIMD-friendly: processes 8 bytes at a time&lt;/span&gt;
&lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nc"&gt;IsSpanBlank&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;data&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;len&lt;/span&gt;&lt;span class="p"&gt;)&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="c1"&gt;// Process 8 bytes at a time&lt;/span&gt;
  &lt;span class="k"&gt;for&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="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&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;i&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="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// For each byte, check if it's NOT a blank character&lt;/span&gt;
    &lt;span class="c1"&gt;// Blank chars: space(0x20), tab(0x09), \n(0x0A), \r(0x0D)&lt;/span&gt;
    &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;all_blank&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&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;size_t&lt;/span&gt; &lt;span class="n"&gt;j&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;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;c&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="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&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;data&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="n"&gt;j&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="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;is_blank&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;' '&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'\t'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
                       &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'\n'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'\r'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;all_blank&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;all_blank&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;is_blank&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="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;all_blank&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&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;// Handle remaining bytes&lt;/span&gt;
  &lt;span class="k"&gt;for&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="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;len&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="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;c&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="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&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;data&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="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;c&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s1"&gt;' '&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s1"&gt;'\t'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s1"&gt;'\n'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s1"&gt;'\r'&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="kc"&gt;false&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="kc"&gt;true&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 makes it seem like the AI knows what it's doing. The inner loop handles eight characters at a time and can be optimized using SIMD instructions. A separate simple loop handles the remaining characters.&lt;/p&gt;

&lt;p&gt;To me, it looked suspicious that the bytes are still being handled sequentially and that the nested loop doesn't add anything useful. I think this code is just as good as the simplest way to implement 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;bool&lt;/span&gt; &lt;span class="nc"&gt;IsSpanBlank_Simple&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;data&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;len&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;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;len&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="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;c&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="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&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;data&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="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;c&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s1"&gt;' '&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s1"&gt;'\t'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s1"&gt;'\n'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s1"&gt;'\r'&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="kc"&gt;false&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="kc"&gt;true&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;I'd write the code this way. Had I known this was a bottleneck and I absolutely had to optimize the code as much as possible, I would've looked for an intrinsic-based solution.&lt;/p&gt;

&lt;p&gt;I wouldn't try any other option, such as one with a nested loop. I know a thing or two about optimization, but not enough to dare write fancy code just to give the compiler a hint.&lt;/p&gt;

&lt;p&gt;One could say, "You're weak! Claude Opus did it— it came up with the smarter version!"&lt;/p&gt;

&lt;p&gt;Let's compare performance. My colleague Phillip, who's an expert on C++, conducted a small experiment with GCC and Clang:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We create the same dataset for both benchmarks: we generate 1,000,000 strings of random lengths, with arbitrary content, ranging from 1 to 127 characters. The calculations don't include set generation.&lt;/li&gt;
&lt;li&gt;We feed a dataset to the algorithm and determine whether each string contains spaces. We measure the total time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In both cases, the code is compiled with aggressive &lt;code&gt;-O3&lt;/code&gt; optimizations. As you can see, in both compilers, the simple function performs better than the one with the nested loop.&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%2Frevhow7phkv5mg3u1w1k.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%2Frevhow7phkv5mg3u1w1k.png" alt="1426_terrible_tips_pt1/image2.png" width="800" height="746"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Shall we specify a magic optimization key to better optimize the vibe code? That key doesn't exist. And even if it does, the compiler will generate an equally efficient version from simple code with a single loop, even without any hints.&lt;/p&gt;

&lt;p&gt;Along with my colleague, I also experimented with the two previously explored options and three new algorithm variants. I used the MSVC compiler. Let's take a look at the measurement results.&lt;/p&gt;

&lt;p&gt;First, someone suggested improving the code by replacing the original &lt;code&gt;||&lt;/code&gt; with &lt;code&gt;|&lt;/code&gt;. In our case, short-circuit evaluation only causes more harm than good. Here's an improved check:&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;is_blank&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;' '&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'\t'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'\n'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'\r'&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;Secondly, I asked DeepSeek. It suggested an optimization approach based on bitwise 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="kt"&gt;bool&lt;/span&gt; &lt;span class="nc"&gt;IsSpanBlank_Deepseek&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;data&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;len&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;noexcept&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Creating a bitmask for valid characters&lt;/span&gt;
  &lt;span class="c1"&gt;// Using a 128-bit mask for ASCII&lt;/span&gt;
  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;constexpr&lt;/span&gt; &lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;blank_mask_low&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="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kt"&gt;uint64_t&lt;/span&gt; &lt;span class="n"&gt;mask&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;mask&lt;/span&gt; &lt;span class="o"&gt;|=&lt;/span&gt; &lt;span class="mi"&gt;1ULL&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s1"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;mask&lt;/span&gt; &lt;span class="o"&gt;|=&lt;/span&gt; &lt;span class="mi"&gt;1ULL&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s1"&gt;'\t'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;mask&lt;/span&gt; &lt;span class="o"&gt;|=&lt;/span&gt; &lt;span class="mi"&gt;1ULL&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s1"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;mask&lt;/span&gt; &lt;span class="o"&gt;|=&lt;/span&gt; &lt;span class="mi"&gt;1ULL&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s1"&gt;'\r'&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;mask&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="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;len&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="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;uc&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="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&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;data&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="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;uc&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// All blank characters in ASCII &amp;lt; 64&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="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blank_mask_low&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;uc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&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="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&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="kc"&gt;true&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;Finally, I asked DeepSeek for an optimized function that truly takes advantage of vectorization. Here's what I got:&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="nc"&gt;IsSpanBlank_Deepseek_SSE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;data&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;len&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;noexcept&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Creating vectors with valid characters&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;__m128i&lt;/span&gt; &lt;span class="n"&gt;space&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;_mm_set1_epi8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;__m128i&lt;/span&gt; &lt;span class="n"&gt;tab&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;_mm_set1_epi8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'\t'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;__m128i&lt;/span&gt; &lt;span class="n"&gt;newline&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;_mm_set1_epi8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;__m128i&lt;/span&gt; &lt;span class="n"&gt;carriage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;_mm_set1_epi8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'\r'&lt;/span&gt;&lt;span class="p"&gt;)&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="c1"&gt;// Processing 16 bytes at a time&lt;/span&gt;
  &lt;span class="k"&gt;for&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="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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;i&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="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;__m128i&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="nf"&gt;_mm_loadu_si128&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;reinterpret_cast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;__m128i&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;data&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;// Checking that each byte matches one of the valid values&lt;/span&gt;
    &lt;span class="n"&gt;__m128i&lt;/span&gt; &lt;span class="n"&gt;eq_space&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;_mm_cmpeq_epi8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;space&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;__m128i&lt;/span&gt; &lt;span class="n"&gt;eq_tab&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;_mm_cmpeq_epi8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tab&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;__m128i&lt;/span&gt; &lt;span class="n"&gt;eq_nl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;_mm_cmpeq_epi8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;newline&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;__m128i&lt;/span&gt; &lt;span class="n"&gt;eq_cr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;_mm_cmpeq_epi8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;carriage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;__m128i&lt;/span&gt; &lt;span class="n"&gt;is_blank&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;_mm_or_si128&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;_mm_or_si128&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;eq_space&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;eq_tab&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                         &lt;span class="nf"&gt;_mm_or_si128&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;eq_nl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;eq_cr&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;// Obtaining the mask and checking it&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;mask&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;_mm_movemask_epi8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;is_blank&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;mask&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mh"&gt;0xFFFF&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&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;// Processing the remainder&lt;/span&gt;
  &lt;span class="k"&gt;for&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="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;len&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="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&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="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;c&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s1"&gt;' '&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s1"&gt;'\t'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s1"&gt;'\n'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s1"&gt;'\r'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&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="kc"&gt;true&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, the results. Although 32-bit builds are no longer relevant, I ran it just for the sake of curiosity. Testing was performed on a 212-megabyte array filled with random space characters. Thread execution is bound to a single preheated core.&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="nc"&gt;Win32&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="nc"&gt;The&lt;/span&gt; &lt;span class="n"&gt;simplest&lt;/span&gt; &lt;span class="n"&gt;option&lt;/span&gt; &lt;span class="n"&gt;with&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;single&lt;/span&gt; &lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;                          &lt;span class="mf"&gt;1.39&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="nc"&gt;The&lt;/span&gt; &lt;span class="n"&gt;original&lt;/span&gt; &lt;span class="s2"&gt;"optimized code"&lt;/span&gt; &lt;span class="n"&gt;with&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;nested&lt;/span&gt; &lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;                &lt;span class="mf"&gt;1.86&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="nc"&gt;The&lt;/span&gt; &lt;span class="n"&gt;original&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="n"&gt;but&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;operators&lt;/span&gt; &lt;span class="n"&gt;were&lt;/span&gt; &lt;span class="n"&gt;replaced&lt;/span&gt; &lt;span class="n"&gt;with&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.351&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="nc"&gt;Deepseek&lt;/span&gt;&lt;span class="s1"&gt;'s implementation with bitmask optimization:             0.817 s.
Deepseek'&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="n"&gt;implementation&lt;/span&gt; &lt;span class="n"&gt;with&lt;/span&gt; &lt;span class="no"&gt;SSE2&lt;/span&gt; &lt;span class="n"&gt;intrinsic&lt;/span&gt; &lt;span class="n"&gt;optimization&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;      &lt;span class="mf"&gt;0.0449&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

&lt;span class="nc"&gt;Win64&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="nc"&gt;The&lt;/span&gt; &lt;span class="n"&gt;simplest&lt;/span&gt; &lt;span class="n"&gt;option&lt;/span&gt; &lt;span class="n"&gt;with&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;single&lt;/span&gt; &lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;                          &lt;span class="mf"&gt;0.175&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="nc"&gt;The&lt;/span&gt; &lt;span class="n"&gt;original&lt;/span&gt; &lt;span class="s2"&gt;"optimized code"&lt;/span&gt; &lt;span class="n"&gt;with&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;nested&lt;/span&gt; &lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;                &lt;span class="mf"&gt;0.309&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="nc"&gt;The&lt;/span&gt; &lt;span class="n"&gt;original&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="n"&gt;but&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;operators&lt;/span&gt; &lt;span class="n"&gt;were&lt;/span&gt; &lt;span class="n"&gt;replaced&lt;/span&gt; &lt;span class="n"&gt;with&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.253&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="nc"&gt;Deepseek&lt;/span&gt;&lt;span class="s1"&gt;'s implementation with bitmask optimization:             0.207 s.
Deepseek'&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="n"&gt;implementation&lt;/span&gt; &lt;span class="n"&gt;with&lt;/span&gt; &lt;span class="no"&gt;SSE2&lt;/span&gt; &lt;span class="n"&gt;intrinsic&lt;/span&gt; &lt;span class="n"&gt;optimization&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;      &lt;span class="mf"&gt;0.0396&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Clearly, the 64-bit code is much more efficient, except when SSE2 is involved. Even then, though, everything runs quickly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The code Claude Opus generated for the markus project is the worst of all the options.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Not only is the simplest implementation with a regular loop faster, it's also shorter. The extra code lines only made things worse.&lt;/p&gt;

&lt;p&gt;Although intrinsic code is in the lead, this comes at the cost of limited portability.&lt;/p&gt;

&lt;p&gt;Some might say I'm worrying for nothing. Yes, Claude Opus chose a poor implementation, but DeepSeek offered a perfectly good, fast, and portable one.&lt;/p&gt;

&lt;p&gt;I agree. However, we see some poorly written code in the project. It looks optimized, but it isn't. The issue remains unresolved. This case requires an expert who understands it and can come up with a different solution. Knowing C++ well isn't enough to be one. It's also necessary to have experience writing and optimizing applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Terrible tip N3. Reading books is no longer relevant
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Reading fundamental books on C++ is a waste of time. Educational videos are enough, GenAI will help with everything else.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This chapter is for those who are just getting started with programming. Experienced developers can skip it. Or they can read it anyway and see how much it resonates with their own thoughts and experiences.&lt;/p&gt;

&lt;p&gt;The point of the previous chapter wasn't to discourage the use of GenAI. Instead, the focus is on understanding and controlling whether the generated code meets the necessary quality, reliability, and security standards. This brings us to the question of learning programming theory and gaining hands-on experience.&lt;/p&gt;

&lt;p&gt;The question of how to learn programming has always existed. The issue has become even more pressing today because of the illusion that programming technologies are evolving faster than ever before. In their articles, GenAI and vibe-coding enthusiasts openly claim that knowledge acquired just a year ago is obsolete and that developers should use AI agents differently. Maybe the problem is that what they had wasn't real knowledge to begin with.&lt;/p&gt;

&lt;p&gt;Fundamental knowledge of system design, OOP, programming languages, team management, and so on remains just as relevant as ever.&lt;/p&gt;

&lt;p&gt;However, all the hype surrounding GenAI may cause inexperienced developers to believe that truly investing in self-education is pointless since everything will become obsolete in just a year. Instead, they should focus on quickly learning the basics of whatever is new while GenAI successfully implements everything they could ever hope to.&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%2Fq3qum2vu8wlqv50d2gk5.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%2Fq3qum2vu8wlqv50d2gk5.png" alt="1426_terrible_tips_pt1/image3.png" width="800" height="582"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ensuring that everything works correctly and efficiently requires understanding of the process. Sometimes, it's optional: for example, when building an MVP or working on a pet project. However, I'm referring to professional software development, where tasks and their complexity extend well beyond the scope of quickly generating some code. GenAI enthusiasts who write articles about it often don't even understand the problems we're dealing with. They see the code and think, "There it is. What more could you want?" I like this quote that captures the idea:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"And that, in my opinion, is one of the main problems right now. Those who don't fully understand programming are now confidently telling everyone else how to do it."&lt;/p&gt;

&lt;p&gt;Mikhail Larchenko.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For anyone getting started in programming, my advice is to learn and practice writing code on your own. These skills are necessary regardless of whether the code is AI-generated or written by a human developer. That distinction doesn't matter. What matters is having experts who can take responsibility for the results!&lt;/p&gt;

&lt;p&gt;What pitfalls await those who want to learn programming today?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When studying theory: the short-form video format that the social media era has made so prevalent.&lt;/li&gt;
&lt;li&gt;When practicing: the temptation to turn to GenAI right away.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Theory
&lt;/h3&gt;

&lt;p&gt;I'm all for reading books on programming. However, I can't downplay the benefits of tutorial videos and other learning methods. I'd enjoy watching a talk from a C++ conference.&lt;/p&gt;

&lt;p&gt;The problem lies in the growing preference for video-based content. I've noticed that reading programming books has gone out of fashion.&lt;/p&gt;

&lt;p&gt;Educational videos have two major drawbacks:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;They don't encourage you to stop and reflect on what you've seen and heard. You can pause or rewind, but neither feels as convenient or natural as reading a book. When studying C++ from books, I would often reread paragraphs or go through example code again, taking my time. In other words, I'm talking about attentive and thoughtful reading. You can use videos to achieve the same results, but it doesn't work quite the same way. You just keep watching, even when you haven't fully understood something. Sometimes, you even speed up the video.&lt;/li&gt;
&lt;li&gt;Unlike books, educational videos lack a comprehensive approach to information. I can't imagine how anyone could fit the same amount of knowledge into a video in an accessible way as, for example, Bjarne Stroustrup presents across 1,200 pages in "The C++ Programming Language" (4th edition).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If approached with the same attention to detail as books, a video would look something like this...&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%2Fxvflia1w2a57s0f9qgig.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%2Fxvflia1w2a57s0f9qgig.png" alt="1426_terrible_tips_pt1/image4.png" width="800" height="564"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Videos are a demanding format to learn from. In my opinion, they're most effective when used to reinforce existing knowledge. They're useful when they build on a solid foundation acquired through books. Otherwise, they create a fragmented illusion of knowledge. A developer may appear to know their way around programming, but these kinds of knowledge gaps will eventually catch up with them and their employer.&lt;/p&gt;

&lt;p&gt;Even before the GenAI era, I've encountered programmers like this. At first glance, they seem incredibly talented. They can quickly build something at a hackathon using trendy libraries or create something stunning with a game engine. It all looks like magic, especially if you've never worked with those libraries yourself. However, this illusion quickly fades when they struggle with a real-world task because they're unaware of basic algorithms or the intricacies of their programming language. Now, in the age of GenAI, things are only going to get worse.&lt;/p&gt;

&lt;p&gt;So, what's the problem if someone doesn't know everything, yet still manages to get things done? The problem is inefficiency and limited capabilities. A lack of knowledge can lead people to adopt strange, highly inefficient approaches. They may rely on vibe coding or search Stack Overflow for ready-made solutions. However, sooner or later, they'll reach a point where they can't solve the problems they're facing. The line isn't visible, but it's there.&lt;/p&gt;

&lt;p&gt;By the way, I ask candidates during job interviews which C++ programming books they've read. Not having read any books doesn't automatically disqualify someone, but it does raise a red flag.&lt;/p&gt;

&lt;h3&gt;
  
  
  Practice
&lt;/h3&gt;

&lt;p&gt;A beginner who uses GenAI to write code from day one skips the gradual learning process from syntax to architecture. They get a working result right away without first understanding why one approach works better than another. They neither learn from their mistakes nor reflect on their actions.&lt;/p&gt;

&lt;p&gt;Getting quick, working results is exciting and motivating. However, not understanding how the system works as a whole is a big issue. It'll catch up with them later when they finally have to figure out why AI generated something that doesn't work.&lt;/p&gt;

&lt;p&gt;When you learn programming, with GenAI writing code for you, you don't have to track down bugs through debugging or understand the intricacies of how an algorithm works. Instead, you're encouraged to fix issues by simply regenerating the code. In a real-world project, that lack of debugging experience will eventually lead to a dead end.&lt;/p&gt;

&lt;p&gt;It's up to you to decide what to do and whether to take my advice. And my friendly advice is to spend less time reading about the AI hype. Explore classic programming books, practice writing code on your own, and make it work. We'll always need experts who truly understand what they're doing.&lt;/p&gt;

&lt;h3&gt;
  
  
  A look back
&lt;/h3&gt;

&lt;p&gt;Sometimes it's interesting to look at something new through the lens of the past. Here's a quote from a wonderful book "Code Complete" by Steve McConnell (second edition, ISBN 978-5-7502-0064-1).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Over the past several decades programmers have seen numerous tools that were supposed to eliminate programming. First it was third generation languages. Then it was fourth generation languages. Then it was automatic programming. Then it was CASE tools. Then it was visual programming. Each of these advances spun off valuable, incremental improvements to computer programming—and collectively they have made programming unrecognizable to anyone who learned programming before these advances. But none of these innovations succeeded in eliminating programming."&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;blockquote&gt;
&lt;p&gt;"We will always need people who can bridge the gap between the real world problem to be solved and the computer that is supposed to be solving the problem. These people will be called programmers regardless of whether we're manipulating machine registers in assembler or dialog boxes in Visual Basic. As long as we have computers, we'll need people who tell the computers what to do, and that activity will be called programming. When you hear a tool vendor claim, "This new tool will eliminate computer programming"—run! Or at least smile to yourself at the vendor's naive optimism."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As they say, Simpsons already did it. The book was published in 2004.&lt;/p&gt;

&lt;h2&gt;
  
  
  Terrible tip N4. Tab characters in string literals
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;If you need a tab character in a string literal, just press the Tab key. Leave \t to the nerds. Don't sweat it.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I'm talking about string literals where tabs separate words:&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="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;str&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="s2"&gt;"AAA\tBBB\tCCC"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It'd seem there's no other way to do it. Yet, sometimes, developers just carelessly press the &lt;code&gt;Tab&lt;/code&gt; key instead of using &lt;code&gt;\t&lt;/code&gt;. This thing happens even in real projects.&lt;/p&gt;

&lt;p&gt;Such code compiles and even works. However, explicitly using the tab character isn't recommended for the following reasons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Literals may have spaces instead of tabs, which depends on the editor settings. Although, the code looks like there's a tab character.&lt;/li&gt;
&lt;li&gt;The developer who maintains the code won't immediately understand whether tabs or spaces are used as delimiters.&lt;/li&gt;
&lt;li&gt;During refactoring (or using code autoformatting utilities), tabs may turn into spaces, which affects the program's behavior.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Moreover, once I saw the code similar to this one in a real application:&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="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;table&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="s2"&gt;"\
bla-bla-bla bla-bla-bla bla-bla-bla-bla-bla \n\
      bla-bla-bla       bla-bla-bla\n\
          %s                %d\n\
          %s                %d\n\
          %s                %d\n\
"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"X"&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="s2"&gt;"Foo"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Time"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&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 '\' character splits the string into parts. Explicit tab characters are mixed with space characters. Unfortunately, I don't know how to show it here, but trust me, the code looked bizarre. On top of that, the alignment started at the beginning of the screen. Bingo! I've seen lots of strange things while developing a code analyzer!&lt;/p&gt;

&lt;p&gt;Explicit tabs added using the &lt;code&gt;Tab&lt;/code&gt; key are very easy to "lose". If they're replaced with spaces during refactoring or auto-formatting, values will no longer print in a column:&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;bla&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bla&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bla&lt;/span&gt; &lt;span class="n"&gt;bla&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bla&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bla&lt;/span&gt; &lt;span class="n"&gt;bla&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bla&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bla&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bla&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bla&lt;/span&gt;
     &lt;span class="n"&gt;bla&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bla&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bla&lt;/span&gt;       &lt;span class="n"&gt;bla&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bla&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bla&lt;/span&gt;
         &lt;span class="no"&gt;X&lt;/span&gt;                  &lt;span class="mi"&gt;1&lt;/span&gt;
         &lt;span class="nc"&gt;Foo&lt;/span&gt;                  &lt;span class="mi"&gt;2&lt;/span&gt;
         &lt;span class="nc"&gt;Time&lt;/span&gt;                  &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code should've been formatted as follows:&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="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;table&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="s2"&gt;"bla-bla-bla bla-bla-bla bla-bla-bla-bla-bla\n"&lt;/span&gt;
  &lt;span class="s2"&gt;"     bla-bla-bla       bla-bla-bla\n"&lt;/span&gt;
  &lt;span class="s2"&gt;"         %s\t            %d\n"&lt;/span&gt;
  &lt;span class="s2"&gt;"         %s\t            %d\n"&lt;/span&gt;
  &lt;span class="s2"&gt;"         %s\t            %d\n"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"X"&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="s2"&gt;"Foo"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Time"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&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;Tab characters ensure that the second column is aligned, regardless of the name length in the first column (&lt;code&gt;X&lt;/code&gt;, &lt;code&gt;Foo&lt;/code&gt;, &lt;code&gt;Time&lt;/code&gt;). These names are expected to be short—fewer than eight characters long. The fixed code will print:&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;bla&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bla&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bla&lt;/span&gt; &lt;span class="n"&gt;bla&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bla&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bla&lt;/span&gt; &lt;span class="n"&gt;bla&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bla&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bla&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bla&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bla&lt;/span&gt;
     &lt;span class="n"&gt;bla&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bla&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bla&lt;/span&gt;       &lt;span class="n"&gt;bla&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bla&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bla&lt;/span&gt;
         &lt;span class="no"&gt;X&lt;/span&gt;                 &lt;span class="mi"&gt;1&lt;/span&gt;
         &lt;span class="nc"&gt;Foo&lt;/span&gt;               &lt;span class="mi"&gt;2&lt;/span&gt;
         &lt;span class="nc"&gt;Time&lt;/span&gt;              &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some people might wonder why I'm talking about such simple and obvious things. Moreover, even the fixed code is a bit off and doesn't look like a good example for the book. However, since I could clearly see tab characters in the lines, I think it's worth mentioning. This fix is better than having someone repeatedly pressing the &lt;code&gt;Tab&lt;/code&gt; key without understanding what's wrong with it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Author: Andrey Karpov&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Andrey has over 19 years of experience with static code analysis and software quality. The author of numerous articles on writing high-quality code in C++. Andrey Karpov has been honored with the Microsoft MVP award in the Developer Technologies category from 2011 to 2021. Andrey is a co-founder of the PVS-Studio project. He has long been the company's CTO and was involved in the development of the C++ analyzer core. Andrey is currently the Chief Business Development Officer (CBDO), responsible for educational marketing.&lt;/p&gt;

&lt;h2&gt;
  
  
  To be continued
&lt;/h2&gt;

&lt;p&gt;In upcoming posts, you'll find more terrible tips for C++ programmers. Make sure you &lt;a href="https://pvs-studio.com/en/subscribe/" rel="noopener noreferrer"&gt;&lt;strong&gt;subscribe to our blog newsletter&lt;/strong&gt;&lt;/a&gt; to stay up to date with all our latest content!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follow Andrey Karpov on &lt;a href="https://uz.linkedin.com/in/andrey-karpov-pvs-studio" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; and &lt;a href="https://x.com/Code_Analysis" rel="noopener noreferrer"&gt;X&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>programming</category>
      <category>softwaredevelopment</category>
      <category>learning</category>
    </item>
    <item>
      <title>What's new in .NET 11?</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Wed, 11 Nov 2026 08:02:20 +0000</pubDate>
      <link>https://dev.to/pvsdev/whats-new-in-net-11-3pb7</link>
      <guid>https://dev.to/pvsdev/whats-new-in-net-11-3pb7</guid>
      <description>&lt;p&gt;.NET 11 is out now, meaning it's time to start moving to it. But first, let's see whether the new features in C# 15, such as Runtime Async and JIT optimizations, justify an immediate upgrade.&lt;/p&gt;

&lt;p&gt;.NET 11 focuses on performance and runtime improvements. The release is a Standard Term Support (STS) release, so it gets two years of support. Starting with .NET 9, STS releases get 24 months of support instead of 18.&lt;/p&gt;

&lt;p&gt;This article covers the most notable changes to the runtime, libraries, and SDK. For the complete list, see &lt;a href="https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-11/overview" rel="noopener noreferrer"&gt;the official documentation&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  C# 15
&lt;/h2&gt;

&lt;p&gt;All of the C# 15 features were covered in a &lt;a href="https://pvs-studio.com/en/blog/posts/csharp/1421/" rel="noopener noreferrer"&gt;separate article&lt;/a&gt;. The language gained union types, closed hierarchies, and extension indexers, and its approach to memory safety has been reworked. This year's changes are genuinely significant. What's your take on the new version?&lt;/p&gt;

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

&lt;p&gt;Every year, Stephen Toub publishes a detailed review of the performance improvements in the new .NET, walking through each change in depth.&lt;/p&gt;

&lt;p&gt;This year's review covers the JIT, Runtime Async, devirtualization, bounds checking, and a good deal more.&lt;/p&gt;

&lt;p&gt;For a thorough look at this part of .NET 11, see Stephen Toub's &lt;a href="https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-11/" rel="noopener noreferrer"&gt;article&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Libraries
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Compression
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.io.compression?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;System.IO.Compression&lt;/code&gt;&lt;/a&gt; namespace now includes Zstandard compression, through &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.zstandardstream?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;ZstandardStream&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.zstandardencoder?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;ZstandardEncoder&lt;/code&gt;&lt;/a&gt;, and related types. When reading ZIP archives, the CRC32 checksum is now verified, so a corrupted archive throws an &lt;code&gt;InvalidDataException&lt;/code&gt;. ZIP entries now also support passwords and can be opened with the &lt;code&gt;FileAccess&lt;/code&gt; option. Deflate, ZLib, and GZip encoders and decoders gained a &lt;code&gt;Reset()&lt;/code&gt; method, which lets you clear an instance's state and reuse it instead of allocating a new object.&lt;/p&gt;

&lt;h3&gt;
  
  
  LINQ
&lt;/h3&gt;

&lt;p&gt;The new version improves the existing &lt;code&gt;Join&lt;/code&gt; methods and adds &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.fulljoin?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;FullJoin&lt;/code&gt;&lt;/a&gt;, which returns every element from both sequences. Elements are matched by key, and unmatched elements on either side are kept with no partner. Let's see &lt;code&gt;FullJoin&lt;/code&gt; in action:&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;products&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nc"&gt;Category&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Laptop"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Electronics"&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="s2"&gt;"Table"&lt;/span&gt;&lt;span class="p"&gt;,&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="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;categories&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="nc"&gt;Description&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Electronics"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Electronic devices"&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="s2"&gt;"Furniture"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Office furniture"&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;joined&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FullJoin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;categories&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Category&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;category&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="n"&gt;joined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;$&lt;span class="s2"&gt;"{product.Name ?? "&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;none&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"} 
    : {category.Description ?? "&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;none&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="s2"&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 prints the following:&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="nc"&gt;Laptop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Electronic&lt;/span&gt; &lt;span class="n"&gt;devices&lt;/span&gt;
&lt;span class="nc"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;none&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;none&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Office&lt;/span&gt; &lt;span class="n"&gt;furniture&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In addition, LINQ now lets you call &lt;code&gt;Join&lt;/code&gt; and &lt;code&gt;GroupJoin&lt;/code&gt; without a &lt;code&gt;resultSelector&lt;/code&gt;, so they return tuples directly, just as &lt;code&gt;LeftJoin&lt;/code&gt;, &lt;code&gt;RightJoin&lt;/code&gt;, and &lt;code&gt;FullJoin&lt;/code&gt; do.&lt;/p&gt;

&lt;h3&gt;
  
  
  JSON
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.text.json?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;System.Text.Json&lt;/code&gt;&lt;/a&gt; namespace gained several capabilities. For the new &lt;code&gt;union&lt;/code&gt; type introduced in C# 15, the serialization and deserialization methods were adapted so that the current value is written directly, with no wrapper or discriminator. A JSON Lines (JSONL) output format is also available, where each value sits on its own line. To do this, the new &lt;code&gt;JsonSerializer.SerializeAsyncEnumerable&lt;/code&gt; method has a &lt;code&gt;topLevelValues&lt;/code&gt; parameter.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonpolymorphicattribute?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;JsonPolymorphicAttribute&lt;/code&gt;&lt;/a&gt; has a new property, &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonpolymorphicattribute.inferclosedtypepolymorphism?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;InferClosedTypePolymorphism&lt;/code&gt;&lt;/a&gt;, which turns on automatic inference of derived types for a single closed hierarchy without changing the serializer's global settings. Previously, every derived type had to be registered explicitly with the &lt;code&gt;JsonDerivedType&lt;/code&gt; attribute on the base type. The setting can also be applied globally, through &lt;code&gt;JsonSerializerOptions&lt;/code&gt; or the source generator options. In .NET 11, it looks 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="nc"&gt;JsonPolymorphic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;InferClosedTypePolymorphism&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;closed&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Shape&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Circle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nc"&gt;Radius&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Shape&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nc"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Shape&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generic &lt;code&gt;GetTypeInfo&amp;lt;T&amp;gt;()&lt;/code&gt; and  &lt;code&gt;TryGetTypeInfo&amp;lt;T&amp;gt;()&lt;/code&gt; methods were added, both returning &lt;code&gt;JsonTypeInfo&amp;lt;T&amp;gt;&lt;/code&gt;. The release also brings the built-in &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.text.json.jsonnamingpolicy.pascalcase?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;JsonNamingPolicy.PascalCase&lt;/code&gt;&lt;/a&gt; policy, the &lt;code&gt;[JsonNamingPolicy]&lt;/code&gt; attribute for types and their members, the option to apply &lt;code&gt;[JsonIgnore]&lt;/code&gt; to an entire type at once, and built-in converters for &lt;code&gt;BFloat16&lt;/code&gt;, &lt;code&gt;Decimal32&lt;/code&gt;, &lt;code&gt;Decimal64&lt;/code&gt;, and &lt;code&gt;Decimal128&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Asynchronous validation
&lt;/h3&gt;

&lt;p&gt;Asynchronous validation is now available through &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.asyncvalidationattribute?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;AsyncValidationAttribute&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.iasyncvalidatableobject?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;IAsyncValidatableObject&lt;/code&gt;&lt;/a&gt;, along with new methods such as &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.validator.validateobjectasync?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;Validator.ValidateObjectAsync&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.validator.tryvalidateobjectasync?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;Validator.TryValidateObjectAsync&lt;/code&gt;&lt;/a&gt;, and others. It helps when a check has to query a database or an external service without blocking the thread. &lt;code&gt;Microsoft.Extensions.Options&lt;/code&gt; can validate options asynchronously too, including at application startup through the new &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.options.iasyncstartupvalidator?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;IAsyncStartupValidator&lt;/code&gt;&lt;/a&gt; interface.&lt;/p&gt;

&lt;h3&gt;
  
  
  New stream types
&lt;/h3&gt;

&lt;p&gt;Four new &lt;code&gt;Stream&lt;/code&gt; types have arrived: &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.io.readonlymemorystream?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;ReadOnlyMemoryStream&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.io.writablememorystream?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;WritableMemoryStream&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.buffers.readonlysequencestream?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;ReadOnlySequenceStream&lt;/code&gt;&lt;/a&gt;, and &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.io.stringstream?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;StringStream&lt;/code&gt;&lt;/a&gt;. They let you hand an existing buffer or string to any API that accepts a &lt;code&gt;Stream&lt;/code&gt; without copying it first, which means fewer allocations and less pressure on the garbage collector.&lt;/p&gt;

&lt;h3&gt;
  
  
  Numeric types and parsing
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.numerics?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;System.Numerics&lt;/code&gt;&lt;/a&gt; gets  &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.numerics.decimal32?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;Decimal32&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.numerics.decimal64?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;Decimal64&lt;/code&gt;&lt;/a&gt;, and &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.numerics.decimal128?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;Decimal128&lt;/code&gt;&lt;/a&gt; types. These are IEEE 754-2019 decimal floating-point types with 7, 16, and 34 significant digits, respectively. Like &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.decimal?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;System.Decimal&lt;/code&gt;&lt;/a&gt;, they store numbers in base 10, so &lt;code&gt;0.1&lt;/code&gt; is represented exactly. Unlike &lt;code&gt;System.Decimal&lt;/code&gt;, however, they follow the standard, supporting NaN, infinity, and generic math. They suit cases where IEEE 754 behavior matters, such as interoperating with other languages and databases that use the format. A generic &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.numerics.complex-1?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;Complex&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/a&gt; also arrived, which works with &lt;code&gt;float&lt;/code&gt;, &lt;code&gt;Half&lt;/code&gt;, &lt;code&gt;BFloat16&lt;/code&gt;, and the new decimal types.&lt;/p&gt;

&lt;p&gt;A new method, &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.numerics.inumberbase-1.tryparsepartial?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;INumberBase&amp;lt;TSelf&amp;gt;.TryParsePartial&lt;/code&gt;&lt;/a&gt;, which parses a number up to the first character that isn't part of it. Previously, input like &lt;code&gt;123;789&lt;/code&gt; made &lt;code&gt;TryParse&lt;/code&gt; return &lt;code&gt;false&lt;/code&gt;, and leave the result at  0. Here is how it behaves now:&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="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;ParseTest&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="kt"&gt;double&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TryParsePartial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"123;789"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                          &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Globalization&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NumberStyles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                          &lt;span class="n"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;consumed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;$&lt;span class="s2"&gt;"Parsed value (TryParsePartial): {result}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;$&lt;span class="s2"&gt;"Consumed: {consumed}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kt"&gt;double&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TryParse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"456;032"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&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="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;$&lt;span class="s2"&gt;"Parsed value (TryParse): {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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output:&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="nc"&gt;Parsed&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;TryParsePartial&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;
&lt;span class="nc"&gt;Consumed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="nc"&gt;Parsed&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;TryParse&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So when something gets in the way, such as a wrong delimiter or a stray character, you now get a partial result along with the count of characters read. &lt;/p&gt;

&lt;h3&gt;
  
  
  Processes
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;Process&lt;/code&gt;&lt;/a&gt; class now has a whole set of new APIs. Previously, running a utility and collecting its output meant subscribing to &lt;code&gt;OutputDataReceived&lt;/code&gt; and &lt;code&gt;ErrorDataReceived&lt;/code&gt; or reading the streams by hand. Now a single &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.runandcapturetext?view=net-11.0" rel="noopener noreferrer"&gt;call&lt;/a&gt; does 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="nc"&gt;ProcessTextOutput&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="nc"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;RunAndCaptureTextAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"git"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                           &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"--porcelain"&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="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&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="nc"&gt;StandardOutput&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;$&lt;span class="s2"&gt;"Exit code: {result.ExitStatus.ExitCode}"&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;Three more methods came with it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.startandforget?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;Process.StartAndForget&lt;/code&gt;&lt;/a&gt; starts a process without waiting for it to finish.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.startsuspended?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;ProcessStartInfo.StartSuspended&lt;/code&gt;&lt;/a&gt; starts a process without waiting for it to finish.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://learn.microsoft.com/en-us/dotnet/api/microsoft.win32.safehandles.safeprocesshandle.resume?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;SafeProcessHandle.Resume()&lt;/code&gt;&lt;/a&gt; resumes a suspended process.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Runtime
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Runtime Async
&lt;/h3&gt;

&lt;p&gt;This is a major change to the runtime this year. For every &lt;code&gt;async&lt;/code&gt; method, the compiler generates a state machine, whereas &lt;a href="https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-11/runtime#runtime-async" rel="noopener noreferrer"&gt;Runtime Async&lt;/a&gt; hands that work to the runtime itself. The feature started as an experiment in .NET 10. In .NET 11 it's still marked as a preview, but .NET 11 projects no longer need &lt;code&gt;EnablePreviewFeatures&lt;/code&gt; to use it. The .NET libraries are already compiled with Runtime Async and contain no generated state machines. To enable it in your project, add this to the project file:&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;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;PropertyGroup&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Features&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;async&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;on&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Features&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;PropertyGroup&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;The gains come in two areas. First, overhead drops. The JIT now optimizes &lt;code&gt;Task.FromResult&lt;/code&gt;, &lt;code&gt;Task.CompletedTask&lt;/code&gt;, and &lt;code&gt;ValueTask.FromResult&lt;/code&gt; into faster paths, reuses continuation objects, and applies tiered compilation to hot asynchronous methods. Second, the call stack obtained from &lt;code&gt;new StackTrace()&lt;/code&gt; is now easier to read, since it shows only the actual call chain, excluding internal calls. Debugging improves as well, partly because stepping over an &lt;code&gt;await&lt;/code&gt; no longer takes you into infrastructure code.&lt;/p&gt;

&lt;h3&gt;
  
  
  New processor requirements
&lt;/h3&gt;

&lt;p&gt;This &lt;a href="https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-11/runtime#updated-minimum-hardware-requirements" rel="noopener noreferrer"&gt;change&lt;/a&gt; may stop the app from launching on older hardware. On x86/x64, the minimum level for JIT and NativeAOT is now &lt;code&gt;x86-64-v2&lt;/code&gt; instead of &lt;code&gt;x86-64-v1&lt;/code&gt;. On a processor that falls short, the application simply won't run and will report that the basic instruction sets are missing.&lt;/p&gt;

&lt;p&gt;The ReadyToRun images for Windows and Linux on x64 now target &lt;code&gt;x86-64-v3&lt;/code&gt; (&lt;code&gt;AVX2&lt;/code&gt;, &lt;code&gt;BMI2&lt;/code&gt;, &lt;code&gt;FMA&lt;/code&gt;, and others). On processors without these instructions, the application still starts, but the precompiled code has to be recompiled by the JIT, which slows startup. Processors like that are hard to find these days, but it's worth checking in advance which hardware your project runs on.&lt;/p&gt;

&lt;h3&gt;
  
  
  JIT enhancements
&lt;/h3&gt;

&lt;p&gt;Besides supporting Runtime Async, the &lt;a href="https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-11/runtime#jit-improvements" rel="noopener noreferrer"&gt;JIT&lt;/a&gt; has picked up several useful tricks. It now removes more bounds checks, including those for expressions like &lt;code&gt;values[^1]&lt;/code&gt; and &lt;code&gt;i + const &amp;lt; length&lt;/code&gt;, as well as for &lt;code&gt;span[0]&lt;/code&gt; after the &lt;code&gt;!span.IsEmpty&lt;/code&gt; check. It can evaluate &lt;code&gt;string.Equals&lt;/code&gt; and &lt;code&gt;SequenceEqual&lt;/code&gt; at compile time when both operands are known. It also strips redundant conditions and branches, and emits a &lt;code&gt;MUL&lt;/code&gt; instruction for &lt;code&gt;Math.BigMul&lt;/code&gt; on x64.&lt;/p&gt;

&lt;h3&gt;
  
  
  NativeAOT and ReadyToRun
&lt;/h3&gt;

&lt;p&gt;In &lt;a href="https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-11/runtime#jit-improvements" rel="noopener noreferrer"&gt;NativeAOT&lt;/a&gt;, interface method calls now go through a shared dispatch helper, which then redirects them to the appropriate implementation. Call sites get smaller, and code that works heavily with interfaces runs faster.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Comparer&amp;lt;T&amp;gt;.Default&lt;/code&gt; and &lt;code&gt;EqualityComparer&amp;lt;T&amp;gt;.Default&lt;/code&gt; are now specialized during precompilation in &lt;a href="https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-11/runtime#readytorun-improvements" rel="noopener noreferrer"&gt;ReadyToRun&lt;/a&gt; during precompilation, so they no longer need the JIT at runtime.&lt;/p&gt;

&lt;h2&gt;
  
  
  SDK
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The CLI is now faster
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;dotnet&lt;/code&gt; &lt;a href="https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-11/sdk#cli-architecture" rel="noopener noreferrer"&gt;CLI&lt;/a&gt; runs as a NativeAOT application by default. Commands such as &lt;code&gt;dotnet --version&lt;/code&gt;, &lt;code&gt;dotnet --info&lt;/code&gt;, and &lt;code&gt;dotnet --help&lt;/code&gt; run entirely natively, while external tools, such as &lt;code&gt;dotnet ef&lt;/code&gt; launch directly, skipping the managed part of the CLI.&lt;/p&gt;

&lt;p&gt;The number of SDK installers for Linux and macOS has dropped: identical builds are now replaced by symbolic links to a single copy.&lt;/p&gt;

&lt;h3&gt;
  
  
  Single-file applications
&lt;/h3&gt;

&lt;p&gt;The ability to run .cs files without a project, introduced in .NET 10, keeps evolving. The &lt;a href="https://learn.microsoft.com/en-us/dotnet/core/sdk/file-based-apps#include" rel="noopener noreferrer"&gt;&lt;code&gt;#:include&lt;/code&gt;&lt;/a&gt; directive lets you to spread code across several files, or even reference a pre-built DLL.&lt;/p&gt;

&lt;p&gt;These applications also support &lt;code&gt;dotnet format&lt;/code&gt; now. Restarting an unmodified NativeAOT application reuses the output compiled earlier.&lt;/p&gt;

&lt;h3&gt;
  
  
  The dotnet run and dotnet watch commands
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;dotnet run&lt;/code&gt; command can pass environment variables to the application straight from the command line using the &lt;code&gt;-e&lt;/code&gt; parameter.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;dotnet watch&lt;/code&gt; command gained integration with the Aspire host, automatic restart after a crash, and device selection for MAUI projects.&lt;/p&gt;

&lt;h3&gt;
  
  
  Testing
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-11/sdk#dotnet-test-improvements" rel="noopener noreferrer"&gt;&lt;code&gt;dotnet test&lt;/code&gt;&lt;/a&gt; command in Microsoft.Testing.Platform now comes with a lot of new options. For example, you can cap the run time or stop after a set number of failed tests.&lt;/p&gt;

&lt;p&gt;A new &lt;code&gt;--no-dependencies&lt;/code&gt; option skips building dependent projects. There's also an experimental mode that runs only the tests affected by your changes.&lt;/p&gt;

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

&lt;p&gt;.NET 11 is, above all, a runtime release. Runtime Async is already in use inside the .NET libraries, the JIT has grown smarter, and the CLI starts faster. In everyday work, though, the features you'll notice most are the C# 15 additions and smaller but handy methods such as &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.runandcapturetextasync?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;Process.RunAndCaptureTextAsync&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.fulljoin?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;FullJoin&lt;/code&gt;&lt;/a&gt;, and &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/system.numerics.inumberbase-1.tryparsepartial?view=net-11.0" rel="noopener noreferrer"&gt;&lt;code&gt;TryParsePartial&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you're planning to upgrade, check the new processor requirements first.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://learn.microsoft.com/dotnet/core/whats-new/dotnet-11/overview" rel="noopener noreferrer"&gt;Microsoft documentation&lt;/a&gt; has the complete list of changes. Which new features did you like? Let us know in the comments.&lt;/p&gt;

&lt;p&gt;For more articles on C# and .NET, visit the &lt;a href="https://pvs-studio.com/en/blog/posts/csharp/" rel="noopener noreferrer"&gt;PVS-Studio blog&lt;/a&gt;, and &lt;a href="https://pvs-studio.com/en/subscribe/" rel="noopener noreferrer"&gt;subscribe here&lt;/a&gt; to catch new ones as they come out.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>dotnetcore</category>
      <category>csharp</category>
    </item>
    <item>
      <title>Unity3D for .NET Developers: How to get started</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Tue, 03 Nov 2026 12:39:35 +0000</pubDate>
      <link>https://dev.to/pvsdev/unity3d-for-net-developers-how-to-get-started-1cco</link>
      <guid>https://dev.to/pvsdev/unity3d-for-net-developers-how-to-get-started-1cco</guid>
      <description>&lt;p&gt;As a C#/.NET developer, have you ever thought about getting into game development with Unity3D? After all, it uses the same language. Is it really the same, though? Can you simply copy code written for .NET into a Unity3D application and expect it to work without any issues? Let's take a closer look to find out what extra knowledge C#/.NET developers need to comfortably and effectively develop games.&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%2Fz2svldbx620qlk982ff1.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%2Fz2svldbx620qlk982ff1.png" alt="1424_unity3d/image1.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We published and translated this article with the copyright holder's permission. The author is &lt;a href="https://github.com/kekchpek/" rel="noopener noreferrer"&gt;Kekchpek&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Compilation and execution
&lt;/h2&gt;

&lt;p&gt;First, we'll compare how our code is compiled and executed in .NET and Unity3D. In .NET, the process is fairly straightforward and transparent. The C# code is first compiled into IL, then packaged into DLL or EXE files, and finally executed by a virtual machine. However, this approach has one drawback that has long posed a major obstacle to cross-platform development for C# programmers: dependence on a virtual machine. Today's .NET is nothing like its predecessor, .NET Framework, which was exclusively Windows-based. Before .NET emerged, the most popular solution for cross-platform development was the Mono virtual machine, which could also execute IL code and served as an alternative to .NET Framework.&lt;/p&gt;

&lt;p&gt;Unity3D originally ran on the Mono virtual machine. Today, Mono remains one of the options for running C# code in Unity3D. The second option is IL2CPP, a newer technology developed by Unity Technologies, which is gradually replacing Mono. Code compiled with IL2CPP runs faster and doesn't require a pre-installed or bundled virtual machine. As for how IL2CPP works, its name speaks for itself. Roslyn first compiles C# into IL, which is then translated into C++. Finally, the C++ compiler converts the code into machine code. The C++ compiler varies depending on the target platform. In terms of C#, however, the code behaves much like it does in .NET. Unity3D uses a very similar garbage collector, standard library, and so on.&lt;/p&gt;

&lt;p&gt;So, although the compilation process differs, the average developer won't notice much difference. One drawback is that it's nearly impossible to predict how the code will compile on different platforms. IL2CPP bugs are certainly a possibility, but encountering them is as unlikely as running into bugs in .NET.&lt;/p&gt;

&lt;p&gt;Any C# DLL libraries that contains IL can be used in Unity3D.  During compilation, Unity runs the code through IL2CPP and recompiles it into C++.&lt;/p&gt;

&lt;p&gt;However, things aren't that simple when it comes to C# versions. Unity uses C# 9, which is rather outdated, and even that version lacks support for certain features. You can read more about it &lt;a href="https://docs.unity3d.com/2022.3/Documentation/Manual/CSharpCompiler.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Libraries and compatibility
&lt;/h2&gt;

&lt;p&gt;As I mentioned earlier, libraries written in C# can be compiled for different platforms using IL2CPP, but there are some limitations. Unity supports compatibility with .NET Framework and .NET Standard libraries. However, that doesn't mean libraries compiled for .NET won't work. They might work, but not all of them will. It depends on whether their IL code contains anything that Unity3D can't translate into C++.&lt;/p&gt;

&lt;p&gt;The standard library works in Unity3D, but with a few caveats. In other words, we have access to all the features of standard C#, including collections, concurrency, Tasks, LINQ, networking, and more. However, &lt;code&gt;Thread&lt;/code&gt; and &lt;code&gt;Task&lt;/code&gt; (when used to run separate threads), for example, won't work in WebGL builds (games that run in browsers). This is one of the most obvious limitations, but one may occasionally encounter others related to the specifics of a particular platform.&lt;/p&gt;

&lt;p&gt;From a coding perspective, Unity3D acts as an extensive library within the &lt;code&gt;UnityEngine&lt;/code&gt; namespace. The &lt;code&gt;UnityEditor&lt;/code&gt; namespace also exists that contains the editor API, which enables developers customize windows, builds, plugins, and more.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to organize code into modules
&lt;/h2&gt;

&lt;p&gt;In .NET, we're used to organizing code into separate modules via &lt;code&gt;.sln&lt;/code&gt; and &lt;code&gt;.csproj&lt;/code&gt; files. Unity3D has them too, but we can't work with them directly. Unity3D creates &lt;code&gt;.sln&lt;/code&gt; and &lt;code&gt;.csproj&lt;/code&gt; files, purely for the developers' convenience. This allows popular IDEs to provide proper syntax highlighting, error detection, and other useful features. Unity itself doesn't rely on the contents of &lt;code&gt;.sln&lt;/code&gt; or &lt;code&gt;.csproj&lt;/code&gt; files at any stage of the project build process. As a result, one common mistake beginners make is adding a library via their IDE. This only modifies the &lt;code&gt;.csproj&lt;/code&gt; file, but Unity remains unaware of the change. So, when a dev tries to compile the project, Unity starts throwing errors even though the IDE shows no errors at all.&lt;/p&gt;

&lt;p&gt;To control how Unity3D splits code into modules and what properties those modules have, we need to use Unity's built-in tools. The main feature we'll work with here is Assembly Definition, Unity's equivalent of a &lt;code&gt;.csproj&lt;/code&gt; file in .NET. To use Assembly Definition, we need to create an &lt;code&gt;.asmdef&lt;/code&gt; file in one of the project folders. We can do this via the Unity graphical interface. Once we create the file, Unity treats all &lt;code&gt;.cs&lt;/code&gt; files in that folder and its subfolders as a separate module. It generates a dedicated &lt;code&gt;.csproj&lt;/code&gt; file for this code and enables us to specify dependencies required by that particular module. We can also use the module as a dependency. We configure everything using Unity3D's graphical interface, and Unity reflects those settings in the generated &lt;code&gt;.csproj&lt;/code&gt; files.&lt;/p&gt;

&lt;p&gt;By default, we don't create any &lt;code&gt;.asmdef&lt;/code&gt; files, Unity automatically divides code into four modules based on the names of the folders containing the &lt;code&gt;.cs&lt;/code&gt; files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Assembly-CSharp-Editor-firstpass: the "*/Standard Assets/**/Editor", "*/Pro Standard Assets/**/Editor" and "*/Plugins/**/Editor" folders&lt;/li&gt;
&lt;li&gt;Assembly-CSharp-firstpass: the "*/Standard Assets", "*/Pro Standard Assets" and "*/Plugins" folders&lt;/li&gt;
&lt;li&gt;Assembly-CSharp-Editor: the "*/Editor" folders&lt;/li&gt;
&lt;li&gt;Assembly-CSharp: any other folder&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using Assembly Definitions is generally considered a good practice, though not every project has them. The key point to keep in mind is the difference between the Editor and Runtime modules. Editor contains code that runs only in the Unity editor and doesn't make it into the game build. Developers usually use this code to create additional tools for game designers, artists, and programmers. We can specify in any Assembly Definition that it's intended only for the editor.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to integrate third-party libraries
&lt;/h2&gt;

&lt;p&gt;The easiest way to add a third-party library is to simply copy its DLL into the project folder. Unity automatically scans the project folder for DLLs and adds them as dependencies to the generated &lt;code&gt;.csproj&lt;/code&gt; files. In the Assembly Definition settings, we can explicitly specify which DLLs to include as dependencies and which to exclude. &lt;/p&gt;

&lt;p&gt;We can also directly add source code instead of using DLLs. This might seem a little strange to a .NET developer, but it's actually a fairly common way to distribute plugins and SDKs for Unity3D. Unity even has a dedicated packaging mechanism called &lt;code&gt;.unitypackage&lt;/code&gt;. These files simplify the distribution of assets that need to be imported into specific parts of a project. Usually, when we want to download some open-source SDK, we get a &lt;code&gt;.unitypackage&lt;/code&gt; file from GitHub and open it in the Unity editor. The editor adds the necessary source files to the project or updates existing ones, first enabling a developer to review the package contents and choose which files to import. Some SDKs even offer to remove outdated files from previous versions after importing the &lt;code&gt;.unitypackage&lt;/code&gt;. However, I'm not sure whether this feature is built into the Unity editor or if SDK developers implemented it themselves.&lt;/p&gt;

&lt;p&gt;A more convenient way to manage dependencies is the Unity Package Manager. It's a package manager similar to npm, with a graphical interface built into the Unity Editor. As with npm, each package used in the project has a &lt;code&gt;package.json&lt;/code&gt; file that describes the package and its dependencies. Every package stores this file in its root directory. &lt;/p&gt;

&lt;p&gt;The project itself, however, uses &lt;code&gt;manifest.json&lt;/code&gt; instead of &lt;code&gt;package.json&lt;/code&gt;, unlike npm. Despite the different name, &lt;code&gt;manifest.json&lt;/code&gt; serves a similar purpose: it lists the project's dependencies and the package registries from which Unity can retrieve them. Instead of specifying a version, we can provide a link to a GitHub repository or a specific directory within that repository from which Unity should download the dependency. We can also use links to private repositories after configuring &lt;a href="https://docs.unity3d.com/Manual/upm-config-https-git.html" rel="noopener noreferrer"&gt;Git Credential Manager&lt;/a&gt;. Unfortunately, we can specify GitHub links only for dependencies listed in &lt;code&gt;manifest.json&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;In other words, we can't use GitHub links for transitive dependencies (dependencies of other dependencies). This limitation makes managing dependencies less convenient when building infrastructure for large-scale projects. One solution is to create our own package registries and host them in the cloud. These registries can be either private or public.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://openupm.com/" rel="noopener noreferrer"&gt;OpenUPM (Open Unity Package Manager)&lt;/a&gt; deserves a special mention. It's an open package registry for the standard Unity Package Manager where anyone can publish their packages. Most packages distributed as &lt;code&gt;.unitypackage&lt;/code&gt; files are also available on OpenUPM. There are also many NuGet packages available on OpenUPM. However, not every NuGet package developer—or developer of any other package—takes the time to publish their packages on OpenUPM. As a result, some packages listed on OpenUPM are just forks of the original projects, tweaked to work with OpenUPM. I'd still advise against dismissing these packages outright. &lt;/p&gt;

&lt;p&gt;If you're a company that takes its code supply chain seriously and carefully controls what goes into its projects, the best approach is to set up your own package registry. You can then validate incoming packages and download everything from that registry. But if you're developing a small game, a prototype, or if you simply lack the resources to maintain a robust infrastructure for your project, OpenUPM is an excellent solution.&lt;/p&gt;

&lt;p&gt;And since this article is aimed at .NET developers, I'd like to mention &lt;a href="https://github.com/GlitchEnzo/NuGetForUnity" rel="noopener noreferrer"&gt;NuGet for Unity&lt;/a&gt;. It's a plugin that adds a user-friendly interface for managing NuGet packages in Unity. I wouldn't recommend it, despite having used it a lot myself. The main disadvantage of using NuGet is that it brings multiple sources and dependency managers into a project. This can quickly turn into a major headache in large projects with lots of dependencies. Managing package versions and resolving conflicts between transitive dependencies can be serious challenges.&lt;/p&gt;

&lt;p&gt;In short, I recommend using the Unity Package Manager alongside OpenUPM and other package registries wherever possible. Resort to other ways of adding dependencies only when the Unity Package Manager isn't an option.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unity3D architecture
&lt;/h2&gt;

&lt;p&gt;Like many game engines, Unity3D uses a tree-based structure to organize objects. An application contains multiple scenes (&lt;code&gt;UnityEngine.Scene&lt;/code&gt;), and each of them contains numerous game objects (&lt;code&gt;UnityEngine.GameObject&lt;/code&gt;) that act as containers for components (&lt;code&gt;UnityEngine.Component&lt;/code&gt;). A component is a basic logical unit that contains executable code. Game objects (&lt;code&gt;GameObject&lt;/code&gt;) can also be arranged into hierarchical structures.&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%2F55kjciwf8ksjjb3dndjn.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%2F55kjciwf8ksjjb3dndjn.png" alt="1424_unity3d/image2.png" width="621" height="686"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every &lt;code&gt;GameObject&lt;/code&gt; has one mandatory component that can't be removed: &lt;code&gt;UnityEngine.Transform&lt;/code&gt;. It controls the object's position in the scene and its place in the hierarchy compared to other &lt;code&gt;GameObjects&lt;/code&gt;. We can add or remove other components either while working with the scene in the editor or at runtime through code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Entry point
&lt;/h2&gt;

&lt;p&gt;I believe we have enough context now to move on to the more hands-on topics. The application entry point is the best place to start. In Unity3D, we don't have access to the &lt;code&gt;Main&lt;/code&gt; function. Instead, we have to work within Unity's architecture, which loads the first scene in the scene list when the application starts. The only option available to us is to create a custom component and then link it to an object in the starting scene. Once the scene has loaded, the code in that component will start executing.&lt;/p&gt;

&lt;p&gt;To create a component, we'll create a class that inherits from &lt;code&gt;UnityEngine.MonoBehaviour&lt;/code&gt;. Here's a simple example of a custom component:&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;using&lt;/span&gt; &lt;span class="n"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyComponent&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MonoBehaviour&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// do something on component start working&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// do something every frame&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, we can use the editor to create as many instances of this class as needed and attach them to any &lt;code&gt;GameObject&lt;/code&gt; in the scene. Each component's &lt;code&gt;Start&lt;/code&gt; method will be called at the beginning of its lifecycle, while &lt;code&gt;Update&lt;/code&gt; will run every frame. Unity orders all these method calls according to its &lt;a href="https://docs.unity3d.com/2022.3/Documentation/Manual/ExecutionOrder.html" rel="noopener noreferrer"&gt;Script Lifecycle&lt;/a&gt;, which is a key architectural element in Unity3D. When designing how code interacts with the engine's API, developers need to understand exactly where each code fragment should run in the cycle.&lt;/p&gt;

&lt;p&gt;This is where Unity3D starts getting quirky. Notice that &lt;code&gt;Start&lt;/code&gt; and &lt;code&gt;Update&lt;/code&gt; don't override any base methods; they aren't part of an interface, and aren't even public. Yet third-party code still calls them. We rarely see this approach in .NET, where such things usually rely on reflection. Unity, however, can afford to bend the language rules. Acting as a compiler, it integrates code into its event system without requiring the explicit language constructs normally used for this purpose. In other words, Unity binds methods by name, almost as if we were writing JavaScript. The methods can be &lt;code&gt;private&lt;/code&gt; or &lt;code&gt;public&lt;/code&gt;—it makes no difference. We can also use inheritance with virtual methods. For example, we can create components that inherit from &lt;code&gt;MyComponent&lt;/code&gt; and override the necessary virtual methods. &lt;/p&gt;

&lt;p&gt;Unity3D's event system includes many other methods that Unity calls at various stages of the Script Lifecycle. We could probably call this reflection in terms of the concept, but the way it works is fundamentally different from using &lt;code&gt;System.Reflection&lt;/code&gt; or similar runtime tools. All the magic happens at compile time, so it doesn't affect the performance of such calls.&lt;/p&gt;

&lt;p&gt;Unity provides a class called &lt;code&gt;UnityEngine.UIBehaviour&lt;/code&gt;, which can also be inherited from to create components. It's a &lt;code&gt;MonoBehaviour&lt;/code&gt; subclass whose main purpose is to provide a set of empty virtual methods related to Unity UI events. However, nothing stops us from inheriting directly from &lt;code&gt;MonoBehaviour&lt;/code&gt; and defining all the necessary methods without overriding any virtual ones. This feels a little odd, as if even the Unity developers haven't quite settled on a consistent way to best implement Script Lifecycle functions.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to create components
&lt;/h2&gt;

&lt;p&gt;We've already encountered one unusual aspect of C# code in Unity, and there's more to come. As I mentioned earlier, we can add components either using the scene editor or at runtime via code. Let's start with adding them in the editor. This part is straightforward: we select an object in the scene, click the &lt;strong&gt;Add Component&lt;/strong&gt; button in the &lt;strong&gt;Inspector&lt;/strong&gt; window, choose &lt;code&gt;MyComponent&lt;/code&gt;, and Unity adds it to the object. To understand how this works, it's worth taking a look under the hood. &lt;/p&gt;

&lt;p&gt;Each scene exists as a separate file that contains everything in that scene, including the entire object hierarchy and all its components. We create an instance of our class when we add a component. When we save the scene, Unity serializes that instance into the scene file. Once we build and run the game, Unity deserializes all our components as it loads the scene, creating new instances of their classes. If we apply the &lt;code&gt;UnityEngine.SerializeField&lt;/code&gt; attribute to any of our fields, we can save a component with a specific value for that field. The editor even displays a UI control that enables us to edit the field on the component we've added. By default, Unity serializes &lt;code&gt;public&lt;/code&gt; fields.&lt;/p&gt;

&lt;p&gt;Adding components at runtime is also quite straightforward. We use the &lt;code&gt;GameObject.AddComponent&amp;lt;T&amp;gt;()&lt;/code&gt; method for this. It's an instance method, so we first need to obtain a &lt;code&gt;GameObject&lt;/code&gt; instance. This isn't a problem, as the Unity API provides plenty of ways to retrieve a particular &lt;code&gt;GameObject&lt;/code&gt;. The most common approach is to use the &lt;code&gt;MonoBehaviour.gameObject&lt;/code&gt; property. &lt;/p&gt;

&lt;p&gt;By the way, the Unity Engine's API naming conventions may look unusual to .NET developers: properties and fields start with a lowercase letter. Still, in almost every project I've seen, developers stick to the naming conventions commonly used in .NET. Here's an example of a component that adds a copy of itself to the same &lt;code&gt;GameObject&lt;/code&gt; three seconds after it starts executing:&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;using&lt;/span&gt; &lt;span class="n"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CopySelfComponent&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MonoBehaviour&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;_passedTime&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Update&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;_passedTime&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// gameObject is a public property of MonoBehaviour&lt;/span&gt;
      &lt;span class="n"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddComponent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;CopySelfComponent&lt;/span&gt;&lt;span class="o"&gt;&amp;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="c1"&gt;// Time.deltaTime is a static property that stores the delta between&lt;/span&gt;
      &lt;span class="c1"&gt;// the start of the previous and current Update phases&lt;/span&gt;
      &lt;span class="c1"&gt;// in the Script lifecycle&lt;/span&gt;
      &lt;span class="n"&gt;_passedTime&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deltaTime&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;At this point, an experienced developer might ask, "What about object dependencies?" It's no secret that OOP revolves around creating various classes and establishing clear, precise, and elegant relationships between them. In most programming languages, including C#, we use constructors to define a class's dependencies. I've already described two ways to create a component, but neither involves a constructor. The reason might blow your mind: constructors for &lt;code&gt;MonoBehaviour&lt;/code&gt; subclasses are UB in C#. For those unfamiliar with C++ and the concept of undefined behavior, here's how I'd describe it: you can use it, but it won't end well. Whatever you're trying to achieve, it'll probably fail. In other words, we can't use constructors for components. &lt;/p&gt;

&lt;p&gt;There's a formal alternative: the &lt;code&gt;Awake()&lt;/code&gt; method. It's the first method Unity calls when creating a component, with a few caveats we won't get into here. However, this method doesn't take any arguments either. A common workaround for dependency injection is to define a separate method, usually called &lt;code&gt;Construct(...)&lt;/code&gt; or &lt;code&gt;Initialize(...)&lt;/code&gt;, and pass all the required dependencies to it. Many developers will immediately recognize the downsides of this approach: we end up with an object in an inconsistent state immediately after its creation. That's something we have to live with. The usual solution is to encapsulate object creation in factories or use dedicated DI frameworks with their own APIs for creating objects and components. Here's what a typical component might look like when written using one of the most popular DI frameworks, Zenject:&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;using&lt;/span&gt; &lt;span class="n"&gt;UnityEngine&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;Zenject&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyComponent&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MonoBehaviour&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="c1"&gt;// Here, we can't even use `readonly` for dependencies, &lt;/span&gt;
  &lt;span class="n"&gt;since&lt;/span&gt; &lt;span class="n"&gt;they&lt;/span&gt; &lt;span class="n"&gt;change&lt;/span&gt; &lt;span class="n"&gt;after&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;object&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;created&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;IService1&lt;/span&gt; &lt;span class="n"&gt;_service1&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;IService1&lt;/span&gt; &lt;span class="n"&gt;_service2&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;IService1&lt;/span&gt; &lt;span class="n"&gt;_service3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// An attribute that tells the DI framework that this is a method &lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;dependency&lt;/span&gt; &lt;span class="n"&gt;injection&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Inject&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Construct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;IService1&lt;/span&gt; &lt;span class="n"&gt;service1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;IService2&lt;/span&gt; &lt;span class="n"&gt;service2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;IService3&lt;/span&gt; &lt;span class="n"&gt;service3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;_service1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;service1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;_service2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;service2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;_service3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;service3&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;Things aren't so bad when it comes to classes that don't inherit from &lt;code&gt;MonoBehaviour&lt;/code&gt;. We can use them just as we would in .NET. In large projects, developers typically implement most of the application's business logic in regular classes and use components as an integration layer with the engine.&lt;/p&gt;

&lt;h2&gt;
  
  
  The intricacies of writing game code
&lt;/h2&gt;

&lt;p&gt;Overall, I've covered the main differences in the language, but Unity3D development also has a few specific aspects. Remember that games are resource-intensive applications, mainly because of their graphics. However, we also need to handle code more carefully to avoid putting unnecessary strain on a device that's already working hard. So, here are a few things to keep in mind:&lt;/p&gt;

&lt;h3&gt;
  
  
  Minimize calls to "expensive" APIs
&lt;/h3&gt;

&lt;p&gt;The Unity library offers plenty of ways to tank an application's performance. Almost all API calls that create or access entities are rather slow. Of course, "slow" in this context doesn't refer to the pace perceived by a human observer; rather, it's a measure of the speed in terms of CPU standards. Here's an example of code that gradually changes a UI image's color from one to another and back again:&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;using&lt;/span&gt; &lt;span class="n"&gt;UnityEngine&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;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UI&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;Components&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyBehaviour&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MonoBehaviour&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="c1"&gt;// When the scene loads, Unity prioritizes &lt;/span&gt;
        &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;serialized&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; 
        &lt;span class="c1"&gt;// If the component is being created for the first time, &lt;/span&gt;
        &lt;span class="n"&gt;Unity&lt;/span&gt; &lt;span class="n"&gt;uses&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;initialization&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="n"&gt;instead&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SerializeField&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;Color&lt;/span&gt; &lt;span class="n"&gt;_color1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SerializeField&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;Color&lt;/span&gt; &lt;span class="n"&gt;_color2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Update&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;img&lt;/span&gt; &lt;span class="o"&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;GetComponent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Image&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;// THIS CODE WILL RUN SLOWLY&lt;/span&gt;

            &lt;span class="c1"&gt;// Some simple math is involved here. I'll spare you the details.&lt;/span&gt;
            &lt;span class="c1"&gt;// You can look into it on your own if you're interested.&lt;/span&gt;
            &lt;span class="n"&gt;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Lerp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_color1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_color2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;Mathf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PingPong&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="n"&gt;f&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;If the code accumulates enough of such calls, they'll significantly impact performance. A better approach is to cache &lt;code&gt;Image&lt;/code&gt; in &lt;code&gt;Awake()&lt;/code&gt; or declare it as a serialized field so we can assign its value in the editor. We can drag and drop many types derived from &lt;code&gt;UnityEngine.Object&lt;/code&gt; into UI fields in the editor to assign references to specific instances 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;using&lt;/span&gt; &lt;span class="n"&gt;UnityEngine&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;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UI&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyBehaviour&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SerializeField&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;Color&lt;/span&gt; &lt;span class="n"&gt;_color1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SerializeField&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;Color&lt;/span&gt; &lt;span class="n"&gt;_color2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;blue&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;Image&lt;/span&gt; &lt;span class="n"&gt;_image&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;  &lt;span class="n"&gt;Awake&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;GetComponent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Image&lt;/span&gt;&lt;span class="o"&gt;&amp;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;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Lerp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_color1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_color2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="n"&gt;Mathf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PingPong&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="n"&gt;f&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;Or 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;using&lt;/span&gt; &lt;span class="n"&gt;UnityEngine&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;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UI&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyComponent&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SerializeField&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;Color&lt;/span&gt; &lt;span class="n"&gt;_color1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SerializeField&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;Color&lt;/span&gt; &lt;span class="n"&gt;_color2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SerializeField&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;Image&lt;/span&gt; &lt;span class="n"&gt;_image&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;_image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Lerp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_color1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_color2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;Mathf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PingPong&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="n"&gt;f&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;Here are some other commonly used—though not the only—methods that can cause performance issues:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;GetComponent&amp;lt;T&amp;gt;()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;GetComponents&amp;lt;T&amp;gt;()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;GetComponentInChildren&amp;lt;T&amp;gt;()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;GetComponentInParent&amp;lt;T&amp;gt;()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;FindObjectOfType&amp;lt;T&amp;gt;()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;FindObjectsOfType&amp;lt;T&amp;gt;()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Find(string)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;FindWithTag(string)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;FindGameObjectsWithTag(string)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How to create objects
&lt;/h3&gt;

&lt;p&gt;Object creation is another aspect of Unity development that deserves careful attention. I'm referring to both Unity3D objects (&lt;code&gt;GameObject&lt;/code&gt; and &lt;code&gt;MonoBehaviour&lt;/code&gt;) and regular class instances. In short, the best approach for performance is to avoid creating objects altogether. This is why Unity3D developers extensively use object pooling. It's hardly a groundbreaking idea, and Unity3D already provides a built-in class for this purpose: &lt;code&gt;UnityEngine.Pool.ObjectPool&amp;lt;T&amp;gt;&lt;/code&gt;. Why does this matter in the game development industry? There are different reasons for Unity3D objects and regular class instances.&lt;/p&gt;

&lt;p&gt;For regular class instances, the issue is straightforward: memory allocation. One of the biggest threats to application performance is the garbage collector. Just as in .NET, the garbage collector pauses code execution to manage memory. Creating large numbers of short-lived objects can trigger frequent garbage collection, causing FPS drops during those pauses. Developers may encounter this issue when their code creates various collections to process and transfer data, DTOs, and similar objects.&lt;/p&gt;

&lt;p&gt;Although they usually have a longer lifespan, memory allocation for &lt;code&gt;GameObject&lt;/code&gt; instances and components is another concern. The issue once again lies with the slow API responsible for creating them. Sometimes, we need to transition from one game state to another while displaying large numbers of objects along the way. For example, we might load an environment, spawn a large number of units, or create bullets when firing a weapon, which is a now-classic use case. Our goal in these situations is to create game objects as quickly as possible to prevent FPS drops and loading screens that frustrate players. That's where object pooling comes in, ideally with the warm pool. However, we shouldn't forget about the balance between memory usage and performance. Pooling too many objects will cause the application to use too much memory, creating its own set of problems. So, we should make smart use of the device's available resources.&lt;/p&gt;

&lt;h3&gt;
  
  
  Structs
&lt;/h3&gt;

&lt;p&gt;For the same performance reasons, it's a useful skill for any developer to know when to use a struct instead of a class, as this helps reduce the load on the garbage collector. I won't go into much detail here, since the rules are roughly the same as in .NET. It's also important to remember about boxing and avoid unnecessarily allocating structs on the heap.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to destroy objects
&lt;/h3&gt;

&lt;p&gt;When it comes to destroying objects, Unity has a small peculiarity. Regular classes depend on the garbage collector. However, things become more complicated when it comes to objects that are part of the engine. Let's take &lt;code&gt;GameObject&lt;/code&gt;, for example. When we write new &lt;code&gt;GameObject()&lt;/code&gt;, we create a new instance of the &lt;code&gt;GameObject&lt;/code&gt; class. At the same time, Unity adds the object to the hierarchy of the currently open scene. It may seem obvious that if the code no longer contains any references to this instance, it will still be part of the scene and will not be removed by the garbage collector.&lt;/p&gt;

&lt;p&gt;To explicitly remove the object from the scene, we need to call the &lt;code&gt;UnityEngine.Object.Destroy(UnityEngine.Object)&lt;/code&gt; static method and pass the instance as an argument. This may seem obvious for &lt;code&gt;GameObject&lt;/code&gt; and &lt;code&gt;MonoBehaviour&lt;/code&gt; subclasses. We know these objects are part of the scene, so it makes sense that we need to take explicit action to remove them. However, there are less obvious cases, too. &lt;/p&gt;

&lt;p&gt;For example, the &lt;code&gt;Texture2D&lt;/code&gt; class is an abstraction of a texture, which is an image. We can create a Texture2D instance and assume that we're the only ones holding a reference to it, so once we lose all references, the garbage collector will remove it. Unfortunately, that's not how it works. The rule is simple: we should call &lt;code&gt;UnityEngine.Object.Destroy(UnityEngine.Object)&lt;/code&gt; for all explicitly created subclasses of &lt;code&gt;UnityEngine.Object&lt;/code&gt;. The reason is that C# objects in the Unity API are simply wrappers for native objects belonging to the engine's C++ code. If we lose all references to our object, only the C# wrapper is removed. However, the C++ object under the hood still occupies memory. On the other hand, calling &lt;code&gt;UnityEngine.Object.Destroy(UnityEngine.Object)&lt;/code&gt; destroys only the C++ object. So, after calling &lt;code&gt;UnityEngine.Object.Destroy(UnityEngine.Object)&lt;/code&gt;, we still have the same C# object, but all of its methods will throw errors because the object that handles the logic has been destroyed.&lt;/p&gt;

&lt;p&gt;We need to be careful when comparing &lt;code&gt;UnityEngine.Object&lt;/code&gt; subclasses to &lt;code&gt;null&lt;/code&gt;. Unity overrides the equality operator for &lt;code&gt;UnityEngine.Object&lt;/code&gt;. So, in addition to checking whether both operands are &lt;code&gt;null&lt;/code&gt;, Unity also checks whether the C++ object has been destroyed. If it has, comparing the object to &lt;code&gt;null&lt;/code&gt; will return &lt;code&gt;true&lt;/code&gt;. This behavior can be confusing and makes comparisons to &lt;code&gt;null&lt;/code&gt; relatively expensive. So, it's better to avoid them in frequently executed code as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  Linq
&lt;/h3&gt;

&lt;p&gt;LINQ extension methods are incredibly convenient. They're also highly effective in middleware-based architectures, where different layers of an application can process the same enumerable object in sequence without repeatedly iterating over its elements. However, there's one major drawback to using LINQ with Unity3D: its extension methods return objects that require heap allocations. &lt;/p&gt;

&lt;p&gt;This brings us back to the issue of unnecessary object creation we discussed earlier. The overhead is negligible on its own, but constantly using it in frequently executed code can create a problem. I recommend keep using LINQ if you follow the middleware-based approach. However, try to replace LINQ methods with regular &lt;code&gt;for&lt;/code&gt; and &lt;code&gt;foreach&lt;/code&gt; if you're using them within a single method. &lt;/p&gt;

&lt;p&gt;Also, be careful with methods that execute LINQ queries, such as &lt;code&gt;ToList()&lt;/code&gt;, &lt;code&gt;ToArray()&lt;/code&gt;, and so on, as they can significantly increase memory usage. Call them only when you actually need the query result as a concrete collection, or avoid them altogether. That said, I don't think things are any different here compared to .NET.&lt;/p&gt;

&lt;h3&gt;
  
  
  Concurrency and asynchronous programming
&lt;/h3&gt;

&lt;p&gt;Concurrency in Unity3D can be tricky. As I mentioned earlier, code dispatched to another thread simply won't run at all on WebGL. I haven't encountered any concurrency issues with how threads behave at the OS level on other platforms. However, almost every Unity API call will throw errors if we try to make it from a thread other than the main one. Experienced Unity developers might point to the Job System, but as far as I know, it only provides limited access to &lt;code&gt;Transform&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;On the one hand, this is nice. We don't have to struggle too much with synchronization, and neither do Unity3D developers. Many Unity developers don't even know about the &lt;code&gt;lock&lt;/code&gt; keyword, which is considered one of the cornerstones of concurrency in C#. On the other hand, we may sometimes want to use multiple CPU cores to modify the properties of a large number of Unity objects.&lt;/p&gt;

&lt;p&gt;Still, Unity makes extensive use of concurrency under the hood, and its API provides asynchronous operations for loading data from disk, handling network communication, and performing some other resource-intensive tasks. That said, developers at Unity Technologies aren't particularly fond of the &lt;code&gt;async&lt;/code&gt;/&lt;code&gt;await&lt;/code&gt; syntax, so we often have to make do with callbacks and events. Of course, nothing stops us from wrapping these in a proper async API using &lt;code&gt;TaskCompletionSource&lt;/code&gt; and a few other methods.&lt;/p&gt;

&lt;p&gt;Unity also offers a rather unconventional approach to asynchronous programming called coroutines. Basically, coroutines enable developers to pass an enumeration to the Unity3D engine via its API. Each element tells the engine when to retrieve the next one. When combined with the &lt;code&gt;yield&lt;/code&gt; operator, this works as a makeshift version of &lt;code&gt;async&lt;/code&gt;/&lt;code&gt;await&lt;/code&gt;. It 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="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;IEnumerator&lt;/span&gt; &lt;span class="nf"&gt;Coroutine&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Do something"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="n"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;WaitForNextFrame&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="n"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Do something in the next frame"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="n"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;WaitForSeconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&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;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Do something after two seconds"&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;t&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;Delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="n"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;WaitUntil&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;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Completed&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="n"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Do something after 4 more seconds."&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;someMonoBehaviourInstance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StartCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Coroutine&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 may have noticed that starting a coroutine requires calling a method on a specific object instance. This tight coupling to an object is one of the advantages of coroutines: Unity cancels a coroutine when we destroy its associated object using &lt;code&gt;UnityEngine.Object.Destroy(obj)&lt;/code&gt;. In every other respect, though, coroutines are simply a less convenient alternative to &lt;code&gt;async&lt;/code&gt;/&lt;code&gt;await&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Unity3D also has its share of issues with &lt;code&gt;Task&lt;/code&gt;. Firstly, it runs on the thread pool, so we run into the same problem: we can't use the Unity API from those threads. Secondly, Unity3D developers try to avoid allocating memory on the heap, and &lt;code&gt;Task&lt;/code&gt; is a class that allocates memory on the heap. &lt;code&gt;ValueTask&lt;/code&gt; helps reduce this overhead, as does &lt;a href="https://github.com/Cysharp/UniTask" rel="noopener noreferrer"&gt;UniTask&lt;/a&gt;, its Unity-specific counterpart. UniTask is an open-source plugin that offers an alternative to &lt;code&gt;ValueTask&lt;/code&gt;. It provides additional optimizations and enhances the user experience when working with the Unity API. The library also includes various extension methods for Unity classes, providing convenient ways to use them with &lt;code&gt;await&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Unity3D also has its own implementation of &lt;code&gt;SynchronizationContext&lt;/code&gt;. It slightly streamlines the use of &lt;code&gt;Task&lt;/code&gt; and ensures that the code after &lt;code&gt;await&lt;/code&gt; won't run on a different thread. If the object we're &lt;code&gt;await&lt;/code&gt;-ing executes on Unity's main thread, the code after &lt;code&gt;await&lt;/code&gt; will run as soon as the execution finishes. If we use concurrency instead, the code after &lt;code&gt;await&lt;/code&gt; will run during an early stage in the Script Lifecycle. WPF uses a similar synchronization context.&lt;/p&gt;

&lt;h3&gt;
  
  
  Covering code with automated tests
&lt;/h3&gt;

&lt;p&gt;Unity includes a built-in editor window for running and viewing tests. The test framework is based on NUnit, but there are a few differences. First, Unity supports two types of automated tests: &lt;code&gt;PlayMode&lt;/code&gt; and &lt;code&gt;EditMode&lt;/code&gt;, which are just regular NUnit tests. We can create usual C# objects, mock dependencies using a mocking library such as NSubstitute (also available through OpenUPM), perform various operations, and check the results. &lt;code&gt;PlayMode&lt;/code&gt; tests, on the other hand, run the Script Lifecycle. They enable us to check how the &lt;code&gt;MonoBehaviour&lt;/code&gt; subclasses behave over time, how they respond to various physics triggers in the Script Lifecycle, and so on.&lt;/p&gt;

&lt;p&gt;In my experience, relatively few projects have made extensive use of &lt;code&gt;PlayMode&lt;/code&gt; tests. Most teams move as much business logic as possible into standard C# classes. This leaves the &lt;code&gt;MonoBehaviour&lt;/code&gt; subclasses as a thin layer between the C# architecture and the Unity API. I think the main reason is that testing anything related to the Script Lifecycle can be challenging. The engine's physics and execution systems don't provide APIs that offer the level of fine-tuning often required when writing automated tests.&lt;/p&gt;

&lt;p&gt;One disadvantage of testing in Unity is the lack of support for asynchronous tests. This is where the coroutines mentioned above come in, replacing asynchronous operations. It 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="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;UnityTest&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;// This is a special attribute for such tests&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;IEnumerator&lt;/span&gt; &lt;span class="nf"&gt;AsyncTest&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Arrange&lt;/span&gt;
  &lt;span class="n"&gt;SomeService&lt;/span&gt; &lt;span class="n"&gt;someSerivce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="c1"&gt;// create some service with mocked dependencies&lt;/span&gt;

  &lt;span class="c1"&gt;// Act&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="kt"&gt;bool&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;asyncHandle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;someService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SomeAsyncMethod&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="n"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;WaitUntil&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;asyncHandle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsCompleted&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Arrange&lt;/span&gt;
  &lt;span class="n"&gt;Assert&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="n"&gt;asyncHandle&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Those were the main points to keep in mind when starting to write C# code in Unity3D. If you have any comments or notice any errors, please leave a comment on the article or open an &lt;a href="https://github.com/kekchpek/articles/tree/master/DotNetToUnity" rel="noopener noreferrer"&gt;Issue on GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Want to learn more?
&lt;/h2&gt;

&lt;p&gt;The PVS-Studio team loves to publish valuable content for C# developers and folks working in game development. &lt;a href="https://pvs-studio.com/en/subscribe/" rel="noopener noreferrer"&gt;Subscribe to our newsletter&lt;/a&gt; to follow the articles from the PVS-Studio website.&lt;/p&gt;

&lt;p&gt;Here are some articles:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/java/1417/" rel="noopener noreferrer"&gt;I found some bugs in Create mod and checked them in Minecraft&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1422/" rel="noopener noreferrer"&gt;Game++. Part 2.2: Types of optimization&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/csharp/1421/" rel="noopener noreferrer"&gt;What's new in C# 15: Overview&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>unity3d</category>
      <category>csharp</category>
      <category>dotnet</category>
      <category>gamedev</category>
    </item>
    <item>
      <title>What Is PVS-Studio?</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Mon, 02 Nov 2026 12:40:38 +0000</pubDate>
      <link>https://dev.to/pvsdev/what-is-pvs-studio-52di</link>
      <guid>https://dev.to/pvsdev/what-is-pvs-studio-52di</guid>
      <description>&lt;p&gt;&lt;strong&gt;Static Analysis for Better Code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PVS-Studio is a Static Application Security Testing (SAST) tool that detects issues in source code, from simple typos to potential vulnerabilities. It offers over 1,300 diagnostic rules for C, C++, C#, Java, JavaScript, TypeScript, and Go, including specialized rules for particular technologies and support for standards such as MISRA and AUTOSAR.&lt;/p&gt;

&lt;p&gt;Watch the video to learn more:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;what languages and standards the analyzer supports;&lt;/li&gt;
&lt;li&gt;how it works;&lt;/li&gt;
&lt;li&gt;where it fits into your development workflow;&lt;/li&gt;
&lt;li&gt;and the kinds of bugs it can catch.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://pvs-studio.com/what_is_pvs_video_en?utm_source=devto&amp;amp;utm_medium=pvs&amp;amp;utm_campaign=video&amp;amp;utm_content=what_is_pvs" rel="noopener noreferrer"&gt;Visit our website to get a free 30-day trial!&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>security</category>
      <category>softwaredevelopment</category>
      <category>debugging</category>
    </item>
    <item>
      <title>Pitfalls of bit fields in C++</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Fri, 30 Oct 2026 13:26:49 +0000</pubDate>
      <link>https://dev.to/pvsdev/pitfalls-of-bit-fields-in-c-3685</link>
      <guid>https://dev.to/pvsdev/pitfalls-of-bit-fields-in-c-3685</guid>
      <description>&lt;p&gt;C++ is a language that lets developers do absolutely anything, and then it just silently watches as they blow everything up. We're used to thinking in terms of bytes, but what if the standard type system seems too wasteful? When your inner perfectionist demands compactness, bit fields are the best solution. Why use an entire eight-bit container for a simple yes-or-no flag when you can carve out exactly one bit with surgical precision? Add a colon, and suddenly you're the master of memory. However, when it comes to saving memory, order and predictability end. A programmer is left to deal with the subtleties of standards and the behavior of specific platforms. Let's take a look under the hood of C++ and see how bit fields 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%2F2sd25e98914rrgvfqfmb.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%2F2sd25e98914rrgvfqfmb.png" alt="1423_Bitfields/image1.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;How did I end up here, and why did I even decide to cover this topic?&lt;/p&gt;

&lt;p&gt;For quite some time, I've been working on supporting the MISRA C++ 2023 rules. Yes, PVS-Studio analyzer learns how to check whether a codebase complies with this standard. My task was to implement support for a rule that regulates the co-use of enumerations and bit fields. It all looked simple in my head: all I had to do was check whether the &lt;code&gt;enum&lt;/code&gt; values with a fixed fundamental data type fit within the specified bit field width.&lt;/p&gt;

&lt;p&gt;Have you ever started a task that seemed like a fun little adventure that would take just a couple of hours, only to find yourself stuck in a bottomless pit of troubleshooting for an entire week? That's exactly what happened to me. I got bogged down in trying to implement support for this rule.&lt;/p&gt;

&lt;p&gt;As I tried to untangle the mess, I realized one thing: bit fields in C++ are an underrated topic that every C++ developer should know about.&lt;/p&gt;

&lt;h2&gt;
  
  
  The basics: what are bit fields?
&lt;/h2&gt;

&lt;p&gt;In C++, a bit field is a data-packing mechanism that allows you to allocate memory within a structure or class bit by bit instead of byte by byte. Unlike ordinary types, which always occupy at least one byte, bit fields enable you to allocate exactly the number of bits you want to a particular variable. This makes them invaluable when memory is strictly limited or when you need to store a set of flags as compactly as possible.&lt;/p&gt;

&lt;p&gt;Let's take a look at the syntax for declaring bit fields. It may differ slightly depending on your technical requirements, but the fundamental structure is as follows:&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="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;specifier&lt;/span&gt; &lt;span class="n"&gt;declarator&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;constant&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;expression&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;constant-expression&lt;/code&gt; specifies how many bits to allocate to a particular field.&lt;/p&gt;

&lt;p&gt;The syntax for bit fields is quite interesting. We use a colon to give the compiler a direct instruction to override the usual memory allocation rules. In standard C++ code, the smallest addressable unit is a byte, which contains &lt;code&gt;CHAR_BIT&lt;/code&gt; bits. The implementation defines the &lt;code&gt;CHAR_BIT&lt;/code&gt; macro, but its most common value is eight. The colon separates the variable's logical name from its actual size. This forces the compiler to divide memory into small chunks of a specific size.&lt;/p&gt;

&lt;p&gt;This approach saves memory. How? It eliminates empty and redundant bits. When you declare standard variables, the processor rounds their size up to whole bytes due to architectural constraints. Bit fields work around this limitation by packing multiple fields into a shared storage unit.&lt;/p&gt;

&lt;p&gt;To understand how bit fields save space, we'll compare two ways of storing the same data. Let's say we need to record three system parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;activity status (on/off): 1 bit is enough (values 0 or 1);&lt;/li&gt;
&lt;li&gt;mode (4 modes): 2 bits are enough (combinations 00, 01, 10, and 11);&lt;/li&gt;
&lt;li&gt;error code (from 0 to 7): 3 bits are enough (combinations from 000 to 111).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All the essential information about the object state takes up exactly six bits.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Option 1. Standard structure&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If we create a standard structure in C++, the compiler allocates one byte of memory for each type, since this is the minimum addressable size:&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;struct&lt;/span&gt; &lt;span class="nc"&gt;StandardStruct&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;is_active&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   
  &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        
  &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;error_code&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;
  The full code
  &lt;br&gt;
&lt;a href="https://godbolt.org/z/1s9v7hhc9" rel="noopener noreferrer"&gt;The example:&lt;/a&gt;&lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;StandardStruct&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;is_active&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   
  &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        
  &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;error_code&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="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;StandardStruct&lt;/span&gt; &lt;span class="n"&gt;stStruct&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;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Sizeof stStruct: "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stStruct&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;"byte(s)"&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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;endl&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;

&lt;p&gt;&lt;/p&gt;
  Program output
  &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;Sizeof&lt;/span&gt; &lt;span class="n"&gt;stStruct&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="n"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&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;

&lt;p&gt;The program uses 24 memory bits. 24 memory cells were allocated for our six units of information; the remaining 18 cells simply remain empty because the processor architecture can't directly handle units smaller than one byte.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Option 2. Structure using bit fields&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now let's add the colon syntax. The compiler starts with a fundamental data type, or container, such as &lt;code&gt;unsigned char&lt;/code&gt;, which is one byte (eight bits) in size. It then begins packing fields tightly inside the container:&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;struct&lt;/span&gt; &lt;span class="nc"&gt;BitFieldStruct&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;char&lt;/span&gt; &lt;span class="n"&gt;is_active&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;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt;       &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;error_code&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&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;
  The full code
  &lt;br&gt;
&lt;a href="https://godbolt.org/z/9PqjcvoT8" rel="noopener noreferrer"&gt;The example:&lt;/a&gt;&lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;BitFieldStruct&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;char&lt;/span&gt; &lt;span class="n"&gt;is_active&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;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt;       &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;error_code&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&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="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;BitFieldStruct&lt;/span&gt; &lt;span class="n"&gt;stStruct&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;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Sizeof stStruct: "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stStruct&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;" byte(s)"&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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;endl&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;

&lt;p&gt;&lt;/p&gt;
  Program output
  &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;Sizeof&lt;/span&gt; &lt;span class="n"&gt;stStruct&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="n"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&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;

&lt;p&gt;As a result of this mechanism, the entire structure is compressed to one byte. In memory, it appears as a single eight-bit array divided into sectors: &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%2F9saacc217a59eb7eus6v.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%2F9saacc217a59eb7eus6v.png" alt="1423_Bitfields/image2.png" width="730" height="609"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The compiler handles everything: when reading from or writing to the &lt;code&gt;mode&lt;/code&gt; variable, it automatically generates hidden bit shifts and masks to isolate the required two bits from the rest of the data. This high-density packaging significantly reduces RAM usage in large arrays and embedded systems (microcontrollers).&lt;/p&gt;

&lt;p&gt;How bit fields work and their limitations&lt;/p&gt;

&lt;p&gt;We've covered the basic principles and syntax of bit fields. Now let's take a closer look at how they work and explore the rules imposed by the language.&lt;/p&gt;

&lt;p&gt;Bit fields can exist only inside structures or classes. Developers can't declare a bit field as a standalone variable inside a function. It's also impossible to apply the &lt;code&gt;const&lt;/code&gt; or &lt;code&gt;volatile&lt;/code&gt; qualifiers directly to an individual bit field. Instead, you can apply them indirectly by declaring an object of that structure or class with the relevant qualifier.&lt;/p&gt;

&lt;h3&gt;
  
  
  Data types for bit fields
&lt;/h3&gt;

&lt;p&gt;The next thing to discuss is which data types you can use with bit fields. The C++ standard allows this mechanism only for &lt;em&gt;integral&lt;/em&gt; types and &lt;em&gt;enumerations&lt;/em&gt;. Trying to limit the bit width of floating-point types (&lt;code&gt;float&lt;/code&gt;, &lt;code&gt;double&lt;/code&gt;) or complex objects such as &lt;code&gt;std::string&lt;/code&gt; is technically impossible and will result in a compilation error.&lt;/p&gt;

&lt;p&gt;Bit fields support the following types:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;1&lt;/em&gt;. Integral types (&lt;code&gt;char&lt;/code&gt;, &lt;code&gt;wchar_t&lt;/code&gt;, &lt;code&gt;char8_t&lt;/code&gt;, &lt;code&gt;char16_t&lt;/code&gt;, &lt;code&gt;char32_t&lt;/code&gt;, &lt;code&gt;bool&lt;/code&gt;, &lt;code&gt;short&lt;/code&gt;, &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;long&lt;/code&gt;, and &lt;code&gt;long long&lt;/code&gt;)&lt;em&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;These are the standard options for creating masks and flags. Before C++14, explicitly specifying the &lt;code&gt;signed&lt;/code&gt; or &lt;code&gt;unsigned&lt;/code&gt; modifier for an &lt;code&gt;int&lt;/code&gt; bit field was critical. Otherwise, the compiler determined how to interpret the signed bit. For example, a one-bit &lt;code&gt;int&lt;/code&gt; field without an explicit unsigned modifier could take 0 and -1 instead of the expected 0 and 1. For a &lt;code&gt;bool&lt;/code&gt; field, allocating exactly one bit is enough.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;2. _Enumerations (&lt;code&gt;enum&lt;/code&gt; and &lt;code&gt;enum class&lt;/code&gt;)&lt;/em&gt;._&lt;/p&gt;

&lt;p&gt;Bit fields work well for storing &lt;code&gt;enum&lt;/code&gt; types with a small set of possible values. If an &lt;code&gt;enum&lt;/code&gt; has four possible states, allocating two bits is enough.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;3. _&lt;code&gt;std::byte&lt;/code&gt; (_C++20).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Starting with &lt;a href="https://timsong-cpp.github.io/cppwp/n5050/cstddef.syn#lib:byte" rel="noopener noreferrer"&gt;C++20&lt;/a&gt;, &lt;code&gt;std::byte&lt;/code&gt; officially joined the list of permitted types. If a structure handles raw data at the hardware level, using &lt;code&gt;std::byte&lt;/code&gt; clearly communicates the developer's intent and prevents accidental arithmetic errors.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rules for working with bit fields
&lt;/h3&gt;

&lt;p&gt;The third important topic to cover is the rules governing bit fields. In C++, four key restrictions apply. &lt;strong&gt;Developers cannot&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;declare an array of bit fields;&lt;/li&gt;
&lt;li&gt;take the address of a bit field;&lt;/li&gt;
&lt;li&gt;create a pointer to a bit field;&lt;/li&gt;
&lt;li&gt;create a reference to a bit field.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's look at some examples to understand why bit fields follow these rules and what happens when you try to get around them. We'll start with a simple structure:&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;struct&lt;/span&gt; &lt;span class="nc"&gt;HardwareFlags&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;mode&lt;/span&gt;   &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&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;status&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="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  No taking addresses or creating pointers
&lt;/h4&gt;

&lt;p&gt;The rule against taking addresses or creating pointers stems from a fundamental aspect of computer architecture: a bit field has no physical memory address. Modern processor architectures use the byte as the smallest addressable unit of memory (&lt;code&gt;CHAR_BIT bits&lt;/code&gt;). A processor simply has no way to address, say, the third bit within a byte.&lt;/p&gt;

&lt;p&gt;For this reason, we can't use the address-of operator &lt;code&gt;&amp;amp;&lt;/code&gt; on a bit field:&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;HardwareFlags&lt;/span&gt; &lt;span class="n"&gt;flags&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;address&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;flags&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mode&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;*&lt;/span&gt; &lt;span class="n"&gt;ptr&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;flags&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// doesn't compile&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since individual bit segments don't have unique addresses in a memory cell, it's physically impossible to obtain a direct pointer to such a variable.&lt;/p&gt;

&lt;h4&gt;
  
  
  No address, no references
&lt;/h4&gt;

&lt;p&gt;There's another restriction that comes from the inability to take a bit field's address: you can't bind a standard reference &lt;code&gt;&amp;amp;&lt;/code&gt; to a bit field. In C++, references rely on pointers and memory addressing. Without an address, there's simply nothing for a reference to bind to:&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;HardwareFlags&lt;/span&gt; &lt;span class="n"&gt;flags&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;checkMode&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;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&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="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;ref&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;flags&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// doesn't compile&lt;/span&gt;
&lt;span class="n"&gt;checkMode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;flags&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;                 &lt;span class="c1"&gt;// doesn't compile&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Arrays: why pointer arithmetic falls short
&lt;/h4&gt;

&lt;p&gt;I'm afraid I have some bad news: you can't create an array of bit fields either. The reason lies in pointer arithmetic. When you create an array, the compiler guarantees that its elements occupy consecutive memory locations. When you access the &lt;code&gt;i&lt;/code&gt;-th element using &lt;code&gt;arr[i]&lt;/code&gt;, the compiler calculates its address relative to the start of the array via pointer arithmetic.&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;struct&lt;/span&gt; &lt;span class="nc"&gt;BrokenStruct&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;items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// doesn't compile&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The anatomy of alignment
&lt;/h2&gt;

&lt;p&gt;Although bit fields aim to save memory, C++ doesn't specify their exact layout within a structure. Compiler developers have considerable freedom in how they pack bits, so a structure's actual size depends on the target platform. This flexibility is impressive, but to understand why it exists, let's take a look at &lt;a href="https://timsong-cpp.github.io/cppwp/n5050/class.bit#1" rel="noopener noreferrer"&gt;what the standard says&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Allocation of bit-fields within a class object is implementation-defined. Alignment of bit-fields is implementation-defined. Bit-fields are packed into some addressable allocation unit.&lt;/p&gt;

&lt;p&gt;[Note 1: Bit-fields straddle allocation units on some machines and not on others. Bit-fields are assigned right-to-left on some machines, left-to-right on others. — end note]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In reality, this means that the layout rules come not from the language, but from the &lt;a href="https://en.wikipedia.org/wiki/Application_binary_interface" rel="noopener noreferrer"&gt;Application Binary Interface&lt;/a&gt; (ABI) defined for a particular OS. For example, many Unix-like systems use the &lt;a href="https://itanium-cxx-abi.github.io/cxx-abi/abi.html" rel="noopener noreferrer"&gt;Itanium ABI&lt;/a&gt;, but it also leaves some aspects of bit ordering unspecified. The packing rules can vary depending on the OS and architecture. For example, MSVC on Windows and GCC on Linux may compile the same structure differently.&lt;/p&gt;

&lt;p&gt;In this section, we'll focus specifically on how bit fields work. I'll touch on alignment briefly, as I've already covered it in a series of articles: the &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1339/" rel="noopener noreferrer"&gt;first part&lt;/a&gt; introduces the basic principles, while the &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1340/" rel="noopener noreferrer"&gt;second&lt;/a&gt; and &lt;a href="https://pvs-studio.com/en/blog/posts/cpp/1369/" rel="noopener noreferrer"&gt;third&lt;/a&gt; explore how inheritance and virtual functions affect alignment.&lt;/p&gt;

&lt;h3&gt;
  
  
  How compilers divide memory into allocation units
&lt;/h3&gt;

&lt;p&gt;The phrase "Bit fields are packed into some addressable allocation unit" describes the concept of an &lt;a href="https://timsong-cpp.github.io/cppwp/n5050/class.bit#1" rel="noopener noreferrer"&gt;&lt;em&gt;allocation unit&lt;/em&gt;&lt;/a&gt;. According to the C++ standard, an allocation unit is an addressable block of hardware memory, such as a byte or machine word, into which bit-fields are packed consecutively. The exact behavior of these units is implementation-defined and depends largely on the platform's ABI.&lt;/p&gt;

&lt;p&gt;A compiler never allocates memory for individual bits out of thin air. Instead, it starts with the fundamental data type you specify when declaring a field and uses it to determine the size of the physical container. For example, if you use &lt;code&gt;unsigned char&lt;/code&gt;, the allocation unit is one byte (8 bits). If you choose &lt;code&gt;unsigned int&lt;/code&gt;, it occupies four bytes (32 bits). The compiler takes this entire physical container and starts slicing it into the specified logical segments, one bit at a time, until no space remains. Once the container runs out of room, the compiler allocates a new unit for the next fields.&lt;/p&gt;

&lt;p&gt;Let's look at how a compiler packs data:&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%2F8026rcborreqjruam1kw.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%2F8026rcborreqjruam1kw.png" alt="1423_Bitfields/image3.png" width="800" height="957"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;
  The packing algorithm
  &lt;ol&gt;
&lt;li&gt;The compiler receives a list of bit fields declared in a structure.&lt;/li&gt;
&lt;li&gt;It allocates the first container (memory allocation unit) based on the size of the fundamental data type.&lt;/li&gt;
&lt;li&gt;The first bit field occupies the beginning of the newly allocated container.&lt;/li&gt;
&lt;li&gt;The compiler calculates how many free bits remain in the current container.&lt;/li&gt;
&lt;li&gt;It takes the next bit field from the list for analysis.&lt;/li&gt;
&lt;li&gt;It compares the field's width in bits with the number of free bits remaining.&lt;/li&gt;
&lt;li&gt;If the field fully fits in the remaining space, the compiler places it in the current container, directly next to the previous one.&lt;/li&gt;
&lt;li&gt;After successfully placing the field, the compiler updates the free-bit counter and moves on to the next field.&lt;/li&gt;
&lt;li&gt;If the field's width exceeds the available space, the compiler must resolve a standard ambiguity.&lt;/li&gt;
&lt;li&gt;The compiler consults the ABI rules for the target architecture. On x86 and ARM, the compiler moves the entire field to a new container to preserve CPU performance. On other architectures, such as some RISC processors, it may split the field into two parts to maximize memory saving, filling the remaining space in the current container.&lt;/li&gt;
&lt;li&gt;The compiler closes the current container, leaving any unused bits empty.&lt;/li&gt;
&lt;li&gt;It opens a new, empty container (allocation unit).&lt;/li&gt;
&lt;li&gt;The field that didn't fit occupies the beginning of the new container.&lt;/li&gt;
&lt;li&gt;The compiler then recalculates the number of free bits in the new container and repeats these checks for the remaining fields.&lt;/li&gt;
&lt;li&gt;The process is complete once the compiler has assigned all bit fields in the list to their respective containers.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Note.&lt;/strong&gt; The direction in which the compiler fills a container depends on the ABI rules for the target architecture. The order can be from least to most significant bit or vice versa.&lt;/p&gt;



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

&lt;h3&gt;
  
  
  sizeof bloat: good vs. bad alignment
&lt;/h3&gt;

&lt;p&gt;Why are we even going into such detail about the compiler's step-by-step algorithm? Bit fields can become a ticking time bomb if one doesn't understand these inner workings. You write code expecting to save memory, but end up with a structure that's several times larger than normal. Declaring bit fields haphazardly can trigger sizeof bloat. The compiler loses its capability to pack data tightly and starts inserting unused bytes.&lt;/p&gt;

&lt;p&gt;Two classic developer mistakes usually lead to such hidden memory leaks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creating giant fields whose widths exceed the physical capacity of the current memory container.&lt;/li&gt;
&lt;li&gt;Mixing data types indiscriminately, so adjacent fields keep switching between fundamental data types (for example, &lt;code&gt;char&lt;/code&gt;, then &lt;code&gt;int&lt;/code&gt;, and then &lt;code&gt;char&lt;/code&gt; again). This is a critical issue on Windows (Microsoft ABI), where the compiler starts a new allocation unit whenever the type changes, leaving unused padding behind. This limitation doesn't affect GCC and Clang on Linux and macOS: they ignore changes in base types and continuously pack fields, minimizing alignment padding.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To see just how much of a difference this makes, let's compare two scenarios: one that shows how not to do it, and one that shows how to do it right.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example N1. Inefficient layout (poor alignment)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine a structure where we've tried to save memory by mixing different &lt;a href="https://godbolt.org/z/Gfohbh69o" rel="noopener noreferrer"&gt;types&lt;/a&gt;. For this example, let's use MSVC on Windows:&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;struct&lt;/span&gt; &lt;span class="nc"&gt;BadPacking&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;char&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&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;b&lt;/span&gt;  &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&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;char&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&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 structure fields will be arranged as follows:&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%2Ff3kj0aaz5ldyam5znled.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%2Ff3kj0aaz5ldyam5znled.png" alt=" " width="800" height="453"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The constant switching between types (&lt;code&gt;char&lt;/code&gt; &amp;gt; &lt;code&gt;int&lt;/code&gt; &amp;gt; &lt;code&gt;char&lt;/code&gt;) completely disrupts continuous packing. MSVC treats each type change as a command to close the current allocation unit. It stops filling the byte after &lt;code&gt;a&lt;/code&gt;, leaves the remaining bits as padding, and aligns the address for the next block. It then opens a 4-byte container for the &lt;code&gt;int&lt;/code&gt; &lt;code&gt;b&lt;/code&gt; field, which takes three bits, and immediately closes it to open a container for the &lt;code&gt;char&lt;/code&gt; type. As a result, large gaps of unused memory remain. The compiler also has to follow alignment rules, so it performs final alignment on the entire structure. Then, &lt;code&gt;sizeof(BadPacking)&lt;/code&gt; returns 12 bytes. Clang and GCC, on the other hand, can handle this structure efficiently: they know how to pack these fields as tightly as possible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example N2. Optimized layout (good alignment)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To fix this, simply follow the golden rule: convert all fields to a single base &lt;a href="https://godbolt.org/z/jzKxvfxKx" rel="noopener noreferrer"&gt;type&lt;/a&gt; and group them by meaning.&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;struct&lt;/span&gt; &lt;span class="nc"&gt;GoodPacking&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;b&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&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;a&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&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;c&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&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 take a look at how the elements are arranged in the structure now:&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%2Fqj9b3fe4a1pden6yiw8v.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%2Fqj9b3fe4a1pden6yiw8v.png" alt=" " width="800" height="332"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The compiler now works with uniform and predictable four-byte blocks. The &lt;code&gt;b&lt;/code&gt; field occupies almost the entire first block, while the &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; fields require only eight bits combined, fitting perfectly into the second &lt;code&gt;int&lt;/code&gt; block. As a result, &lt;code&gt;sizeof(GoodPacking)&lt;/code&gt; returns just eight bytes (exactly two &lt;code&gt;int&lt;/code&gt; containers). The data is packed as tightly as possible so the processor can access it without any unnecessary overhead. This gives us a compact structure.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hardware padding: reserving space for unused bits
&lt;/h3&gt;

&lt;p&gt;When writing drivers, implementing network protocols, or working with processor registers, you may encounter hardware controllers with gaps in their bit layout. For example, a chip manufacturer may reserve certain bits in a hardware byte for future use, meaning you can't write or read data to or from them.&lt;/p&gt;

&lt;p&gt;To precisely mirror the register's physical layout in code, you can use &lt;a href="https://timsong-cpp.github.io/cppwp/n5050/class.bit#2" rel="noopener noreferrer"&gt;unnamed bit fields with non-zero widths&lt;/a&gt; as placeholders or padding:&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;struct&lt;/span&gt; &lt;span class="nc"&gt;TestController&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;char&lt;/span&gt; &lt;span class="n"&gt;transmit_ready&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;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;                &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&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;char&lt;/span&gt; &lt;span class="n"&gt;receive_ready&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;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;                &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&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;However, they still occupy their designated space in memory when the compiler generates the structure. This enables you to map a C++ structure to the microcontroller's physical memory layout with bit-level precision, without shifting any of the meaningful flags.&lt;/p&gt;

&lt;p&gt;Forcing a packing boundary: why use a zero-width bit field?&lt;/p&gt;

&lt;p&gt;In some cases, such as when working with hardware interfaces, it's necessary to prevent the compiler from packing fields into the current allocation unit. For example, you may need to ensure that the next data block starts at a new byte or machine word. To do this, you can use an &lt;a href="https://timsong-cpp.github.io/cppwp/n5050/class.bit#2" rel="noopener noreferrer"&gt;unnamed zero-width bit field&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;struct&lt;/span&gt; &lt;span class="nc"&gt;Test&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;char&lt;/span&gt; &lt;span class="n"&gt;command&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&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;char&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="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;    &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&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;A zero-width bit field acts as an artificial boundary. It tells the compiler that the current memory allocation unit is closed. The next field must start in a new allocation unit and can't use any remaining bits, even if plenty of space remains. One important rule to remember when working with these fields is that you &lt;strong&gt;can't give a zero-width bit field a name&lt;/strong&gt;. It must always remain unnamed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Under the hood: how compilers store bit fields
&lt;/h2&gt;

&lt;p&gt;Now, it's time to take a look under the hood. The internal structure of bit fields in C++ is subject to implementation-defined behavior. The C++ standard abstracts away the physical details, providing only high-level rules and delegating the challenging tasks to the compiler and ABI. To understand how data actually occupies memory, we need to take a closer look at three fundamental factors: bit allocation order, compiler differences across architectures, and binary portability.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bit allocation order
&lt;/h3&gt;

&lt;p&gt;The first and most important factor that determines a structure's physical layout in memory is the order in which the compiler places bits inside an allocated container. Most developers are familiar with byte order, which determines how multibyte values are mapped to memory addresses. A similar concept exists at a lower level, though: the compiler decides which direction to fill individual bits within a particular byte or machine word. The C++ standard deliberately leaves this detail unspecified, so each compiler's implementation determines how it fills each container. In real world, compilers use one of two opposite approaches: packing from the least significant bit to the most significant bit (LSB-first, Little Endian) or from the most significant bit to the least significant bit (MSB-first, Big Endian).&lt;/p&gt;

&lt;p&gt;In the &lt;a href="https://en.wikipedia.org/wiki/Bit_numbering#Least_significant_bit" rel="noopener noreferrer"&gt;&lt;em&gt;LSB-first&lt;/em&gt;&lt;/a&gt; scheme, the compiler fills the allocated container from right to left. In other words, packing begins with the least significant bit, or bit 0. The first bit field declared in the structure occupies the rightmost bits, and each subsequent field sits directly next to it, extending leftward toward bits with greater binary significance. GCC, Clang, and MSVC all use this approach when compiling code for architectures such as x86, x86-64, and ARM.&lt;/p&gt;

&lt;p&gt;The opposite strategy is &lt;a href="https://en.wikipedia.org/wiki/Bit_numbering#Most_significant_bit" rel="noopener noreferrer"&gt;&lt;em&gt;MSB-first (Most Significant Bit-first)&lt;/em&gt;&lt;/a&gt;. In this case, the compiler packs bits from left to right, starting with the highest available bit: for example, the seventh bit in a one-byte container. The first field in the structure occupies the most significant bits on the left side of the container, while subsequent fields follow in sequence toward the least significant bits. This is common in compilers for &lt;a href="https://en.wikipedia.org/wiki/Network_processor" rel="noopener noreferrer"&gt;network processors&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let's see how this works in real code. Take a look at the following &lt;a href="https://godbolt.org/z/MYM1x8rPK" rel="noopener noreferrer"&gt;structure&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;struct&lt;/span&gt; &lt;span class="nc"&gt;HeaderFlags&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;    &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;      &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&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;
  The full code
  &lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;cstdint&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;cstring&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;HeaderFlags&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;    &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;      &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;result_byte&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="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;HeaderFlags&lt;/span&gt; &lt;span class="n"&gt;flags&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;flags&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;version&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;flags&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;flags&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&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;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;memcpy&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;result_byte&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;flags&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HeaderFlags&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;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;&lt;/p&gt;

&lt;p&gt;We created a &lt;code&gt;HeaderFlags&lt;/code&gt; structure that occupies exactly one byte and assigned three values to its fields. To safely extract the packed byte without violating memory access rules, we used &lt;code&gt;std::memcpy&lt;/code&gt; and copied the result into the &lt;code&gt;result_byte&lt;/code&gt; global variable.&lt;/p&gt;

&lt;p&gt;Now let's see how different compilers handle this code. To avoid wading through unnecessary assembly instructions, we'll enable the &lt;code&gt;-O2&lt;/code&gt; optimization level.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://godbolt.org/z/6G4s9envG" rel="noopener noreferrer"&gt;GCC x86-64&lt;/a&gt; output:&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;globl&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;ios_base_library_init&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="s"&gt;"main"&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;BYTE&lt;/span&gt; &lt;span class="n"&gt;PTR&lt;/span&gt; &lt;span class="s"&gt;"result_byte"&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;rip&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;111&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;xor&lt;/span&gt;     &lt;span class="n"&gt;eax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;eax&lt;/span&gt;
        &lt;span class="n"&gt;ret&lt;/span&gt;
&lt;span class="s"&gt;"result_byte"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;zero&lt;/span&gt;   &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;mov&lt;/code&gt; instruction contains the &lt;code&gt;-111&lt;/code&gt; negative value, which is how x86-64 assembly represents one-byte constants. Converting it to the familiar unsigned format gives us &lt;code&gt;256 - 111 = 145&lt;/code&gt;, which is &lt;code&gt;0x89&lt;/code&gt; in hexadecimal or &lt;code&gt;0b1001'0001&lt;/code&gt; in binary. The compiler has eliminated all the intermediate packing instructions and written the final result directly to memory. Since the target cell occupies four bytes, the &lt;code&gt;0x00000089&lt;/code&gt; value appears in memory as the &lt;code&gt;89 00 00 00&lt;/code&gt; byte sequence. The fact that the least significant byte 89 appears at the lowest memory address confirms the LSB-first (Little Endian) layout. &lt;/p&gt;

&lt;p&gt;Switching the compiler to the Big-Endian MIPS architecture, such as mips-linux-gcc 16.1.0, reveals a completely different picture. The assembly output for this platform &lt;a href="https://godbolt.org/z/99hYqr6b4" rel="noopener noreferrer"&gt;looks as follows&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;globl&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;ios_base_library_init&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;lui&lt;/span&gt;     &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;hi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result_byte&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;li&lt;/span&gt;      &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;52&lt;/span&gt;                 &lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="mh"&gt;0x34&lt;/span&gt;
        &lt;span class="n"&gt;sb&lt;/span&gt;      &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;lo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result_byte&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;jr&lt;/span&gt;      &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;31&lt;/span&gt;
        &lt;span class="n"&gt;move&lt;/span&gt;    &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="n"&gt;result_byte&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;space&lt;/span&gt;  &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the &lt;code&gt;li $3, 52&lt;/code&gt; instruction loads the precomputed constant &lt;code&gt;52&lt;/code&gt;, which is &lt;code&gt;0x34&lt;/code&gt; in hexadecimal and &lt;code&gt;0b0011'0100&lt;/code&gt; in binary. The compiler uses the MSB-first strategy, packing bits from left to right: the &lt;code&gt;version&lt;/code&gt; field occupies the most significant bits 7–5, type occupies bits 4–3, and &lt;code&gt;id&lt;/code&gt; lands in the least significant bits 2–0. We can illustrate the bit layout schematically as follows:&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%2Fwbvw6zyjpiuiwlbczf5q.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%2Fwbvw6zyjpiuiwlbczf5q.png" alt="1423_Bitfields/image6.png" width="799" height="488"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This example shows how changing the bit endianness can completely change the physical representation of data. Directly transferring raw bytes between systems that use different bit-packing schemes can corrupt the data structure. Understanding this mechanism leads us to the second key factor: the architectural differences between compilers and the role of the ABI.&lt;/p&gt;

&lt;h3&gt;
  
  
  Platform dependence: the anatomy of the MSVC vs. GCC/Clang conflict
&lt;/h3&gt;

&lt;p&gt;We often think that fundamental data types in C++ behave deterministically on the same hardware architecture. However, bit fields have an important caveat: the compiler decides how to arrange them in memory. The in-memory layout of the same structure compiled for an x86-64 processor can differ significantly between Windows and Linux. The physical layout of bit fields follows the rules defined by the ABI. The ABI specifies how the operating system's platform expects the compiler to group, align, and divide data into individual fields.&lt;/p&gt;

&lt;p&gt;Several ABI standards define different approaches to data packing. Here are some of the most widely used ones:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://refspecs.linuxbase.org/elf/gabi41.pdf" rel="noopener noreferrer"&gt;System V ABI&lt;/a&gt; (based on the Itanium C++ ABI) is used by default in GCC and Clang on Linux, macOS, and FreeBSD. Although developers originally designed this standard for Intel Itanium processors, it has become a common foundation for GCC and Clang across various architectures, including &lt;a href="https://www.sco.com/developers/devspecs/abi386-4.pdf" rel="noopener noreferrer"&gt;x86&lt;/a&gt;, &lt;a href="https://refspecs.linuxbase.org/elf/x86_64-abi-0.99.pdf" rel="noopener noreferrer"&gt;x64&lt;/a&gt;, and &lt;a href="https://github.com/ARM-software/abi-aa/blob/main/sysvabi64/sysvabi64.rst" rel="noopener noreferrer"&gt;ARM64&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://docs.riscv.org/reference/abi/v1.0/index.html" rel="noopener noreferrer"&gt;RISC-V ABI&lt;/a&gt; is a specification for the RISC-V open-standard architecture. This standard isn't formally derived from the System V ABI, but it shares many similar data packing and alignment rules. The goal is to pack bit fields in memory as tightly as possible.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://learn.microsoft.com/en-us/cpp/build/x64-software-conventions?view=msvc-170" rel="noopener noreferrer"&gt;Microsoft x64 ABI&lt;/a&gt; is used by default in MSVC on Windows systems running on the x86-64 architecture.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://learn.microsoft.com/en-us/cpp/build/arm64-windows-abi-conventions?view=msvc-170" rel="noopener noreferrer"&gt;Microsoft ARM64 ABI&lt;/a&gt; is the modern Windows standard for ARM64 processors. It's designed with x64 limitations in mind. Its structure alignment rules offer greater flexibility, and its calling convention enables programs to pass data directly through registers, reducing stack usage.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These documents differ in a single rule.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The rule. Compatibility between different data types&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Developers can declare consecutive bit fields with different fundamental data types within the same structure. This kind of type mixing is one of the main causes of hidden memory bloat, as compilers use very different algorithms to handle boundaries between allocation units with different fundamental types.&lt;/p&gt;

&lt;p&gt;Let's see what happens when these rules conflict. We'll run an experiment using Compiler Explorer and measure the structure's size with the &lt;code&gt;sizeof&lt;/code&gt; operator. Let's write the following structure:&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;struct&lt;/span&gt; &lt;span class="nc"&gt;PackagingConflict&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;uint8_t&lt;/span&gt;  &lt;span class="n"&gt;mode&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&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;
  The full code
  &lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;cstdint&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;PackagingConflict&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;uint8_t&lt;/span&gt;  &lt;span class="n"&gt;mode&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&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="nf"&gt;main&lt;/span&gt;&lt;span class="p"&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;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"--- BITFIELD PACKING SIZES ---"&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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;endl&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;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"1. PackagingConflict size: "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;PackagingConflict&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;" byte(s)"&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;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;&lt;/p&gt;

&lt;p&gt;Look at the outputs from &lt;a href="https://godbolt.org/z/nY8WMGGT7" rel="noopener noreferrer"&gt;Clang&lt;/a&gt; and &lt;a href="https://godbolt.org/z/qrjj17vcE" rel="noopener noreferrer"&gt;MSVC&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;
  The Clang program output
  &lt;br&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;BITFIELD&lt;/span&gt; &lt;span class="n"&gt;PACKING&lt;/span&gt; &lt;span class="n"&gt;SIZES&lt;/span&gt; &lt;span class="o"&gt;---&lt;/span&gt;
&lt;span class="mf"&gt;1.&lt;/span&gt; &lt;span class="n"&gt;PackagingConflict&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="n"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&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;

&lt;p&gt;&lt;/p&gt;
  The MSVC program output
  &lt;br&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;BITFIELD&lt;/span&gt; &lt;span class="n"&gt;PACKING&lt;/span&gt; &lt;span class="n"&gt;SIZES&lt;/span&gt; &lt;span class="o"&gt;---&lt;/span&gt;
&lt;span class="mf"&gt;1.&lt;/span&gt; &lt;span class="n"&gt;PackagingConflict&lt;/span&gt; &lt;span class="n"&gt;size&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;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&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;

&lt;p&gt;Here's a visual layout of the memory components:&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%2Fvjl57xk8v098twn10w0u.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%2Fvjl57xk8v098twn10w0u.png" alt="1423_Bitfields/image7.png" width="799" height="446"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Upon analyzing this structure, we can spot a clear difference: Clang packs it into four bytes, while MSVC inflates it to eight. Since the fields use different fundamental data types (&lt;code&gt;uint8_t&lt;/code&gt; and &lt;code&gt;uint32_t&lt;/code&gt;), both compilers initially keep them separate. They allocate one byte for the mode and add three bytes of padding so the type starts at a properly aligned four-byte boundary. Clang then applies an Itanium ABI optimization, trimming the unused part of the second allocation unit to save space. MSVC, on the other hand, strictly follows the Microsoft ABI rule that the structure's total size must be a multiple of its largest fundamental type (&lt;code&gt;uint32_t&lt;/code&gt;, or four bytes). It preserves the full four-byte allocation unit for each block, doubling the structure's final size.&lt;/p&gt;

&lt;p&gt;The real danger is when we move between platforms with different ABIs, the well-defined physical boundaries of data in memory simply disappear. Hidden padding and differences in how compilers handle unused space within allocation units can completely change the internal field layout. This makes bit fields unsuitable for direct byte-by-byte copying, file storage, or binary serialization. If a structure is saved using Clang's four-byte layout and then read using MSVC's eight-byte layout, the program reads from incorrect offsets, resulting in corrupted data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Binary fragility and industry standards
&lt;/h3&gt;

&lt;p&gt;Due to inconsistencies in alignment and bit ordering, structures containing bit fields offer virtually no binary portability. C++ compilers treat bit fields as a black box: you write concise code, but leave the actual memory layout to chance.&lt;/p&gt;

&lt;p&gt;Using bit fields is virtually guaranteed to cause problems in three areas:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Network I/O.&lt;/strong&gt; Sending a structure directly over a socket with a call like &lt;code&gt;send(sock, &amp;amp;packet, sizeof(packet), 0)&lt;/code&gt; is a ticking time bomb. If the server runs Linux (Clang) and the client runs Windows (MSVC), ABI differences can shift bit positions, turning valid flags into a meaningless jumble.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. File I/O (serialization).&lt;/strong&gt; For example, writing bit fields directly to a file byte by byte with &lt;code&gt;fwrite&lt;/code&gt; ties the data to a specific ABI. According to Section &lt;a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2310.pdf#page=95" rel="noopener noreferrer"&gt;6.7.2.1&lt;/a&gt; of the ISO C standard, the order in which compilers lay out bit fields and padding is implementation-defined behavior. Writing such a structure to a file on Linux and then reading it on Windows can cause differences between the Microsoft and System V ABIs to result in the program reading corrupted data from incorrect offsets.&lt;/p&gt;

&lt;p&gt;The C++ object model in C++23 partially addresses the issue of object lifetimes with the &lt;a href="https://en.cppreference.com/cpp/memory/start_lifetime_as" rel="noopener noreferrer"&gt;&lt;code&gt;std::start_lifetime_as&lt;/code&gt;&lt;/a&gt; function. However, this function only validates the creation of an object from raw bytes in memory; it doesn't address differences in the physical layout of fields across platforms. Moreover, copying uninitialized structure padding when writing data to a file technically involves reading indeterminate values, which leads to undefined behavior.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Drivers and hardware (Hardware MMIO).&lt;/strong&gt; A microcontroller's hardware defines the bit layout of its registers, and it never changes. In C, however, the order and alignment of bit fields, as well as the width of memory accesses, depend on the implementation. So, describing a hardware register with bit fields doesn't guarantee that each field will map to the intended bits. Switching compilers, compiler versions, or build flags can shift the fields, causing writes to affect the wrong register bits.&lt;/p&gt;

&lt;p&gt;Developers avoid bit fields entirely for any task that requires a strictly defined data layout, such as network communication, file storage, or hardware registers. Instead, they use fixed-width integer types (&lt;code&gt;uint8_t&lt;/code&gt;, &lt;code&gt;uint16_t&lt;/code&gt;, and &lt;code&gt;uint32_t&lt;/code&gt;) combined with explicit bit shifts (&lt;code&gt;&amp;lt;&amp;lt;, &amp;gt;&amp;gt;&lt;/code&gt;) and bit masks (&lt;code&gt;&amp;amp;, |&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Differences between compiler ABIs make it difficult to use standard bit fields without manual alignment for cross-platform data exchange. A program compiled on Linux using GCC or Clang produces a data structure that a Windows application built with MSVC can't read correctly because the Windows application expects alignment and padding bits in different locations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use cases: where bit fields actually make sense
&lt;/h2&gt;

&lt;p&gt;In C++ development, bit fields serve specific engineering needs rather than abstract memory-saving goals. There are two main scenarios where this feature proves most useful.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Compact metadata for millions of objects.&lt;/strong&gt; Developers often need to store metadata, such as status flags, type IDs, and reference counts, for huge numbers of small elements when designing game engines, custom memory allocators, or databases. Allocating a separate variable for each flag can waste enormous amounts of memory due to compiler-imposed alignment. Bit fields let you pack several independent properties into a single byte, minimizing memory usage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Optimizing CPU cache usage (Cache Lines).&lt;/strong&gt; Modern processors don't read data from RAM one item at a time. Instead, they fetch large blocks of data called cache lines, typically 64 bytes each. When a data structure becomes too large, each cache line contains fewer useful objects. This forces the processor to repeatedly wait for slow RAM access, resulting in a cache miss. Using bit fields to pack data tightly allows the CPU cache to hold several times more objects. An algorithm can run several times faster when it iterates over objects sequentially in large batches because it retrieves data from the CPU's ultra-fast cache.&lt;/p&gt;

&lt;h2&gt;
  
  
  Modern alternatives to bit fields
&lt;/h2&gt;

&lt;p&gt;Due to portability issues, dependence on platforms, and restrictions on address taking, developers often need to look for alternatives to traditional bit fields. Modern C++ offers three main architectural alternatives, each designed to address a specific use case.&lt;/p&gt;

&lt;h3&gt;
  
  
  The classic approach: manual bit masks and shifts
&lt;/h3&gt;

&lt;p&gt;If you need to have complete control over the physical position of each bit, the classic C-style approach is your best option. Rather than relying on the compiler to automatically pack bits, you select a fixed-width integer type and manually manipulate individual bits using bitwise operators (&lt;code&gt;&amp;amp;,&lt;/code&gt; &lt;code&gt;|&lt;/code&gt;, &lt;code&gt;~&lt;/code&gt;, &lt;code&gt;^&lt;/code&gt;, &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt;, and &lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Take a look at the 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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DeviceConfig&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;set_mode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="n"&gt;MODE_MASK&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;mode&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;MODE_MASK&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;get_mode&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="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;MODE_MASK&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;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="k"&gt;private&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
  &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;constexpr&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;ENABLE_CLOCK_MASK&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x01&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;constexpr&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;MODE_MASK&lt;/span&gt;         &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x0E&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;constexpr&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;INTERRUPT_MASK&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xF0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
  &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;data&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;Yes, this code is more complex, but its binary layout is now completely independent of the compiler and ABI. This makes it an ideal approach for implementing network protocols and writing device drivers. The only thing developers need to keep in mind is the processor's byte order, or endianness. Multi-byte values appear in reverse order on Big Endian architectures compared to Little Endian ones.&lt;/p&gt;

&lt;h3&gt;
  
  
  For maximum safety: the std::bitset container
&lt;/h3&gt;

&lt;p&gt;The built-in &lt;a href="https://timsong-cpp.github.io/cppwp/n5050/bitset" rel="noopener noreferrer"&gt;&lt;code&gt;std::bitset&lt;/code&gt;&lt;/a&gt; class is the best choice if code safety and convenient manipulation of Boolean flags matter more than saving memory in individual structures.&lt;/p&gt;

&lt;p&gt;This container provides a fixed-size array of bits whose size is specified as a template parameter at compile time. It encapsulates all bitwise operations, providing developers with a high-level, type-safe interface.&lt;/p&gt;

&lt;p&gt;Look at the following &lt;a href="https://godbolt.org/z/vh47czvWT" rel="noopener noreferrer"&gt;example&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="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;bitset&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;Permission&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;Read&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;Write&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;Execute&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;Admin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;MaxPermissions&lt;/span&gt; 
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ManagePermissions&lt;/span&gt;&lt;span class="p"&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;bitset&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;MaxPermissions&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;user_rights&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;user_rights&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Permission&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Read&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    
  &lt;span class="n"&gt;user_rights&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Permission&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   
  &lt;span class="n"&gt;user_rights&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;flip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Permission&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Execute&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;user_rights&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Permission&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&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;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"The user can write files.&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="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;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Current rights mask: "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;user_rights&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&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;"&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="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;ManagePermissions&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 main advantage of &lt;code&gt;std::bitset&lt;/code&gt; is that it protects against human error. If you pass an invalid index to one of its member functions—&lt;a href="https://timsong-cpp.github.io/cppwp/n5050/bitset#lib:set_(member),bitset_" rel="noopener noreferrer"&gt;&lt;code&gt;std::bitset&amp;lt;N&amp;gt;::set&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://timsong-cpp.github.io/cppwp/n5050/bitset#lib:reset,bitset_" rel="noopener noreferrer"&gt;&lt;code&gt;std::bitset&amp;lt;N&amp;gt;::reset&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://timsong-cpp.github.io/cppwp/n5050/bitset#lib:flip,bitset_" rel="noopener noreferrer"&gt;&lt;code&gt;std::bitset&amp;lt;N&amp;gt;::flip&lt;/code&gt;&lt;/a&gt;, or &lt;a href="https://timsong-cpp.github.io/cppwp/n5050/bitset#lib:test,bitset" rel="noopener noreferrer"&gt;&lt;code&gt;std::bitset&amp;lt;N&amp;gt;::test&lt;/code&gt;&lt;/a&gt;—such as index 15 for a four-bit bitset, the function throws a &lt;code&gt;std::out_of_range&lt;/code&gt; exception. With manual bit masks, the same mistake could lead to unpredictable reads from unrelated memory or corrupt adjacent data.&lt;/p&gt;

&lt;p&gt;There's a caveat, though: &lt;code&gt;std::bitset&lt;/code&gt; typically rounds its internal storage up to the processor's native word size, so it can't save memory at the individual-bit level.&lt;/p&gt;

&lt;h3&gt;
  
  
  For large numbers of flags: the std::vector specialization
&lt;/h3&gt;

&lt;p&gt;Using a regular &lt;code&gt;bool&lt;/code&gt; array to manage a huge, dynamically sized array of Boolean flags wastes a lot of memory because each element occupies an entire byte. To fix this issue, the standard library provides a specialized version of &lt;code&gt;std::vector&lt;/code&gt;: &lt;a href="https://timsong-cpp.github.io/cppwp/n5050/vector.bool" rel="noopener noreferrer"&gt;&lt;code&gt;std::vector&amp;lt;bool&amp;gt;&lt;/code&gt;.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This container works differently from ordinary vectors. The standard library optimizes it for memory efficiency by packing &lt;code&gt;bool&lt;/code&gt; elements bit by bit into larger integer types (machine words). Usually, it can store 32 or 64 Boolean values in a single memory block.&lt;/p&gt;

&lt;p&gt;The following is an example of using a large dynamic 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="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&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;bool&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;feature_flags&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;process_features&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;feature_flags&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;]&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;feature_flags&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// code&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;Despite its convenience, &lt;code&gt;std::vector&amp;lt;bool&amp;gt;&lt;/code&gt; is one of the most controversial features of the C++ standard. Developers should exercise caution when using it. Because it packs bits so tightly, its elements aren't actual &lt;code&gt;bool&lt;/code&gt; objects in memory. As a result, you can't take the address of an element directly (the &lt;code&gt;bool *ptr = &amp;amp;feature_flags[0];&lt;/code&gt; code won't compile) or pass an element by reference or pointer. The &lt;code&gt;[]&lt;/code&gt; operator returns a special temporary proxy object that mimics reference behavior rather than a reference. This is the trade-off for compressing data in dynamically allocated memory.&lt;/p&gt;

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

&lt;p&gt;Well, that's all I have for you today. What can I say? Bit fields in C++ perfectly embody the language's philosophy. They give you complete freedom to cut out exactly one bit, then leave you alone to navigate the hidden landmines buried in compilers and system ABIs. Now that you've looked under the hood and seen how this intricate memory optimization coexists with platform-dependent chaos, there's no room for illusions. Bit fields are a powerful tool for optimizing cache lines and working with hardware. However, they can also create hidden issues when you try to send a structure over the network. Now you know the rules of this low-level game. How you wield this power over memory in your own projects is up to you.&lt;/p&gt;

&lt;p&gt;&lt;strong&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=1423" rel="noopener noreferrer"&gt;Get a free one-month trial&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>programming</category>
    </item>
    <item>
      <title>Game++. Part 2.2: Types of optimization</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Fri, 23 Oct 2026 12:18:36 +0000</pubDate>
      <link>https://dev.to/pvsdev/game-part-22-types-of-optimization-4aal</link>
      <guid>https://dev.to/pvsdev/game-part-22-types-of-optimization-4aal</guid>
      <description>&lt;p&gt;This book offers insights into C++, including the algorithms and practices used in game development, and explores the language's strengths and weaknesses, its established workflows, and hands-on solutions, because C++ still dominates the game industry today thanks to its combination of high performance, flexibility, and deep low-level control.&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%2F04qurrit37jls97ecno1.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%2F04qurrit37jls97ecno1.png" alt="1422_pt_7/image1.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Hotspots and bottlenecks
&lt;/h2&gt;

&lt;p&gt;The key task in optimization is to find hotspots, which are code fragments with a high computational load, and bottlenecks, which are components that limit overall throughput, and these problems usually fall into three categories, namely abstractions, smoothness, and hardware limitations.&lt;/p&gt;

&lt;p&gt;Abstractions, or more precisely premature abstractions, add unnecessary layers to the engine architecture and make the whole system more complex than it needs to be, and the usual suspects here are multiple inheritance, one-size-fits-all solutions to game development problems, and heavy use of design patterns in places where simple procedural code would do the job just fine.&lt;/p&gt;

&lt;p&gt;The same category includes writing beautiful code instead of fast code, when performance-critical sections get buried under unnecessary layers of ceremony and checks, which is exactly what the KISS principle warns us about.&lt;/p&gt;

&lt;p&gt;Most developers agree that smoothness matters more than peak performance, and the things that work against it are well known, because sporadic memory allocations on the main thread make the frame rate uneven, algorithms with unpredictable complexity make frame times inconsistent, and blocking resource loads cause stuttering. A stable 30 FPS beats a frame rate that jumps between 60 and 20, because players feel those jumps immediately, which is why a consistent frame rate is such an easy-to-understand and measurable indicator of quality.&lt;/p&gt;

&lt;p&gt;Hardware limitations are best treated as creative constraints, because brute-forcing a platform's physical limits instead of applying smart optimizations only adds complexity without any real payoff. Raising the polygon count without a proper level-of-detail system will leave the player with low FPS, and the same goes for complex shaders that ignore fill-rate limits, memory-hungry data structures that don't account for the cache hierarchy, and so on.&lt;/p&gt;

&lt;p&gt;A modern CPU-GPU architecture is a complex data-processing pipeline with many parallel execution units, and in this "digital hell" data moves through a hierarchy of caches, travels between the CPU and GPU over the bus, and gets synchronized across threads with memory barriers and atomic operations, so the system almost never does exactly what you wrote in the code.&lt;/p&gt;

&lt;p&gt;You can get solid and stable performance by balancing the workload across all of these components, and even then you may not be able to push the CPU beyond 70 to 80%, but you can make much better use of the GPU, memory bandwidth, and caches. The real challenge is finding a delicate balance right at the edge of what's possible for every component at once, and if you get it wrong, resource contention follows, one component becomes the critical link, and the rest of the system suffers from pipeline stalls, context-switching overhead, and reduced throughput.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hotspots
&lt;/h2&gt;

&lt;p&gt;In software systems, hotspots are localized sections of code with unusually high resource consumption, whether that's CPU time, memory, or disk I/O, and they're characterized by a disproportionately high execution frequency, heavy CPU utilization, high memory pressure, or significant system call overhead.&lt;/p&gt;

&lt;p&gt;In the context of code optimization, the Pareto principle, also known as the 80/20 rule, means that 20% of the source code accounts for 80% of a program's total execution time, which makes hunting down hotspots a highly effective strategy. Searching iteratively helps you find the code blocks that consume the most resources so you can target them and cool them down, but once one issue is fixed, the workload shifts and previously minor sections turn into new hotspots that need attention. This process goes on until the results are acceptable or the time allocated for the task runs out, and it's worth remembering that an engineer's time is a finite resource too, because the more time a developer spends polishing one part of the code, the less time they have for everything else.&lt;/p&gt;

&lt;p&gt;Modern profilers provide detailed analysis down to the level of individual CPU instructions, which makes it possible to identify temporal hotspots, code with a high number of cache misses, heavy I/O operations, and resource-lock contention in multithreaded applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bottlenecks
&lt;/h2&gt;

&lt;p&gt;Imagine a dish being prepared in a café. The chef can't make the whole thing at once, so they hand the side dish off to an assistant while working on the main part themselves, and then the two parts come together and a waiter takes the finished plate to the customer. How long will the whole process take? Suppose the chef finishes first, but the assistant simply can't work any faster, and now the assistant is the bottleneck that slows down the entire system.&lt;/p&gt;

&lt;p&gt;Games work exactly the same way, because there's always a "slow assistant" somewhere, whether it's the CPU, the GPU, memory, or something in the code that holds everything else back, and the developer's job is to find that weak link and bring its performance in line with the rest. Optimization almost always involves trade-offs, and in 99% of cases there's no silver bullet that will fix the problem right away.&lt;/p&gt;

&lt;p&gt;The first and most common trade-off is between performance and memory, since sometimes it's more efficient to store extra data than to recalculate it from scratch. This is called &lt;strong&gt;memoization&lt;/strong&gt; or &lt;strong&gt;caching&lt;/strong&gt;, and the idea is that you calculate something once, store the result, and then simply pull it from memory whenever you need it, which costs more memory but gets you the result faster.&lt;/p&gt;

&lt;p&gt;The second trade-off is between accuracy and speed. Sometimes you can cut corners and speed up a system by simplifying a data type or using a less precise algorithm, and shaders are a good example, because they provide both &lt;code&gt;log&lt;/code&gt; and &lt;code&gt;logp&lt;/code&gt;, where the former is more accurate but slower and the latter is faster but less precise. In the final image, players are unlikely to notice that a neighboring polygon has shifted by a quarter of a pixel, but the game may run noticeably faster, and the same principle applies to almost any data model, pathfinding system, or game AI. The key question is how much precision you're willing to give up, or, in more formal terms, how much you can shift resources from lower-priority computations to higher-priority ones.&lt;/p&gt;

&lt;p&gt;The third classic trade-off is simplicity versus speed, and this one has more to do with people and the team than with hardware. A classic example is a young, talented developer who has just discovered design patterns and responds by building five levels of nested caching in an attempt to optimize the texture-loading system.&lt;/p&gt;

&lt;p&gt;Sure, they'll get that system built, and it may even run faster thanks to intrusive pointers, custom allocator pools, and a lock-free hash table, but the result is predictable, because nobody, not even the author a month or two later, can understand, debug, or modify that code without risking breaking something. Other developers on the team start avoiding those files, they groan every time they have to touch the system, and code reviews keep getting comments like "What the hell is this? Can we just rewrite it in plain C++?"&lt;/p&gt;

&lt;p&gt;An experienced developer knows that real performance gains don't come from showing off their knowledge of algorithms, but from understanding the trade-off between how maintainable the code is and how much the optimization actually pays off.&lt;/p&gt;

&lt;p&gt;Yes, you can squeeze out another 5% of performance with custom memory pools, but that will take two weeks to implement and another month to debug all the weird edge cases, so it might be smarter to use a simple linear allocator and trade some memory for time.&lt;/p&gt;

&lt;p&gt;Mature optimization means fixing the truly critical bottlenecks with clean, well-documented solutions that you can easily switch off with conditional compilation if something goes wrong. In the real world, shipping the game matters more than cool algorithms, and having code the team can maintain matters more than having code that looks impressive in a conference talk.&lt;/p&gt;

&lt;h2&gt;
  
  
  Myths and misconceptions
&lt;/h2&gt;

&lt;p&gt;Software optimization is inherently endless, because there will always be something left to improve, but it's important to understand that past a certain point you start hitting diminishing returns, and every additional effort delivers a smaller and less noticeable performance gain.&lt;/p&gt;

&lt;p&gt;On top of that, developers often spend time chasing dead ends that provide no real benefit (but make for great slides and conference talks) and only make the system harder to maintain. The key skill here is knowing when to stop and ask whether further improvements will actually add value or turn into a witch hunt.&lt;/p&gt;

&lt;p&gt;One of the most common mistakes is making decisions based on assumptions. You can't just "guess" what's slowing the system down, because those guesses are usually wrong, and the only reliable approach is to measure and analyze, then analyze and measure again, until guesses and gut feelings turn into well-founded engineering decisions.&lt;/p&gt;

&lt;p&gt;Optimization doesn't start after the code is written, and in fact 80% of performance problems can be eliminated at the architecture design stage, because proper decomposition, a clear object hierarchy, and well-thought-out dependency management help you avoid bottlenecks in the first place. Despite the overhead that comes with languages like C# and Java, a well-structured system often outperforms code written in pure C++ that ignores architectural constraints.&lt;/p&gt;

&lt;p&gt;When it comes to optimization, it's important to focus on worst-case scenarios rather than average ones. Most developers test their games and applications on powerful workstations, which often gives them a distorted picture, and on weaker hardware the bottlenecks can be completely different, with memory bandwidth or CPU power becoming the limiting factor instead of the GPU.&lt;/p&gt;

&lt;p&gt;To analyze performance properly, developers need to deliberately test on configurations with minimal or weak hardware specs, because those machines are much closer to what a good portion of your players actually experience. Players can tell the difference between 55 and 60 FPS, but they rarely complain about it, while a drop to 20 FPS feels like serious lag even if it only happens once in a while, and that's why consistent frame times often matter more than a high average frame rate.&lt;/p&gt;

&lt;p&gt;Debug and release builds give fundamentally different results (Pic. 3.1). In debug mode, compiler optimizations are turned off and extra checks are turned on, which can slow the code down several times over, and not evenly. For valid performance profiling, work only with release builds and enable debug symbols or disable optimization for specific sections when you need to, because this lets you strike a balance between traceability and accurate measurements while still giving you a realistic picture of performance.&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%2Fovqor2327a7y6h3tnf8y.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%2Fovqor2327a7y6h3tnf8y.png" alt="1422_pt_7/image2.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Picture 3. 1&lt;/p&gt;

&lt;p&gt;That said, debug builds are useful in the early stages of testing, when they can reveal excessive CPU usage or inefficient algorithms before those problems become critical.&lt;/p&gt;

&lt;p&gt;Finally, the key requirement for any benchmark is control over external variables, since poorly designed tests can create the illusion of optimization. For example, the first measurement might be taken in fullscreen mode while the second runs in windowed mode, at a different refresh rate, or at a different resolution, and the frame rate goes up because the environment changed rather than the code, which makes the whole comparison meaningless. To avoid mistakes like this, keep every environment parameter fixed and change only one thing between runs, namely the piece of code you're optimizing. The best practice is to automate your benchmarks, which removes human error and gives you reproducible results.&lt;/p&gt;

&lt;p&gt;Many people think optimization time should be added to sprints once the game is nearly finished, or, even worse, only when performance problems show up. But ignoring optimization in the early stages of development means risking increasingly tangled processes and unpredictable performance problems at the worst possible moment.&lt;/p&gt;

&lt;p&gt;Another myth is that reactive optimization works well enough on its own. Yes, hundreds of games ship after being optimized at the last minute, but this approach usually ends with cutting features or simplifying mechanics to make up for low FPS. Reactive optimization creates a false sense of control, because fixing problems late in development takes far more time and effort than taking a systematic approach throughout the whole project.&lt;/p&gt;

&lt;p&gt;The next myth is that optimization needs large, dedicated blocks of time, when in reality it's much more effective to work it in gradually. With practices like performance budgets, you can set limits for FPS, CPU, and GPU usage up front and organize the team's work around those numbers, and continuous monitoring with incremental code adjustments produces better results than occasional optimization pushes in the final weeks of development.&lt;/p&gt;

&lt;p&gt;There's also a persistent belief that every optimization is justified. In reality, there are plenty of areas where programmers can sink time without seeing any obvious benefit, so only optimize what actually affects performance and weigh the potential payoff against the time the changes will take. This approach keeps you from wasting effort and keeps the focus on the bottlenecks that are really slowing the game down.&lt;/p&gt;

&lt;p&gt;Finally, there's a misconception that performance is a single metric, when a game that "feels slow" can mean any combination of low FPS, longer response times, high CPU and GPU load, and frame rate drops. To optimize even one of these metrics effectively, developers first need to understand which ones are critical for their particular project, how they relate to each other, and what's actually holding the game back.&lt;/p&gt;

&lt;p&gt;A systematic approach like this is the only way to avoid the common misconceptions about optimization. Will anyone actually approve such changes, though? Most likely not, because it's extremely unlikely that the project architect, tech lead, or CTO will sign off on real architectural changes, and that's not really the programmer's call to make anyway, so pushing for it tends to step on the architect's toes. Besides, changing an existing, working, well-tested system can break the systems around it, and there's no way to predict when the error will surface, only that it inevitably will. That's exactly why a developer who just joined the team can't rewrite half the engine "the right way," and what's left for them is code-level and algorithmic tweaks, which can still squeeze out a performance improvement of at least 10%.&lt;/p&gt;

&lt;p&gt;This brings us to the reality that game development forces us to balance our "ideal" technical solutions against the practical constraints of the project. Even a programmer who sees through every optimization myth often has to work within an existing architecture they can't change and settle for local improvements at the algorithm and code level, but this approach can still deliver tangible results, such as the 10 to 15% performance gains that can make all the difference for a successful release.&lt;/p&gt;

&lt;p&gt;Knowing where your influence ends matters, and so does choosing your battles wisely. Optimization was never just a technical skill, because it's also a team sport where every change needs to be justified, measured, and agreed upon, and in the end the best optimization is the one players actually notice, not the one that gets you a few nods in a technical chat.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tools
&lt;/h2&gt;

&lt;p&gt;Any work aimed at improving performance is wasted if you can't see what's actually happening in your code. It's like trying to repair a watch blindfolded or tune a musical instrument with your ears plugged, because you can turn the screws and pluck the strings all you want, but the result will be purely random.&lt;/p&gt;

&lt;p&gt;In everyday life, we get immediate feedback, so if you trip, you fall, if you take a wrong turn, you may end up at a dead end, and if you add too much salt, you ruin the dish. These signals help us correct course right away and remember not to make the same mistake twice. In programming, however, things work differently, because code can execute millions of times per second, process gigabytes of data, and interact with dozens of system components, while all we see is the end result, say, an FPS counter that refuses to go above 50.&lt;/p&gt;

&lt;p&gt;Modern software, and games in particular, can run to hundreds of thousands of lines of code stacked on top of complex operating systems, graphics drivers, and various libraries that add even more code. Without specialized analysis tools, all you can really do is guess whether the CPU can't keep up, whether the bottleneck is loading textures from disk, or whether memory is leaking somewhere. Profilers, performance monitors, and debuggers can mislead you too, but you can't do without them, because they show which functions take the longest to execute, how much memory each part of the program uses, how often the application hits the disk, and where the CPU sits idle waiting for data. Without this information you're flying blind, and with it the solution often becomes obvious, although measuring and analyzing performance correctly is its own kind of science that sometimes borders on alchemy.&lt;/p&gt;

&lt;p&gt;Any tool that measures a program's performance will inevitably affect that performance. Some tools are light enough on their feet that they barely slow the program down, and those are called &lt;strong&gt;non-intrusive&lt;/strong&gt;, while others can seriously drag down an application's performance, and those are called &lt;strong&gt;intrusive&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;How much interference you get depends on how much data you need and how often you need it. A simple FPS counter has virtually no impact on the game, while a detailed analyzer that logs every function call and its execution time can slow things down considerably. Some tools, such as Valgrind, go even further and run the entire program inside a virtual machine to collect highly detailed information about every operation, and you can guess what that does to performance, since the program can easily run ten times slower than usual or worse.&lt;/p&gt;

&lt;p&gt;There are several ways to measure performance, and sampling profilers are the gentlest and simplest of them. They periodically take a snapshot of the program to see which code is running at that moment, and by collecting a series of these snapshots, developers can build a statistical picture of what the program spends most of its time doing.&lt;/p&gt;

&lt;p&gt;A more intrusive approach relies on interception tools that sit between the program and external resources, so PIX, for example, intercepts every call the program makes to the GPU through the DirectX API, while network analyzers peek at all network traffic.&lt;/p&gt;

&lt;p&gt;Getting even more detail means adding special commands and profiling markers to the code to collect data, and this process is called instrumentation.&lt;/p&gt;

&lt;p&gt;Choosing the right tool depends on your specific goals and on how much performance you're willing to sacrifice for a more detailed analysis. The one rule that really matters is that the tool shouldn't distort the measurements more than the problem you're hunting for, since there's little point in optimizing a function that accounts for only 1% of execution time if the profiler itself slows the program down by 50%. That's why all three types of tools are often used, each in its own circumstances.&lt;/p&gt;

&lt;h2&gt;
  
  
  Theory and terminology
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Profilers&lt;/strong&gt; is an umbrella term for the performance analysis tools used across most of game and software development, and a profiler's main job is to capture how the workload breaks down during execution in as much detail as the developer allows.&lt;/p&gt;

&lt;p&gt;The classic use case is measuring CPU time, but modern profilers can track much more, including cache misses, memory allocations, disk access, thread usage, and synchronization. Depending on the level of instrumentation, they can give you either an aggregated view of the workload that shows which modules or systems put the most load on the CPU and where, or a per-frame or per-interval snapshot with the call stack and the exact distribution of work across individual instructions.&lt;/p&gt;

&lt;p&gt;Profilers fall into two categories, general-purpose and specialized. General-purpose profilers are usually the ones built into an IDE or a platform SDK, such as the Visual Studio Profiler, and they give you a broad sense of where performance is leaking. Specialized profilers are low-level tools, such as Sony Razor, Intel VTune, PIX, or Tracy, that collect data from CPU performance counters and OS events and let you analyze code behavior with much greater precision, and only these low-level tools let developers evaluate how specific algorithmic steps or memory access patterns affect the overall performance of a piece of code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Total time&lt;/strong&gt; shows the cumulative execution time of a function, including all the nested calls it makes to other functions. In theory, it helps identify hotspots in an application and gives a complete picture of how much time the program spends in a particular section of code along with all its dependencies, but in practice it can give a distorted view of performance.&lt;/p&gt;

&lt;p&gt;As part of its optimizations, the compiler may inline functions directly into the code of the calling function, which blurs the execution time of the parent function even though the inlined code is what actually does the work. This metric also doesn't account for indirect costs, such as cache pollution or data eviction, which can seriously undermine how reliable the measurement is.&lt;/p&gt;

&lt;p&gt;For example, when you analyze functions whose performance depends heavily on their input data, such as sorting an almost-sorted array versus an unsorted one, the profiler will report an average value, which can hide real performance problems that only show up in certain use cases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Self time&lt;/strong&gt; measures only the time spent executing instructions inside the function itself, excluding the time spent in all the functions it calls. This helps identify the code blocks that actually do the expensive work rather than just handing it off to other functions, and functions with heavy algorithms, loops, or number crunching typically show high self time.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;ObjectUpdate()&lt;/code&gt; function may have a high total time because it calls all the other subsystems responsible for updating the game state, but a low self time because it doesn't do much work on its own, while a collision-physics function may show significant self time, which tells you that this particular section needs optimization. The problems here are much the same as with the previous metric, because self time can't reflect a function's actual impact on the overall performance of the system. A function may have a low self time while triggering many expensive operations through other functions, which creates a false impression of efficiency. On top of that, this metric ignores function call overhead, so if a function makes thousands of small calls, the cost of creating stack frames, passing parameters, and returning values can be significant, or even close to the actual execution time, and still stay invisible in the self-time numbers.&lt;/p&gt;

&lt;p&gt;Another issue is "aggressive optimization," which is especially common with Clang-based compilers that inline small functions and partially embed their code at the call site. As a result, self-time measurements get distorted, because code that belongs to a different function is counted as self time for the caller (Pic. 3.2).&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%2Fy1pbly8wx5as494pc6lr.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%2Fy1pbly8wx5as494pc6lr.png" alt="1422_pt_7/image3.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Picture 3. 2&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Calls&lt;/strong&gt; metric shows how often a particular piece of code runs during a profiling session, which helps reveal how different functions are actually used in the application. Even if their individual execution times are short, functions with a high call count are good candidates for optimization, because the cumulative overhead of all those calls can be significant.&lt;/p&gt;

&lt;p&gt;Analyzing call counts is one of the first steps in finding inefficient algorithms or unnecessary computations (with one caveat, which is covered in the note below). A function that gets called thousands of times per frame may need a whole range of optimization techniques, and a high call count can also point to recursion, resource leaks, or other architectural problems in the application (Pic. 3.3).&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%2Fbenh4kchc00j2rzy4g5u.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%2Fbenh4kchc00j2rzy4g5u.png" alt="1422_pt_7/image4.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Picture 3.3&lt;/p&gt;

&lt;p&gt;Overall, this is a fairly useful metric, but the call count alone doesn't show how those calls are distributed over time. A function may be called 100,000 times in a session, but if all those calls happen while a level is loading, they have no impact on gameplay performance, and in that case the profiler will show a high call count without telling you that the function sits idle while frames are being rendered. The call count can also be misleading when it comes to spotting real architectural problems, because a high number of calls may simply be the result of a well-designed architecture with lots of small, specialized functions rather than a sign of inefficiency.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Modern compilers have gotten very good at inlining, so focusing only on call counts can lead to premature or incorrect conclusions\.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Total waste time&lt;/strong&gt; tracks how long a function spends with its thread blocked while waiting for resources to become available, I/O operations to complete, or system events to occur (Fig. 3.4). This time doesn't reflect active execution of the function, but rather delays caused by external factors, and a high waste time can point to indirect performance problems that affect the function's overall execution time. This metric is usually analyzed when refactoring code or reducing dependencies between functions.&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%2Fq6bvymm65m6c9wvxgkpm.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%2Fq6bvymm65m6c9wvxgkpm.png" alt="1422_pt_7/image5.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Picture 3.4&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;ul&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/1361/" rel="noopener noreferrer"&gt;Game++. Part 1.1: C++, game engines, and architectures&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/1375/" rel="noopener noreferrer"&gt;Game++. Part 1.2: C++, game engines, and architectures&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/1384/" rel="noopener noreferrer"&gt;Game++. Part 1.3: C++, game engines, and architectures&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/1390/" rel="noopener noreferrer"&gt;Game++. Part 1.4: C++, game engines, and architectures&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/1397/" rel="noopener noreferrer"&gt;Game++. Part 2.1: Types of optimization&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>cpp</category>
      <category>gamedev</category>
      <category>programming</category>
    </item>
    <item>
      <title>What's new in C# 15: Overview</title>
      <dc:creator>Unicorn Developer</dc:creator>
      <pubDate>Tue, 20 Oct 2026 14:12:02 +0000</pubDate>
      <link>https://dev.to/pvsdev/whats-new-in-c-15-overview-3md3</link>
      <guid>https://dev.to/pvsdev/whats-new-in-c-15-overview-3md3</guid>
      <description>&lt;p&gt;A new version of C# is coming soon, so we're continuing our annual tradition of reviewing its new features. The Microsoft team has prepared several highly anticipated and exciting changes for this release. Let's take a look at 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%2F7h6yhwy2pluk2yy3spls.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%2F7h6yhwy2pluk2yy3spls.png" alt=" " width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Union types
&lt;/h2&gt;

&lt;p&gt;We'll start with the most straightforward change, &lt;code&gt;union&lt;/code&gt; types. They enable developers to combine types without relying on inheritance or interfaces where it's unnecessary or unavailable. A &lt;code&gt;union&lt;/code&gt; is a structure that contains the complete set of supported types.&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="k"&gt;union&lt;/span&gt; &lt;span class="nf"&gt;TestUnion&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="n"&gt;Version&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;IEnumerable&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="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, &lt;code&gt;union&lt;/code&gt; is the keyword, while &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;Version&lt;/code&gt;, and &lt;code&gt;IEnumerable&amp;lt;string&amp;gt;&lt;/code&gt; are the set of supported types.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;union&lt;/code&gt; types come in handy when traditional inheritance isn't an option, as it requires the types to have something in common. However, there may be cases where inheritance is impossible or makes little architectural sense. We might need to handle a specific set of types, including primitives. Normally, we'd have to specify a variable of the &lt;code&gt;object&lt;/code&gt; type as input and then cast it to the required types within the method, including logic for handling anything unsupported. That's error-prone. Using &lt;code&gt;union&lt;/code&gt; in this situation would solve these problems, making the code safer and easier to read.&lt;/p&gt;

&lt;p&gt;Given the nature of the new structure, when we use &lt;code&gt;union&lt;/code&gt;, the compiler already knows the complete list of types it can contain. So, the &lt;code&gt;default&lt;/code&gt; branch is no longer necessary. The change protects us from a &lt;code&gt;switch&lt;/code&gt; that misses some types while new subclasses keep falling into the &lt;code&gt;default&lt;/code&gt; section, which can cause unpredictable program behavior.&lt;/p&gt;

&lt;p&gt;Let's look at a more specific example. Before this update, we'd have had to write something like this to define a method for handling API responses:&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;abstract&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="n"&gt;ApiResult&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;record&lt;/span&gt; &lt;span class="nf"&gt;ErrorData&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;Message&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;record&lt;/span&gt; &lt;span class="n"&gt;Success&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;Data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ApiResult&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;record&lt;/span&gt; &lt;span class="n"&gt;ValidationError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ErrorData&lt;/span&gt; &lt;span class="n"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ApiResult&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;string&lt;/span&gt; &lt;span class="nf"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ApiResult&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&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;Success&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;ValidationError&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Data&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;_&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Unknown result"&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;ApiResult&lt;/code&gt; abstract class, from which specific responses inherit, is declared. So, if a new subclass appears and the developer fails to handle it, executions falls into the &lt;code&gt;default&lt;/code&gt; branch. With a &lt;code&gt;union&lt;/code&gt;, it works differently:&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;record&lt;/span&gt; &lt;span class="nf"&gt;ErrorData&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;Message&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;record&lt;/span&gt; &lt;span class="nf"&gt;Success&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;Data&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;record&lt;/span&gt; &lt;span class="nf"&gt;ValidationError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ErrorData&lt;/span&gt; &lt;span class="n"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;union&lt;/span&gt; &lt;span class="nf"&gt;ApiResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Success&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ValidationError&lt;/span&gt;&lt;span class="p"&gt;)&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;string&lt;/span&gt; &lt;span class="n"&gt;GetData&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="k"&gt;this&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;Success&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;ValidationError&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Data&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="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;As you can see, there's no need for a parent class just to group the types together. The behavior is also more predictable because the compiler knows at compile time exactly which types &lt;code&gt;union&lt;/code&gt; can contain. So, if a &lt;code&gt;switch&lt;/code&gt; doesn't handle every type in the union, the compiler issues a warning.&lt;/p&gt;

&lt;p&gt;Moreover, the &lt;code&gt;union&lt;/code&gt; gives developers an easier way to express patterns such as monads. It also enables implementing custom methods and properties within &lt;code&gt;union&lt;/code&gt;, so working with all its types doesn't require writing &lt;code&gt;switch&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Closed hierarchies
&lt;/h2&gt;

&lt;p&gt;This change is somewhat related to &lt;code&gt;union&lt;/code&gt; in that it also lets the compiler know exactly how many types it'll work with. It does so by preventing inheritance from a class outside the assembly where it was declared. This behavior resembles a mix of &lt;code&gt;internal&lt;/code&gt; and &lt;code&gt;sealed&lt;/code&gt;: it takes the scope from &lt;code&gt;internal&lt;/code&gt; and the restriction on inheritance from &lt;code&gt;sealed&lt;/code&gt;. This can be useful, for example, in libraries where we want to restrict inheritance from certain classes without blocking it within our own assembly. Here's an example declaration:&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;closed&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="n"&gt;ClosedRecordClass&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;closed&lt;/code&gt; keyword marks the class as closed to inheritance. As a result, attempting to inherit from such a class in another assembly causes a compilation error. Note that, like &lt;code&gt;union&lt;/code&gt;, a closed hierarchy supports an exhaustive &lt;code&gt;switch&lt;/code&gt;, which enables omitting the &lt;code&gt;default&lt;/code&gt; branch. &lt;/p&gt;

&lt;p&gt;A few important details are worth keeping in mind. Firstly, a class marked as &lt;code&gt;closed&lt;/code&gt; is always abstract. This also helps enable an exhaustive &lt;code&gt;switch&lt;/code&gt;. Secondly, descendants don't automatically become closed. If we need to hide them from other assemblies, we can mark them with the &lt;code&gt;internal&lt;/code&gt; access modifier. Thirdly, only classes (&lt;code&gt;class&lt;/code&gt; and &lt;code&gt;record class&lt;/code&gt;) can be closed to inheritance from another assembly. Structs don't support inheritance, while interfaces are specifically designed to be inherited from. So, adding the keyword to a struct or an interface will result in a compilation error. &lt;/p&gt;

&lt;h2&gt;
  
  
  Labeled break and continue
&lt;/h2&gt;

&lt;p&gt;A small but quite important addition: most of us have probably run into situations where we needed to interrupt the execution of an external structure or skip to its next iteration. The classic approach would have looked 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;public&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;WorkItemType&lt;/span&gt;
&lt;span class="p"&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;StopAll&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;WorkItem&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="n"&gt;WorkItemType&lt;/span&gt; &lt;span class="n"&gt;Type&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;set&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;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OldLabeledBreak&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;PerformAction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;WorkItem&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;workList&lt;/span&gt;&lt;span class="p"&gt;)&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;stopProcessing&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;foreach&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;item&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="n"&gt;workList&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// #1&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;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// #2&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;WorkItemType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
          &lt;span class="c1"&gt;// ....&lt;/span&gt;
          &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// break #2&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;WorkItemType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StopAll&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
          &lt;span class="n"&gt;stopProcessing&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="c1"&gt;// break #2&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;stopProcessing&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="c1"&gt;// break #1&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In other words, we need an additional flag so that the outer loop can exit after the inner &lt;code&gt;switch&lt;/code&gt; finishes executing. Of course, we could use &lt;code&gt;goto&lt;/code&gt;, but it can make the code harder to follow because the label code execution jumps to can appear anywhere. Labeled &lt;code&gt;break&lt;/code&gt; and &lt;code&gt;continue&lt;/code&gt; simplify this. Let's see what this method would look like now.&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="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;PerformAction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;WorkItem&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;workList&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;outer:&lt;/span&gt;  &lt;span class="n"&gt;foreach&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;item&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="n"&gt;workList&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// #1&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;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// #2&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;WorkItemType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;// ....&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// break #2&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;WorkItemType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StopAll&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt; &lt;span class="n"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// break #1&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;As you can see, the syntax is similar to &lt;code&gt;goto&lt;/code&gt;, while the code becomes more readable. This lets us avoid creating extra flags and checks, which can slightly reduce memory usage and also make the code more expressive. Labels can be applied with the following constructs: &lt;code&gt;for&lt;/code&gt;, &lt;code&gt;foreach&lt;/code&gt;, &lt;code&gt;while&lt;/code&gt;, &lt;code&gt;do&lt;/code&gt;, and &lt;code&gt;switch&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Extension indexers
&lt;/h2&gt;

&lt;p&gt;This change enables developers to define how collection elements can be accessed by index using extension indexers. For example, it's possible to implement element access with &lt;code&gt;Range&lt;/code&gt; for &lt;code&gt;IReadOnlyList&lt;/code&gt;. Before C# 15, we could implement this as follows:&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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OldExtensionIndexers&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;IReadOnlyList&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;readOnlyList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"val1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"val2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"val3"&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;elem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;readOnlyList&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="mf"&gt;.2&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;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OldReadOnlyListExtensions&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;IReadOnlyList&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;Slice&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="k"&gt;this&lt;/span&gt; &lt;span class="n"&gt;IReadOnlyList&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;list&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                                          &lt;span class="n"&gt;Range&lt;/span&gt; &lt;span class="n"&gt;range&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;range&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetOffsetAndLength&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&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;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;length&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;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;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;length&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="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;offset&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="k"&gt;return&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While a regular extension method already solves this problem, C# 15 introduces the option to create extension indexers. So, we'll get access to elements using the familiar brackets. Let's look at how this would work in our 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="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NewExtensionIndexers&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;IReadOnlyList&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;readOnlyList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"val1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"val2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"val3"&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;elem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;readOnlyList&lt;/span&gt;&lt;span class="p"&gt;[.&lt;/span&gt;&lt;span class="mf"&gt;.2&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;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NewReadOnlyListExtensions&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;extension&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;IReadOnlyList&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;list&lt;/span&gt;&lt;span class="p"&gt;)&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;IReadOnlyList&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Range&lt;/span&gt; &lt;span class="n"&gt;range&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;get&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;range&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetOffsetAndLength&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Count&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;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Skip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;Take&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;ToList&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;It's worth noting that if a type has its own indexer, it takes precedence over an indexer declared as an extension. The official order of precedence is as follows: &lt;code&gt;explicit&lt;/code&gt; type indexers, &lt;code&gt;implicit&lt;/code&gt; type indexers, &lt;code&gt;explicit&lt;/code&gt; extension indexers, and &lt;code&gt;implicit&lt;/code&gt; extension indexers. &lt;/p&gt;

&lt;h2&gt;
  
  
  Collection expression arguments
&lt;/h2&gt;

&lt;p&gt;The next feature will help shorten code and make collection initialization more convenient by passing the required arguments directly to the constructor. For example, we can pass &lt;code&gt;capacity&lt;/code&gt; and &lt;code&gt;comparer&lt;/code&gt;. In C# 14 and earlier, we would need 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;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ProcessList&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;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;loginList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"login1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"login2"&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;newLoginList&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;string&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;loginList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;newLoginList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddRange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loginList&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ProcessHashSet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;HashSet&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;hashSet&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;HashSet&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;StringComparer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OrdinalIgnoreCase&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;hashSet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"value 1"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;hashSet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"value 2"&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 initializer supports only one option because &lt;code&gt;List&lt;/code&gt; has just three constructors: an empty one, one that takes &lt;code&gt;capacity&lt;/code&gt;, and one that takes a collection. Let's look at the new possible implementation:&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="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ProcessList&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;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;loginList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"login1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"login2"&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;newLoginList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;loginList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;..&lt;/span&gt;&lt;span class="n"&gt;loginList&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="c1"&gt;// newLoginList.AddRange(loginList);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ProcessHashSet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;HashSet&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;hashSet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;StringComparer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OrdinalIgnoreCase&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; 
    &lt;span class="s"&gt;"value 1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"value 2"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="c1"&gt;// hashSet.Add("value 1");&lt;/span&gt;
  &lt;span class="c1"&gt;// hashSet.Add("value 2");&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 looks cleaner, right? Your IDE will probably even suggest this syntax and collapse everything into a single line. However, note that &lt;code&gt;with&lt;/code&gt; must always come first; otherwise, the compiler will issue an error.&lt;/p&gt;

&lt;h2&gt;
  
  
  Memory safety
&lt;/h2&gt;

&lt;p&gt;The final change involves reevaluating what C# considers unsafe. The language team aims to flag only code that actually accesses unmanaged memory as unsafe. In other words, using pointers will no longer automatically make code unsafe. Before C# 15, unsafe code was marked 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;public&lt;/span&gt; &lt;span class="n"&gt;unsafe&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OldMemorySafety&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;ReadMemory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;ptr&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="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;byte&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;newPtr&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;b&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;ReadMemory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;newPtr&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;Any use of pointers used to require the &lt;code&gt;unsafe&lt;/code&gt; keyword. To better understand the change, let's take a look at what the code will look like in the new 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="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NewMemorySafety&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;ReadMemory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;unsafe&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ptr&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;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;byte&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;newPtr&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;b&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;ReadMemory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;newPtr&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;We no longer need to mark the class as &lt;code&gt;unsafe&lt;/code&gt;. Instead, the keyword goes only where it's needed. In this case, dereferencing the pointer is unsafe, so it should be marked as such.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;safe&lt;/code&gt; keyword has also been added. It should be used when a block contains unsafe operations that are safe for the caller's context, so callers don't require the &lt;code&gt;unsafe&lt;/code&gt; block. In other words, use &lt;code&gt;safe&lt;/code&gt; when the compiler can't verify safety on its own. Examples include &lt;code&gt;extern&lt;/code&gt; methods and fields in structs with explicit memory layout (&lt;code&gt;[StructLayout(LayoutKind.Explicit)]&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;It's also worth mentioning &lt;code&gt;stackalloc&lt;/code&gt;. Its use is now considered unsafe when all of the following apply: it's converted to &lt;code&gt;Span&amp;lt;T&amp;gt;&lt;/code&gt; or &lt;code&gt;ReadOnlySpan&amp;lt;T&amp;gt;&lt;/code&gt;, it has no initializer, and it's used where &lt;code&gt;SkipLocalsInitAttribute&lt;/code&gt; is present.&lt;/p&gt;

&lt;p&gt;The new rules introduced by this change should be enabled separately. If you'd like to learn more, follow &lt;a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/language#enable-the-updated-memory-safety-rules" rel="noopener noreferrer"&gt;this link&lt;/a&gt; for the official instructions.&lt;/p&gt;

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

&lt;p&gt;This year's language release has brought developers a number of fairly important features. Among them are useful additions such as &lt;code&gt;union&lt;/code&gt; types and closed hierarchies, while several other changes simplify common coding patterns, making development little smoother once again. Some of these features may not become as popular, but that doesn't make them any less useful to certain groups of developers. Which of these changes did you find most useful? Share your thoughts in the comments!&lt;/p&gt;

&lt;p&gt;You can read the C# 15 documentation via &lt;a href="https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-15" rel="noopener noreferrer"&gt;this link&lt;/a&gt;. Don't forget to check out our overviews of previous versions, here's the list:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/csharp/0860/" rel="noopener noreferrer"&gt;What's new in C# 9: overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/csharp/0875/" rel="noopener noreferrer"&gt;What's new in C# 10: overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/csharp/1002/" rel="noopener noreferrer"&gt;What's new in C# 11: overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/csharp/1074/" rel="noopener noreferrer"&gt;What's new in C# 12: overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/csharp/1173/" rel="noopener noreferrer"&gt;What's new in C# 13: overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pvs-studio.com/en/blog/posts/csharp/1301/" rel="noopener noreferrer"&gt;What's new in C# 14: overview&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>news</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
