<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Java on Marouane LAARIF scribbles</title>
    <link>https://blog.laarif-marouane.com/tags/java/</link>
    <description>Recent content in Java on Marouane LAARIF scribbles</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <managingEditor>marouane.laarif94@gmail.com (LAARIF Marouane)</managingEditor>
    <webMaster>marouane.laarif94@gmail.com (LAARIF Marouane)</webMaster>
    <lastBuildDate>Tue, 21 Oct 2025 23:03:01 +0100</lastBuildDate>
    <atom:link href="https://blog.laarif-marouane.com/tags/java/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>JMH perfasm on Windows</title>
      <link>https://blog.laarif-marouane.com/posts/jmh-perf-windows/</link>
      <pubDate>Tue, 21 Oct 2025 23:03:01 +0100</pubDate><author>marouane.laarif94@gmail.com (LAARIF Marouane)</author>
      <guid>https://blog.laarif-marouane.com/posts/jmh-perf-windows/</guid>
      <description>&lt;p&gt;When running JMH benchmarks, you&amp;rsquo;ll usually see the profile &lt;code&gt;perfasm&lt;/code&gt;. If you&amp;rsquo;re on Windows, trying to run JMH with that profile fails, as it uses &lt;code&gt;perf&lt;/code&gt;, which is a Linux tool, therefore on Windows we use &lt;code&gt;-xperasm&lt;/code&gt; instead, which relies on &lt;code&gt;xperf&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;p&gt;To have xperf we need to install the &lt;strong&gt;Windows Performance Toolkit (WPT)&lt;/strong&gt;, which contains &lt;code&gt;Xperf.exe&lt;/code&gt;.&#xA;The WPT is distributed as part of the &lt;strong&gt;Windows Assessment and Deployment Kit (ADK)&lt;/strong&gt; or sometimes separately. And can be found here: &lt;a href=&#34;https://learn.microsoft.com/en-us/windows-hardware/get-started/adk-install&#34;&gt;https://learn.microsoft.com/en-us/windows-hardware/get-started/adk-install&lt;/a&gt;&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>When running JMH benchmarks, you&rsquo;ll usually see the profile <code>perfasm</code>. If you&rsquo;re on Windows, trying to run JMH with that profile fails, as it uses <code>perf</code>, which is a Linux tool, therefore on Windows we use <code>-xperasm</code> instead, which relies on <code>xperf</code>.</p>
<p>To have xperf we need to install the <strong>Windows Performance Toolkit (WPT)</strong>, which contains <code>Xperf.exe</code>.
The WPT is distributed as part of the <strong>Windows Assessment and Deployment Kit (ADK)</strong> or sometimes separately. And can be found here: <a href="https://learn.microsoft.com/en-us/windows-hardware/get-started/adk-install">https://learn.microsoft.com/en-us/windows-hardware/get-started/adk-install</a></p>
<p>After the installation, you can then run the benchmarks as administrator.</p>
]]></content:encoded>
    </item>
    <item>
      <title>The assert Trap: A Java Developer&#39;s Cautionary Tale</title>
      <link>https://blog.laarif-marouane.com/posts/java-assert-trap/</link>
      <pubDate>Sat, 19 Jul 2025 16:30:26 +0100</pubDate><author>marouane.laarif94@gmail.com (LAARIF Marouane)</author>
      <guid>https://blog.laarif-marouane.com/posts/java-assert-trap/</guid>
      <description>&lt;p&gt;A couple of years ago, I was assigned a bug: some operation was performed even when validation conditions were not met. A quick glance at the method revealed a peculiar sight:&lt;/p&gt;&#xA;&#xA;&#xA;&#xA;&#xA;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-java&#34; data-lang=&#34;java&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;kt&#34;&gt;void&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;foo&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(...){&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;k&#34;&gt;assert&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;p&#34;&gt;...&lt;/span&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;//validation is here&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;//rest of the logic&lt;/span&gt;&lt;span class=&#34;w&#34;&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;w&#34;&gt;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;That&amp;rsquo;s weird, we&amp;rsquo;re &amp;ldquo;asserting&amp;rdquo; the condition. Right? Wrong!&lt;/p&gt;&#xA;&lt;p&gt;Until that point, I&amp;rsquo;ve never seen &amp;ldquo;assert&amp;rdquo; used in any of the projects I worked on, so I had to do some digging to understand why the validation was not executing.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>A couple of years ago, I was assigned a bug: some operation was performed even when validation conditions were not met. A quick glance at the method revealed a peculiar sight:</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="ln">1</span><span class="cl"><span class="kd">private</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">foo</span><span class="p">(...){</span><span class="w">
