|
1671 | 1671 | "<p>See also Guideline 7.3.2.</p>" |
1672 | 1672 | ] |
1673 | 1673 | } |
| 1674 | + }, |
| 1675 | + "ADA95_5.5.2": { |
| 1676 | + "checked": false, |
| 1677 | + "language": "Ada", |
| 1678 | + "tags": [ "Language: Ada", "Standard: Ada 95", "Expressions" ], |
| 1679 | + "name": "Published Standards/Ada 95/5.5.2 Array Attributes", |
| 1680 | + "desc": { |
| 1681 | + "html": [ |
| 1682 | + "<b>", |
| 1683 | + "<span style=\"color:red\">This check cannot be automated</span>", |
| 1684 | + "</b>", |
| 1685 | + "", |
| 1686 | + "<p><b>Guideline</b></p>", |
| 1687 | + "<p>• Use array attributes <code>'First</code>, <code>'Last</code>, or <code>'Length</code> instead of numeric literals for accessing arrays.</p>", |
| 1688 | + "<p>• Use the <code>'Range</code> of the array instead of the name of the index subtype to express a range.</p>", |
| 1689 | + "<p>• Use <code>'Range</code> instead of <code>'First .. 'Last</code> to express a range.</p>", |
| 1690 | + "", |
| 1691 | + "<p><b>Example</b></p>", |
| 1692 | + "<pre><code language=\"Ada\">subtype Name_String is String (1 .. Name_Length);", |
| 1693 | + "File_Path : Name_String := (others => ' ');", |
| 1694 | + "...", |
| 1695 | + "for I in File_Path'Range loop", |
| 1696 | + " ...", |
| 1697 | + "end loop;", |
| 1698 | + "</code></pre>", |
| 1699 | + "", |
| 1700 | + "<p><b>Rationale</b></p>", |
| 1701 | + "<p>In the example above, it is better to use <code>Name_String'Range</code> in the <code>for</code> loop than to use <code>Name_String_Size</code>, <code>Name_String'First .. Name_String'Last</code>, or <code>1 .. 30</code> because it is clearer, less error-prone, and less dependent on the definitions of <code>Name_String</code> and <code>Name_String_Size</code>. If <code>Name_String</code> is changed to have a different index type or if the bounds of the array are changed, this will still work correctly. This enhances program reliability.</p>", |
| 1702 | + "", |
| 1703 | + "<p><b>Developer's Note</b></p>", |
| 1704 | + "<p>Guideline 3 (use <code>'Range</code> instead of <code>'First .. 'Last</code>) is covered by <code>ADA95_5.5.1</code>.</p>" |
| 1705 | + ] |
| 1706 | + } |
| 1707 | + }, |
| 1708 | + "ADA95_5.5.3": { |
| 1709 | + "checked": false, |
| 1710 | + "language": "Ada", |
| 1711 | + "tags": [ "Language: Ada", "Standard: Ada 95", "Expressions" ], |
| 1712 | + "name": "Published Standards/Ada 95/5.5.3 Parenthetical Expressions", |
| 1713 | + "desc": { |
| 1714 | + "html": [ |
| 1715 | + "<b>", |
| 1716 | + "<span style=\"color:red\">This check cannot be automated</span>", |
| 1717 | + "</b>", |
| 1718 | + "", |
| 1719 | + "<p><b>Guideline</b></p>", |
| 1720 | + "<p>• Use parentheses to specify the order of subexpression evaluation to clarify expressions (NASA 1987).</p>", |
| 1721 | + "<p>• Use parentheses to specify the order of evaluation for subexpressions whose correctness depends on left to right evaluation.</p>", |
| 1722 | + "", |
| 1723 | + "<p><b>Example</b></p>", |
| 1724 | + "<pre><code language=\"Ada\">(1.5 * X**2)/A - (6.5*X + 47.0)", |
| 1725 | + "2*I + 4*Y + 8*Z + C", |
| 1726 | + "</code></pre>", |
| 1727 | + "", |
| 1728 | + "<p><b>Rationale</b></p>", |
| 1729 | + "<p>The Ada rules of operator precedence are defined in the Ada Reference Manual, §4.5 and follow the same commonly accepted precedence of algebraic operators. The strong typing facility in Ada combined with the common precedence rules make many parentheses unnecessary. However, when an uncommon combination of operators occurs, it may be helpful to add parentheses even when the precedence rules apply. The expression <code>5 + ((Y ** 3) mod 10)</code> is clearer, and equivalent to <code>5 + Y**3 mod 10</code>. The rules of evaluation do specify left to right evaluation for operators with the same precedence level. However, it is the most commonly overlooked rule of evaluation when checking expressions for correctness.</p>" |
| 1730 | + ] |
| 1731 | + } |
| 1732 | + }, |
| 1733 | + "ADA95_5.5.4": { |
| 1734 | + "checked": false, |
| 1735 | + "language": "Ada", |
| 1736 | + "tags": [ "Language: Ada", "Standard: Ada 95", "Expressions" ], |
| 1737 | + "name": "Published Standards/Ada 95/5.5.4 Positive Forms of Logic", |
| 1738 | + "desc": { |
| 1739 | + "html": [ |
| 1740 | + "<b>", |
| 1741 | + "<span style=\"color:red\">This check cannot be automated</span>", |
| 1742 | + "</b>", |
| 1743 | + "", |
| 1744 | + "<p><b>Guideline</b></p>", |
| 1745 | + "<p>• Avoid names and constructs that rely on the use of negatives.</p>", |
| 1746 | + "<p>• Choose names of flags so they represent states that can be used in positive form.</p>", |
| 1747 | + "", |
| 1748 | + "<p><b>Example</b></p>", |
| 1749 | + "<p>Use:</p>", |
| 1750 | + "<pre><code language=\"Ada\">if Operator_Missing then", |
| 1751 | + "</code></pre>", |
| 1752 | + "<p>rather than either:</p>", |
| 1753 | + "<pre><code language=\"Ada\">if not Operator_Found then", |
| 1754 | + "</code></pre>", |
| 1755 | + "<p>or:</p>", |
| 1756 | + "<pre><code language=\"Ada\">if not Operator_Missing then", |
| 1757 | + "</code></pre>", |
| 1758 | + "", |
| 1759 | + "<p><b>Rationale</b></p>", |
| 1760 | + "<p>Relational expressions can be more readable and understandable when stated in a positive form. As an aid in choosing the name, consider that the most frequently used branch in a conditional construct should be encountered first.</p>", |
| 1761 | + "", |
| 1762 | + "<p><b>Exceptions</b></p>", |
| 1763 | + "<p>There are cases in which the negative form is unavoidable. If the relational expression better reflects what is going on in the code, then inverting the test to adhere to this guideline is not recommended.</p>" |
| 1764 | + ] |
| 1765 | + } |
| 1766 | + }, |
| 1767 | + "ADA95_5.5.5": { |
| 1768 | + "checked": false, |
| 1769 | + "language": "Ada", |
| 1770 | + "tags": [ "Language: Ada", "Standard: Ada 95", "Expressions" ], |
| 1771 | + "name": "Published Standards/Ada 95/5.5.5 Short Circuit Forms of the Logical Operators", |
| 1772 | + "desc": { |
| 1773 | + "html": [ |
| 1774 | + "<b>", |
| 1775 | + "<span style=\"color:red\">This check cannot be automated</span>", |
| 1776 | + "</b>", |
| 1777 | + "", |
| 1778 | + "<p><b>Guideline</b></p>", |
| 1779 | + "<p>• Use short-circuit forms of the logical operators to specify the order of conditions when the failure of one condition means that the other condition will raise an exception.</p>", |
| 1780 | + "", |
| 1781 | + "<p><b>Example</b></p>", |
| 1782 | + "<p>Use:</p>", |
| 1783 | + "<pre><code language=\"Ada\">if Y /= 0 or else (X/Y) /= 10 then", |
| 1784 | + "</code></pre>", |
| 1785 | + "<p>or:</p>", |
| 1786 | + "<pre><code language=\"Ada\">if Y /= 0 then if (X/Y) /= 10 then", |
| 1787 | + "</code></pre>", |
| 1788 | + "<p>rather than either:</p>", |
| 1789 | + "<pre><code language=\"Ada\">if Y /= 0 and (X/Y) /= 10 then", |
| 1790 | + "</code></pre>", |
| 1791 | + "<p>or:</p>", |
| 1792 | + "<pre><code language=\"Ada\">if (X/Y) /= 10 then", |
| 1793 | + "</code></pre>", |
| 1794 | + "<p>to avoid <code>Constraint_Error</code>.</p>", |
| 1795 | + "<p>Use:</p>", |
| 1796 | + "<pre><code language=\"Ada\">if Target /= null and then Target.Distance < Threshold then", |
| 1797 | + "</code></pre>", |
| 1798 | + "<p>rather than:</p>", |
| 1799 | + "<pre><code language=\"Ada\">if Target.Distance < Threshold then", |
| 1800 | + "</code></pre>", |
| 1801 | + "<p>to avoid referencing a field in a nonexistent object.</p>", |
| 1802 | + "", |
| 1803 | + "<p><b>Rationale</b></p>", |
| 1804 | + "<p>The use of short-circuit control forms prevents a class of data-dependent errors or exceptions that can occur as a result of expression evaluation. The short-circuit forms guarantee an order of evaluation and an exit from the sequence of relational expressions as soon as the expression's result can be determined.</p>", |
| 1805 | + "<p>In the absence of short-circuit forms, Ada does not provide a guarantee of the order of expression evaluation, nor does the language guarantee that evaluation of a relational expression is abandoned when it becomes clear that it evaluates to <code>False</code> (for <code>and</code>) or <code>True</code> (for <code>or</code>).</p>", |
| 1806 | + "", |
| 1807 | + "<p><b>Notes</b></p>", |
| 1808 | + "<p>If it is important that all parts of a given expression always be evaluated, the expression probably violates Guideline 4.1.4, which limits side-effects in functions.</p>" |
| 1809 | + ] |
| 1810 | + } |
1674 | 1811 | } |
1675 | 1812 | } |
0 commit comments