
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
48 lines
1.3 KiB
Plaintext
48 lines
1.3 KiB
Plaintext
== Why is this an issue?
|
|
|
|
An ``++assert++`` is inappropriate for parameter validation because assertions can be disabled at runtime in the JVM, meaning that a bad operational setting would completely eliminate the intended checks. Further, ``++assert++``s that fail throw ``++AssertionError++``s, rather than throwing some type of ``++Exception++``. Throwing ``++Error++``s is completely outside of the normal realm of expected ``++catch++``/``++throw++`` behavior in normal programs.
|
|
|
|
|
|
This rule raises an issue when a ``++public++`` method uses one or more of its parameters with ``++assert++``s.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,java]
|
|
----
|
|
public void setPrice(int price) {
|
|
assert price >= 0 && price <= MAX_PRICE;
|
|
// Set the price
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,java]
|
|
----
|
|
public void setPrice(int price) {
|
|
if (price < 0 || price > MAX_PRICE) {
|
|
throw new IllegalArgumentException("Invalid price: " + price);
|
|
}
|
|
// Set the price
|
|
}
|
|
----
|
|
|
|
|
|
== Resources
|
|
|
|
https://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html[Programming With Assertions]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::../message.adoc[]
|
|
|
|
include::../highlighting.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|