</span></span></span><span class="line"><span class="ln">2</span><span class="cl"><span class="w"> </span><span class="k">assert</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="c1">//validation is here</span><span class="w">
</span></span></span><span class="line"><span class="ln">3</span><span class="cl"><span class="w"> </span><span class="c1">//rest of the logic</span><span class="w">
</span></span></span><span class="line"><span class="ln">4</span><span class="cl"><span class="w"></span><span class="p">}</span></span></span></code></pre></div><p>That&rsquo;s weird, we&rsquo;re &ldquo;asserting&rdquo; the condition. Right? Wrong!</p>
<p>Until that point, I&rsquo;ve never seen &ldquo;assert&rdquo; used in any of the projects I worked on, so I had to do some digging to understand why the validation was not executing.</p>
<h3 id="what-is-assert">What is &ldquo;assert&rdquo;?</h3>
<p>The <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html">Oracle documentation</a> defines it as :</p>
<blockquote>
<p> a statement in the Java programming language that enables you to test your assumptions about your program. For example, if you write a method that calculates the speed of a particle, you might assert that the calculated speed is less than the speed of light.
&hellip;
Experience has shown that writing assertions while programming is one of the quickest and most effective ways to detect and correct bugs. As an added benefit, assertions serve to document the inner workings of your program, enhancing maintainability.</p></blockquote>
<p>Example usage :</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-Java" data-lang="Java"><span class="line"><span class="ln">1</span><span class="cl"><span class="kd">private</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">setMaxRetries</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">maxRetries</span><span class="p">){</span><span class="w">
</span></span></span><span class="line"><span class="ln">2</span><span class="cl"><span class="w"></span><span class="k">assert</span><span class="w"> </span><span class="n">maxRetries</span><span class="o">&gt;</span><span class="n">MIN_RETRIES</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">maxRetries</span><span class="o">&lt;</span><span class="w"> </span><span class="n">MAX_RETRIES</span><span class="w">
</span></span></span><span class="line"><span class="ln">3</span><span class="cl"><span class="w"></span><span class="p">...</span><span class="w"> </span><span class="c1">//perform action</span><span class="w">
</span></span></span><span class="line"><span class="ln">4</span><span class="cl"><span class="w"></span><span class="p">}</span></span></span></code></pre></div><h3 id="when-to-use-it-when-not-to-use-it-">When to use it, when not to use it ?</h3>
<p>Ok, reading that and seeing the example made me wonder why it&rsquo;s so rare to see it used.
When do I need to use it? And the persistent question, why is it not executing?
Well, reading the docs actually provides all the answers.</p>
<p>As for when to use assertions, the short answer is: probably never.
But the intended use is:</p>
<ul>
<li><strong>Internal Invariants:</strong> To check conditions. If false, indicate a bug in your code. For instance, ensuring a private helper method always receives non-null arguments from within your class.</li>
<li><strong>Post-conditions:</strong> To verify that the state of an object is as expected after a method execution.</li>
<li><strong>Pre-conditions (for private methods):</strong> Similar to internal invariants; checking conditions for private methods that are only called internally.</li>
<li><strong>Control-flow invariants:</strong> For example, in a <code>switch</code> statement with an <code>enum</code>, using <code>assert false</code> in the <code>default</code> case ensures all enum values are handled.</li>
</ul>
<p>And for when to not use them :</p>
<ul>
<li><strong>Validating public method arguments:</strong> Public API inputs should be validated using explicit checks (e.g., <code>if (arg == null) throw new IllegalArgumentException();</code>). These validations are crucial for the correct and safe execution of your application and <em>must</em> always be active. <code>assert</code> is designed to be disabled in production.</li>
<li><strong>Enforcing business rules:</strong> Business logic validations (e.g., &ldquo;An order must have at least one item&rdquo;) are not programming errors; they are expected conditions that the application must handle gracefully, typically by throwing specific business exceptions or returning error codes.</li>
<li><strong>Side effects:</strong> Assertion conditions should not cause any side effects because they might be disabled. For instance, <code>assert calculateAndSetState();</code> is a very bad idea.</li>
<li><strong>Replacing error handling:</strong> Assertions are for internal development-time checks, not for handling recoverable runtime errors.</li>
</ul>
<h3 id="so-what-was-happening-">So what was happening ?</h3>
<p>Reading through the documentation, I came across the following statements:</p>
<blockquote>
<p>There are also situations where you should <em>not</em> use them:</p>
<ul>
<li>Do <em>not</em> use assertions for argument checking in public methods.
Argument checking is typically part of the published specifications (or <em>contract</em>) of a method, and these specifications must be obeyed whether <strong>assertions are enabled or disabled.</strong>..</li>
<li>Do <em>not</em> use assertions to do any work that your application requires for correct operation.
Because <strong>assertions may be disabled</strong>&hellip;</li>
</ul></blockquote>
<p>And that was the answer: assertions can be disabled and, indeed, they are by default.
When you run a Java application from the command line, assertions are not active unless explicitly enabled. This is a deliberate design choice, reflecting their role as development-time debugging aids rather than production-time safeguards.
To enable assertions, you can run the application passing the flags <code>--enableassertions</code> or  <code>-ea</code>.</p>
<h3 id="the-fix">The Fix:</h3>
<p>The fix was very simple. Once I understood what was happening, it was just a matter of replacing the assertion with an if statement that throws a descriptive exception.</p>
<h3 id="conclusion">Conclusion:</h3>
<p>This bug highlighted a crucial distinction: <code>assert</code> is a tool for catching internal programming errors during development and testing, but it is <strong>not</strong> a substitute for runtime validation and error handling of external inputs or business logic.</p>
<p>The silent failure of the <code>assert</code> in our production environment reinforced a fundamental best practice: <strong>never rely on assertions for code that must execute for your application to be correct and secure.</strong> If a condition is critical for the application&rsquo;s functionality or data integrity, it must be validated with explicit <code>if</code> statements and appropriate exception handling, ensuring it runs reliably regardless of JVM flags.</p>
<hr>
<h4 id="references-and-further-reading">references and further reading:</h4>
<ul>
<li><a href="https://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html">Oracle documentation</a></li>
<li><a href="https://stackoverflow.com/questions/2758224/what-are-assertions-in-java-and-when-should-they-be-used">This stackoverflow answer</a></li>
<li><a href="https://stackoverflow.com/a/19425549">Another stackoverflow discussion</a></li>
</ul>
<hr>
]]></content:encoded>
    </item>
  </channel>
</rss>
