Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

Wiki Markup Never use assertions to validate arguments of public methods. According to the _Java Language Specification_ , [§14 , §14.10, "The {{assert}} Statement"|http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.10] \ [[JLS 2005|AA. References#JLS 05]\]:

...assertions should not be used for argument-checking in public methods. Argument-checking is typically part of the contract of a method, and this contract must be upheld whether assertions are enabled or disabled.

Another problem with using assertions for argument checking is that erroneous arguments should result in an appropriate runtime exception (such as IllegalArgumentException, IndexOutOfBoundsException or NullPointerException). An assertion failure will not throw an appropriate exception.

...

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

MET01-J

medium

probable

medium

P8

L2

Related Guidelines

MITRE CWE

CWE-617. Reachable assertion

Bibliography

...

[[Daconta 2003AA. References#Daconta 03] ]

Item 7. My assertions are not gratuitous

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a0a3fc0b-58d8-4768-b729-d3f956239e63"><ac:plain-text-body><![CDATA[

[[ESA 2005AA. References#ESA 05]]

Rule 68. Explicitly check method parameters for validity, and throw an adequate exception in case they are not valid. Do not use the assert statement for this purpose

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="37ec7a7e-763a-45ed-b81d-ee452e4b8483"><ac:plain-text-body><![CDATA[

[ [JLS 2005AA. References#JLS 05] ]

§14.10, The assert Statement ]]></ac:plain-text-body></ac:structured-macro>

...

      05. Methods (MET)