1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//! # Documentation and Knowledge Metrics
//!
//! This module measures whether the knowledge needed to safely maintain
//! a codebase is actually documented and findable, not just whether
//! documentation technically exists somewhere: does a new engineer, or
//! an existing one working on unfamiliar code, have what they need to
//! make a safe change, or does that knowledge live only in the heads of
//! a shrinking number of tenured people. A system maintained for years
//! by the same two engineers can function perfectly well with almost no
//! written documentation, right up until both of those engineers leave
//! within the same year — at which point the knowledge is discovered to
//! have never been captured anywhere durable. That risk has a standard
//! name in the industry: the **bus factor** (or truck factor).
//!
//! ## Formula
//!
//! ```text
//! Bus factor = the minimum number of people whose combined knowledge
//! share meets or exceeds a critical threshold (commonly 50%)
//!
//! At risk when bus_factor <= minimum_safe_bus_factor
//! ```
//!
//! ## Why it matters
//!
//! Documentation existence is not the same as documentation usefulness —
//! counting wiki pages or READMEs tells you almost nothing about whether
//! knowledge is actually accessible when needed. Bus factor measures the
//! underlying risk directly: how concentrated is the knowledge needed to
//! safely change a system. A low bus factor can hide behind apparent
//! stability — a system that has not changed in years is not necessarily
//! low-risk, it may simply not have needed its sole expert yet — and
//! discovering the gap only during an emergency staff transition is
//! exactly the expensive, avoidable failure mode this module exists to
//! surface in advance.
//!
//! ## Example
//!
//! ```rust
//! use software_engineering::documentation_and_knowledge_metrics::{
//! bus_factor, is_bus_factor_at_risk,
//! };
//!
//! // One person holds 60% of the knowledge share for a system: a single
//! // departure alone crosses the 50% critical threshold.
//! let concentrated = bus_factor(&[60.0, 25.0, 15.0], 50.0).unwrap();
//! assert_eq!(concentrated, 1);
//! assert!(is_bus_factor_at_risk(concentrated, 2));
//!
//! // Five people each hold an even 20% share: it takes three departures
//! // to cross the same threshold.
//! let spread_out = bus_factor(&[20.0, 20.0, 20.0, 20.0, 20.0], 50.0).unwrap();
//! assert_eq!(spread_out, 3);
//! assert!(!is_bus_factor_at_risk(spread_out, 2));
//! ```
//!
//! ## Pitfalls
//!
//! - **Counting documentation existence rather than usefulness** — tells
//! you almost nothing about whether knowledge is actually accessible
//! when needed.
//! - **Never checking documentation staleness relative to how much the
//! system has changed** — risks actively misleading, out-of-date
//! content.
//! - **Mistaking apparent stability for low risk** — a system that has
//! not changed in years can mask a severe, undocumented bus-factor
//! problem behind a system that simply has not yet needed its sole
//! expert.
//! - **Discovering critical undocumented knowledge only during an
//! emergency staff transition** — the expensive, avoidable failure mode
//! this chapter is built to prevent.
//!
//! ## Sources
//!
//! - Chapter 4.6, Documentation and knowledge metrics.
//! - The "bus factor" (or "truck factor") is a widely used, informally
//! named industry concept for knowledge-concentration risk.
//!
//! Topic doc: software-engineering-metrics/locales/en-001/chapters/04-06-documentation-and-knowledge-metrics.md
/// The bus factor: the minimum number of people whose combined knowledge
/// share meets or exceeds `critical_threshold_percent` (commonly `50.0`).
///
/// Sorts a copy of `knowledge_shares_percent` in descending order and
/// accumulates from the largest share down, counting how many people it
/// takes to reach the threshold. A low result means knowledge is
/// dangerously concentrated in a few people; a high result means it is
/// spread widely.
///
/// # Arguments
///
/// * `knowledge_shares_percent` — each person's percentage share of
/// understanding or ownership of the system. Values are assumed to be
/// finite, non-NaN percentages (they need not sum to exactly `100.0`).
/// * `critical_threshold_percent` — the combined share (e.g. `50.0`)
/// whose loss is considered critical.
///
/// # Returns
///
/// The number of people whose combined share reaches the threshold, or
/// `None` if `knowledge_shares_percent` is empty, or if the shares never
/// reach `critical_threshold_percent` even after summing all of them.
///
/// # Examples
///
/// ```rust
/// use software_engineering::documentation_and_knowledge_metrics::bus_factor;
///
/// // One person alone holds 60%, past the 50% threshold.
/// assert_eq!(bus_factor(&[60.0, 25.0, 15.0], 50.0), Some(1));
///
/// // Five even 20% shares need three of them to cross 50%.
/// assert_eq!(bus_factor(&[20.0, 20.0, 20.0, 20.0, 20.0], 50.0), Some(3));
///
/// assert_eq!(bus_factor(&[], 50.0), None);
/// // Shares that never reach the threshold even combined.
/// assert_eq!(bus_factor(&[10.0, 10.0], 50.0), None);
/// ```
/// Whether a bus factor is at or below a defined minimum-safe threshold
/// (e.g. a bus factor of 1 or 2 is commonly considered dangerously low).
///
/// True iff `bus_factor <= minimum_safe_bus_factor`.
///
/// # Arguments
///
/// * `bus_factor` — a bus factor computed by [`bus_factor`].
/// * `minimum_safe_bus_factor` — the smallest bus factor considered
/// acceptable.
///
/// # Returns
///
/// `true` if the bus factor is at or below the minimum-safe threshold.
///
/// # Examples
///
/// ```rust
/// use software_engineering::documentation_and_knowledge_metrics::is_bus_factor_at_risk;
///
/// assert!(is_bus_factor_at_risk(1, 2));
/// assert!(!is_bus_factor_at_risk(3, 2));
/// ```