Counting Events in a Loop
A counter increases when a specific event occurs. Its final value depends on both the input and the counting rule.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 2 min
In this article4 chapters
State what gets counted
In the values 7, 3, 9, and 7, there are three items above five and two distinct values above five. A per-item count increments three times. A count of distinct values also needs a collection holding what has already been seen. Membership tests can distinguish first occurrences.
Read one step at a time
After 7 and 3 the count is one. After 9 it is two. After the last 7 it is three. Confirm the counter changes only at the relevant branch using a state trace.[1]
Choose the event that qualifies as one increment
Consider a job receiving five records, one empty, one rejected, and three accepted. A received count increases on every record. A parsed count can exclude the empty record and still include the rejected one. An acceptance count increases only after the domain rule succeeds. Moving an update across a continue changes which of those populations it represents. Write a trace with as many counters as the final report actually exposes. See break and continue to locate which statements a rejected item can skip.
Distinguish records and distinct keys
Given labels north, north, west, an event counter reaches three. A counter of distinct labels reaches two by adding an increment only when membership is first established. If an input stream can replay records, decide whether a repeated identifier should count as a new event or the original one. See frequency maps.
Test one duplicate and one item that fails a required rule. Use a completed batch with no acceptable records to check the return and initialization path too. If the count is a denominator, documenting precisely which event increments it also helps avoid mixing eligible inputs with successful outcomes.