<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
On 15/07/2023 17:53, Daohan Qu wrote:<br>
<blockquote type="cite" cite="mid:BLAPR07MB7508B6FA999645629E2A0374CB35A@BLAPR07MB7508.namprd07.prod.outlook.com">
:
<div id="mail-editor-reference-message-container" dir="auto">
<div class="elementToProof ContentPasted0 ContentPasted1" style="font-size:12pt; color:rgb(0,0,0)">
<div class="ContentPasted1" style="font-family:Calibri,Arial,Helvetica,sans-serif">
<span style="font-family:Calibri,Helvetica,sans-serif"><br>
</span></div>
<div class="ContentPasted1" style="font-family:Calibri,Arial,Helvetica,sans-serif">
<span style="font-family:Calibri,Helvetica,sans-serif">Although
the <code>assert</code> keyword has been around for a
long time and</span></div>
<div class="ContentPasted1" style="font-family:Calibri,Arial,Helvetica,sans-serif">
<span style="font-family:Calibri,Helvetica,sans-serif">is
handy </span><span style="font-family:Calibri,Helvetica,sans-serif">for
invariant checks, it does not seem to be widely used.</span></div>
<div class="ContentPasted1" style="font-family:Calibri,Arial,Helvetica,sans-serif">
<span style="font-family:Calibri,Helvetica,sans-serif">For
example, in the famous
<code>j.u.c</code> packages, nearly all <code>assert</code></span></div>
<div class="ContentPasted1" style="font-family:Calibri,Arial,Helvetica,sans-serif">
<span style="font-family:Calibri,Helvetica,sans-serif">statements
are commented out [1].</span><br>
</div>
<div class="ContentPasted1" style="font-family:Calibri,Arial,Helvetica,sans-serif">
<br>
</div>
<div class="ContentPasted1" style="font-family:Calibri,Arial,Helvetica,sans-serif">
<span style="font-family:Calibri,Helvetica,sans-serif">My
questions are, should <code>
assert</code> be heavily used in Java programs,</span></div>
<div class="ContentPasted1" style="font-family:Calibri,Arial,Helvetica,sans-serif">
<span style="font-family:Calibri,Helvetica,sans-serif">especially
in production code? And should we enable them in the</span></div>
<div class="ContentPasted1" style="font-family:Calibri,Arial,Helvetica,sans-serif">
<span style="font-family:Calibri,Helvetica,sans-serif">production
code?</span></div>
<br>
</div>
</div>
</blockquote>
Asserts are very useful during development or when testing, e.g. the
JDK tests run with -esa and can periodically help catch issues when
testing a change.<br>
<br>
You will find places in the JDK code, esp. in performance critical
code, where assertions are commented out. The reason is that
asserts, even if disabled, increase the method size and can impact
inlining by the compiler at run-time. So while useful when
debugging some issue in such code, they are commended out to avoid
increasing the method size.<br>
<br>
-Alan<br>
</body>
</html>