{"path": "/posts/when-you-see-null-ask-yourself-if/", "title": "When you see `null`, ask yourself if that is your fault · Bruno Dias", "html": "<!doctype html>\n<html lang=\"en\">\n<head>\n  <meta charset=\"utf-8\">\n  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n  <meta name=\"description\" content=\"A null value in 2025 is a developer&#x27;s decision. It has its uses, but mostly a terrible decision.\">\n  <title>When you see `null`, ask yourself if that is your fault · Bruno Dias</title>\n  <link rel=\"canonical\" href=\"https://diasbruno.github.io/posts/when-you-see-null-ask-yourself-if/\" data-post-metadata>\n  <meta name=\"author\" content=\"Bruno Dias\" data-post-metadata>\n  <meta property=\"og:type\" content=\"article\" data-post-metadata>\n  <meta property=\"og:title\" content=\"When you see `null`, ask yourself if that is your fault\" data-post-metadata>\n  <meta property=\"og:url\" content=\"https://diasbruno.github.io/posts/when-you-see-null-ask-yourself-if/\" data-post-metadata>\n  <meta property=\"article:published_time\" content=\"2025-11-08T02:04:25+00:00\" data-post-metadata>\n  <meta property=\"og:image\" content=\"https://diasbruno.github.io/assets/site/substack/when-you-see-null-ask-yourself-if/figure-1.jpg\" data-post-metadata>\n  <link rel=\"icon\" href=\"/assets/site/signature.svg\" type=\"image/svg+xml\">\n  <link rel=\"alternate\" type=\"application/atom+xml\" title=\"diasbruno\" href=\"/atom.xml\">\n  <link rel=\"alternate\" type=\"application/json\" href=\"/posts/when-you-see-null-ask-yourself-if/content.json\">\n  <link rel=\"stylesheet\" href=\"/assets/site/tokens.css\">\n  <link rel=\"stylesheet\" href=\"/assets/site/site.css\">\n  <link rel=\"stylesheet\" href=\"/assets/site/footer.css\">\n\n</head>\n<body class=\"light\">\n<div id=\"site-page\">\n<a class=\"skip-link\" href=\"#content\">Skip to content</a>\n<header class=\"site-header\">\n  <a class=\"brand\" href=\"/\" data-site-navigation aria-label=\"Bruno Dias, home\">\n    <img src=\"/assets/site/signature.svg\" width=\"24\" height=\"34\" alt=\"\">\n  </a>\n  <nav aria-label=\"Main navigation\">\n    <ul>\n<li><a class=\"nav-vrn\" href=\"/vrn/\" data-site-navigation><span class=\"nav-label\">vrn</span> <span class=\"nav-badge\">research</span></a></li>\n<li><a class=\"nav-posts\" href=\"/posts/\" data-site-navigation aria-current=\"location\">Posts</a></li>\n<li><a class=\"nav-projects\" href=\"/projects/\" data-site-navigation>Projects</a></li>\n<li><a class=\"nav-about\" href=\"/about/\" data-site-navigation>About</a></li>\n</ul>\n  </nav>\n</header>\n\n<main class=\"section\" id=\"content\" tabindex=\"-1\">\n  <article class=\"post-detail\">\n    <header class=\"section-header\">\n      <p class=\"post-category\">Post</p>\n      <h1>When you see `null`, ask yourself if that is your fault</h1>\n<p class=\"post-subtitle\">A null value in 2025 is a developer&#x27;s decision. It has its uses, but mostly a terrible decision.</p>\n      <p><time datetime=\"2025-11-08T02:04:25+00:00\">2025-11-08</time></p>\n    </header>\n    <div class=\"post-columns\">\n      <section class=\"post-body\" aria-label=\"Article\">\n\n<figure><img src=\"/assets/site/substack/when-you-see-null-ask-yourself-if/figure-1.jpg\" alt=\"\" loading=\"lazy\" width=\"1456\" height=\"848\">\n</figure>\n<p>Languages are catching up to 2025 standards. From the decision to allow uninitialized class properties and variables, to today’s understanding that null is a deliberate developer’s decision (and a terrible one).</p>\n<p>Even if you say “the programming language I use allows it to happen”, at least, most of the languages are implementing a kind of optional syntax to indicate this value.</p>\n<p>“?” (question mark) is the most used type notation to defined that an object can be a disjoint union of null and the values of the actual type.</p>\n<p>Other options have existed for long time know - but they only existed on smart languages and used only by smart people, and today are also becoming popular in many languages like Maybe type (or Optional depending on the language or package you use).</p>\n<p>Here is a list of thing to look out before starting writing code…</p>\n<h3>Is null a valid value?</h3>\n<p>If null is valid, then everything is ok, otherwise I have to inform you that this is your fault.</p>\n<p>We all know we cannot add <code>null + 4</code>. So we need to look how it happened.</p>\n<ol><li><p>Is this a callee problem? </p>\n<p></p>\n<p>Who calls the function is responsible for having valid data before calling a function, otherwise your entire code base is going to be performing unnecessary checks (defensive programming).</p>\n<p></p>\n</li>\n<li><p>Are the defaults of the types right?</p>\n<p></p>\n<p>If you need to sum with the value and you have a null value in hands, you must know that if you convert null to zero, then we fix all the problems (<code>for sum 0 + 4 = 4, or for multiplication 1 * 4 = 4</code>).</p>\n</li>\n</ol>\n<h3>Can you use Option or Maybe type?</h3>\n<p>If you can use them, they are great options if you can build in a more pipeline style.</p>\n<pre><code>(true\n  ? std::optional&lt;int&gt;(5)\n  : std::nullopt)\n  .and_then([](int value){ \n     return value + 5 \n  })\n  .value_or(5);</code></pre>\n<p>If <code>std::nullopt</code> is passed through the pipeline, then <code>value_or</code> will execute and <code>5</code> will be returned. If <code>std::optional</code> is passed, then the pipeline is executed and the result will be <code>10</code>.</p>\n<p>Depending on the language it can optimize this construction, but in most of the cases, any performance can be neglected. Measure always!</p>\n<h3>So…</h3>\n<p>This is enough for you to remember about default values and see a few options to not see anymore nulls on your code base.</p>\n\n      </section>\n      <aside class=\"post-links\" aria-labelledby=\"post-links-heading\">\n        <h2 id=\"post-links-heading\">Links</h2>\n        <ul>\n<li><a href=\"/posts/\" data-site-navigation>All posts</a></li>\n<li><a href=\"https://diasbruno.substack.com/p/when-you-see-null-ask-yourself-if\">Original on Substack</a></li>\n        </ul>\n      </aside>\n    </div>\n  </article>\n</main>\n\n<footer class=\"site-footer\">\n  <div class=\"site-footer-inner\">\n    <a class=\"site-footer-home\" href=\"/\" data-site-navigation>Bruno Dias</a>\n    <ul aria-label=\"Footer links\">\n      <li><a href=\"https://github.com/diasbruno\">GitHub</a></li>\n      <li><a href=\"/atom.xml\">Atom feed</a></li>\n    </ul>\n  </div>\n</footer>\n\n</div>\n<script type=\"module\" src=\"/assets/site/navigation.js\"></script>\n</body>\n</html>\n"}
