Files
openide/java/java-impl/resources/inspectionDescriptions/MagicNumber.html
Leonid Shalupov 40795fe787 IJI-2422: community/java: move resources under resources root
GitOrigin-RevId: 8b2b63fc6db476ca0c2cfe5cadd84db6c4236d0f
2025-02-05 04:43:28 +00:00

36 lines
1.4 KiB
HTML

<html>
<body>
Reports "magic numbers": numeric literals that are not named by a constant declaration.
<p>Using magic numbers can lead to unclear code, as well as errors if a magic
number is changed in one location but remains unchanged not another. The numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 1000, 0L, 1L, 2L,
0.0, 1.0, 0.0F and 1.0F are not reported by this inspection.</p>
<p>Example:</p>
<pre><code>
void checkFileSize(long bytes) {
if (bytes > 1_048_576) {
throw new IllegalArgumentException("too big");
}
}
</code></pre>
<p>A quick-fix introduces a new constant:</p>
<pre><code>
static final int MAX_SUPPORTED_FILE_SIZE = 1_048_576;
void checkFileSize(long bytes) {
if (bytes > MAX_SUPPORTED_FILE_SIZE) {
throw new IllegalArgumentException("too big");
}
}
</code></pre>
<!-- tooltip end -->
<p>Configure the inspection:</p>
<ul>
<li>Use the <b>Ignore constants in 'hashCode()' methods</b> option to disable this inspection within <code>hashCode()</code> methods.</li>
<li>Use the <b>Ignore in annotations</b> option to ignore magic numbers in annotations.</li>
<li>Use the <b>Ignore initial capacity for StringBuilders and Collections</b> option to ignore magic numbers used as initial capacity when constructing
<code>Collection</code>, <code>Map</code>,
<code>StringBuilder</code> or <code>StringBuffer</code> objects.</li>
</ul>
</body>
</html>