oct
Loading...
Searching...
No Matches
metrics.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# oct/complexity/metrics.py
4
5"""
6Purpose
7-------
8Calculate CRAP scores from cyclomatic complexity and statement coverage.
9
10Responsibilities
11----------------
12- Apply the pinned CRAP formula without rounding measurements.
13- Reject invalid complexity and coverage inputs.
14
15Diagnostics
16-----------
17Domain: OCT-COMPLEXITY
18Levels:
19 L2 - lifecycle
20 L3 - calculation details
21 L4 - deep tracing
22
23Contracts
24---------
25- The formula is ``CC^2 * (1 - coverage)^3 + CC``.
26- Complexity is a positive integer and coverage is in the inclusive range zero to one.
27
28Dependencies
29------------
30"""
31
32# OCT-LINT: disable-file=dbg_usage reason="Pure deterministic arithmetic has no runtime lifecycle or diagnostic side effects"
33# OCT-LINT: disable-file=dbg_import reason="Pure deterministic arithmetic has no diagnostic dependency"
34
35from __future__ import annotations
36
37
38def calculate_crap(complexity: int, coverage: float) -> float:
39 """Return ``CC^2 * (1 - coverage)^3 + CC``.
40
41 Complexity is a positive integer and coverage is a fraction in the
42 inclusive range zero through one. Failing fast on invalid measurements
43 prevents the reporting layer from presenting approximate scores.
44 """
45 if isinstance(complexity, bool) or not isinstance(complexity, int):
46 raise TypeError("complexity must be an integer")
47 if complexity < 1:
48 raise ValueError("complexity must be at least 1")
49 if not 0.0 <= coverage <= 1.0:
50 raise ValueError("coverage must be between 0.0 and 1.0")
51 uncovered = 1.0 - coverage
52 return float(complexity**2 * uncovered**3 + complexity)
float calculate_crap(int complexity, float coverage)
Definition metrics.py:38