<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Hi Liang,</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof ContentPasted0">
Thank you for your response! The provided guidelines are quite helpful. I have</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof ContentPasted0">
noticed that different packages in JDK use the <code>assert</code> keyword to varying degrees.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof ContentPasted0">
I wonder if this is more a matter of developer preference (if it is not the performance</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof ContentPasted0">
reason mentioned by Alan). :P</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof ContentPasted0">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof ContentPasted0">
Regards,<br>
Daohan<br class="ContentPasted0">
<br>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> liangchenblue@gmail.com <liangchenblue@gmail.com><br>
<b>Sent:</b> Sunday, July 16, 2023 7:09<br>
<b>To:</b> Daohan Qu <quadhier@outlook.com><br>
<b>Cc:</b> core-libs-dev@openjdk.org <core-libs-dev@openjdk.org><br>
<b>Subject:</b> Re: Questions about using `assert` in Java</font>
<div> </div>
</div>
<div>
<div dir="ltr">Hi Daohan,
<div>assert keyword is safely usable within production code. I don't think it has a significant performance impact if it is not enabled. Thus, you should not enable it in production unless you are debugging.</div>
<div><br>
</div>
<div>A package that uses assert extensively is java.lang.invoke package.</div>
<div><br>
</div>
<div>For me, these are the general notes for using assert:</div>
<div>1. They should not perform side-effects, such as changing variables. Having -ea, -esa and no flag run differently is definitely not desirable.</div>
<div>2. assert should not be used to defend against incorrect user inputs; it should only be used to ensure the correctness of internal states of your program, as assert is optional.</div>
<div><br>
</div>
<div>Chen Liang</div>
</div>
<br>
<div class="x_gmail_quote">
<div dir="ltr" class="x_gmail_attr">On Sun, Jul 16, 2023 at 12:54 AM Daohan Qu <<a href="mailto:quadhier@outlook.com">quadhier@outlook.com</a>> wrote:<br>
</div>
<blockquote class="x_gmail_quote" style="margin:0px 0px 0px 0.8ex; border-left:1px solid rgb(204,204,204); padding-left:1ex">
<div>
<div dir="auto"><span style="color:rgb(0,0,0); font-family:Calibri,Helvetica,sans-serif; font-size:12pt">Dear developers,</span><br>
</div>
<div id="x_m_-4355526851130570919mail-editor-reference-message-container" dir="auto">
<div style="font-size:12pt; color:rgb(0,0,0)"><font face="Calibri, Helvetica, sans-serif"><br>
</font></div>
<div style="font-size:12pt; color:rgb(0,0,0)"><font face="Calibri, Helvetica, sans-serif">I'm a graduate student doing software engineering research.</font></div>
<div style="font-size:12pt; color:rgb(0,0,0)"><span style="font-family:Calibri,Helvetica,sans-serif">Recently, I came across some interesting facts about Java
</span><code style="font-size:inherit; font-style:inherit; font-variant-ligatures:inherit; font-variant-caps:inherit; font-weight:inherit">assert</code><span style="font-family:Calibri,Helvetica,sans-serif"></span></div>
<div style="font-size:12pt; color:rgb(0,0,0)"><span style="font-family:Calibri,Helvetica,sans-serif">statements </span><span style="font-family:Calibri,Helvetica,sans-serif">and have a few questions to ask.</span></div>
<div style="font-size:12pt; color:rgb(0,0,0)">
<div style="font-family:Calibri,Arial,Helvetica,sans-serif"><span style="font-family:Calibri,Helvetica,sans-serif"><br>
</span></div>
<div 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 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 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 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 style="font-family:Calibri,Arial,Helvetica,sans-serif"><br>
</div>
<div 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 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 style="font-family:Calibri,Arial,Helvetica,sans-serif"><span style="font-family:Calibri,Helvetica,sans-serif">production code?</span></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif"><span style="font-family:Calibri,Helvetica,sans-serif"><br>
</span></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif"><span style="font-family:Calibri,Helvetica,sans-serif">By the way, I have read a useful article about using assertions in</span></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif"><span style="font-family:Calibri,Helvetica,sans-serif">Java [2], but it does not provide clear answers to my questions above.</span><br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif"><span style="font-family:Calibri,Helvetica,sans-serif"><br>
</span></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif"><span style="font-family:Calibri,Helvetica,sans-serif">Thank you in advance!</span></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif"><br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif">[1] <a href="https://github.com/search?q=repo%3Aopenjdk%2Fjdk+assert+path%3A%2F%5Esrc%5C%2Fjava.base%5C%2Fshare%5C%2Fclasses%5C%2Fjava%5C%2Futil%5C%2Fconcurrent%5C%2F%2F&type=code" target="_blank">https://github.com/search?q=repo%3Aopenjdk%2Fjdk+assert+path%3A%2F%5Esrc%5C%2Fjava.base%5C%2Fshare%5C%2Fclasses%5C%2Fjava%5C%2Futil%5C%2Fconcurrent%5C%2F%2F&type=code</a></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif"><span style="font-family:Calibri,Helvetica,sans-serif">[2]
<a href="https://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html" target="_blank">
https://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html</a></span></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif"><span style="font-family:Calibri,Helvetica,sans-serif"><br>
</span></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif"><span style="font-family:Calibri,Helvetica,sans-serif">Regards,</span></div>
<span style="font-family:Calibri,Helvetica,sans-serif">Daohan Qu</span><br>
</div>
<br>
</div>
</div>
</blockquote>
</div>
</div>
</body>
</html>