Part I — Foundations · Chapter 3 · Lesson 5 of 5 · 8 min read
Chapter 3 Checkpoint: Validation and Scoring Engine
Checkpoint level: Foundation
Purpose: Integrate boolean decisions, while and for loops, ranges, collection iteration, break, continue, nesting, invariants, and defensive termination in one bounded rules program.
Checkpoint outcomes
By completing this checkpoint, you will demonstrate that you can:
- validate a bounded collection of source-defined records;
- skip invalid records without corrupting accounting;
- calculate scores through explicit policy rules;
- classify valid results by ordered thresholds;
- stop safely on a critical condition or work limit;
- trace nested control flow and exit reasons;
- maintain loop invariants and progress evidence; and
- produce a complete, safe operational summary.
Project scenario
A service team needs a command-line engine that validates support-case records, assigns each valid case a numeric score, converts the score into a priority, and stops early if it encounters a critical security case. The foundation version uses a source-defined list of string-keyed maps.
3C.1 Required record fields
| Field | Meaning | Valid values |
|---|---|---|
| id | Readable case identifier | Nonempty string |
| impact | Service-impact score | Integer from 0 through 10 |
| affected_users | Number of affected users | Nonnegative integer |
| customer_tier | Service tier | standard, gold, or platinum |
| security_relevant | Security significance | Boolean |
3C.2 Validation rules
- Every required key must be present.
- Impact must be within 0 through 10.
- Affected users must not be negative.
- Customer tier must be one of the approved strings.
- An invalid record is skipped with reason INVALID_RECORD.
- Skipped records increase inspected and invalid counts but do not affect score totals or priority counts.
3C.3 Scoring policy
Start each valid record at zero and add:
- impact multiplied by 10;
- 20 points when affected users are 100 or more;
- 10 points for gold tier or 20 points for platinum tier; and
- 50 points when security_relevant is true.
Keep the components named while calculating so a reader can explain the final score.
3C.4 Priority classification
- Score 120 or higher: critical
- Score 80 through 119: high
- Score 40 through 79: medium
- Score below 40: low
A valid security-relevant record with score at or above 120 is a critical security case. Record its identifier and stop processing after its result has been counted and reported.
3C.5 Defensive limits
- Declare max_records as a constant.
- Stop before inspecting more than max_records records.
- Record LIMIT_EXHAUSTED when the input collection contains more work than permitted.
- Keep process execution, file, and network capabilities disabled.
- Limit printed details to approved fields and calculated evidence.
3C.6 Required program state
Declare and maintain these summary values:
- inspected_count — records reached by the outer loop;
- valid_count — records that pass validation;
- invalid_count — records skipped after validation failure;
- total_score — sum of valid record scores;
- low_count, medium_count, high_count, and critical_count;
- critical_case_id — null until a critical security case is found;
- limit_exhausted — whether the record budget stopped processing; and
- completion_code — COMPLETE, CRITICAL_FOUND, or LIMIT_EXHAUSTED.
3C.7 Required control-flow design
- Iterate through records with a for loop.
- Before inspecting a record, stop if inspected_count has reached max_records.
- Increment inspected_count exactly once for every reached record.
- Validate the required fields and allowed ranges.
- For an invalid record, increment invalid_count, print a safe diagnostic, and continue.
- For a valid record, calculate named score components and total score.
- Classify the score with one ordered if/else-if/else chain.
- Update valid, total-score, and priority counters together.
- Print a labeled result for the record.
- If the record is a critical security case, save its id, set the completion code, and break.
- After the loop, distinguish critical success, limit exhaustion, and normal completion.
3C.8 Loop invariants
State and verify these invariants:
- Accounting: inspected_count equals valid_count plus invalid_count after every completed record decision.
- Priority totals: valid_count equals low_count plus medium_count plus high_count plus critical_count.
- Score total: total_score equals the sum of scores for valid processed records only.
- Bounds: inspected_count never exceeds max_records.
- Critical exit: critical_case_id is non-null only when a counted critical security record caused early exit.
3C.9 Completion codes
| Code | Meaning |
|---|---|
| COMPLETE | All available records were processed within the limit. |
| CRITICAL_FOUND | A critical security case was counted and caused early exit. |
| LIMIT_EXHAUSTED | The record budget stopped processing before collection exhaustion. |
3C.10 Reference implementation outline
Attempt the checkpoint independently before using this outline.
const max_records = 10
const user_threshold = 100
const critical_threshold = 120
const high_threshold = 80
const medium_threshold = 40
let inspected_count = 0
let valid_count = 0
let invalid_count = 0
let total_score = 0
let low_count = 0
let medium_count = 0
let high_count = 0
let critical_count = 0
let critical_case_id = null
let limit_exhausted = false
let completion_code = "COMPLETE"
for case in cases {
if inspected_count >= max_records {
limit_exhausted = true
completion_code = "LIMIT_EXHAUSTED"
break
}
inspected_count += 1
let valid = true
if case.id == "" { valid = false }
if case.impact < 0 or case.impact > 10 { valid = false }
if case.affected_users < 0 { valid = false }
let approved_tier = case.customer_tier == "standard" or case.customer_tier == "gold" or case.customer_tier == "platinum"
if not approved_tier { valid = false }
if not valid {
invalid_count += 1
print "Case {case.id}: INVALID_RECORD"
continue
}
let impact_points = case.impact * 10
let user_points = 0
if case.affected_users >= user_threshold { user_points = 20 }
let tier_points = 0
if case.customer_tier == "gold" { tier_points = 10 } else if case.customer_tier == "platinum" { tier_points = 20 }
let security_points = 0
if case.security_relevant { security_points = 50 }
let score = impact_points + user_points + tier_points + security_points
let priority = "low"
if score >= critical_threshold {
priority = "critical"
critical_count += 1
} else if score >= high_threshold {
priority = "high"
high_count += 1
} else if score >= medium_threshold {
priority = "medium"
medium_count += 1
} else {
priority = "low"
low_count += 1
}
valid_count += 1
total_score += score
print "Case {case.id}: score {score}, priority {priority}"
if priority == "critical" and case.security_relevant {
critical_case_id = case.id
completion_code = "CRITICAL_FOUND"
break
}
}
print "Completion: {completion_code}"
print "Inspected: {inspected_count}"
print "Valid: {valid_count}"
print "Invalid: {invalid_count}"
print "Total score: {total_score}"
print "Priorities: low={low_count}, medium={medium_count}, high={high_count}, critical={critical_count}"
print "Critical case: {critical_case_id}"
The outline assumes every record has the required keys so field access is safe. For malformed maps with missing keys, validate presence using the supported map operations available in your environment before accessing those fields.
3C.11 Required test matrix
| Case | Purpose | Expected behavior |
|---|---|---|
| A | Ordinary mixed valid records | COMPLETE; all valid records classified |
| B | Impact -1 | INVALID_RECORD; continue |
| C | Impact 11 | INVALID_RECORD; continue |
| D | Affected users -1 | INVALID_RECORD; continue |
| E | Unsupported tier | INVALID_RECORD; continue |
| F | Score exactly 40 | medium |
| G | Score exactly 80 | high |
| H | Score exactly 120 without security | critical; processing continues |
| I | Critical security case | CRITICAL_FOUND; counted, then break |
| J | More records than max_records | LIMIT_EXHAUSTED |
| K | Empty collection | COMPLETE with zero counts |
3C.12 Boundary and overlap tests
- Test impact 0 and 10 as valid boundaries.
- Test affected_users 99 and 100.
- Test standard, gold, and platinum tier contributions.
- Construct scores immediately below and exactly at 40, 80, and 120.
- Test a critical score without security relevance to prove that it does not trigger early exit.
- Place invalid records before and after valid records to prove continue preserves accounting.
- Place a critical security record in the first, middle, and final position.
3C.13 Validation workflow
- Prepare source-defined records for one test.
- Predict validation, score components, score, priority, and completion code.
- Trace inspected, valid, invalid, priority, and total-score state after each record.
- Run formatting and formatting check.
- Run source checking.
- Run the program.
- Compare actual output with predictions and invariants.
- Record pass or fail with the minimal correction.
3C.14 Deliberate defects
Create temporary variants that demonstrate:
- continue before invalid_count is updated;
- break before the critical record is counted;
- a classification rule ordered from low to high;
- an inspected_count update after a possible continue;
- a max_records comparison with an off-by-one error;
- a priority counter omitted from one branch; and
- an unbounded while-loop replacement for the finite for loop.
For each defect, state the broken invariant, observed output, root cause, correction, and regression test.
3C.15 Acceptance criteria
- The source formats cleanly and passes checking.
- All required validation rules are enforced.
- Invalid records are skipped without affecting valid totals.
- Score components and final scores are correct.
- Threshold boundaries classify exactly as specified.
- A critical security case is counted before early exit.
- The record limit prevents excess inspection.
- All loop invariants hold for every test.
- Completion codes distinguish every exit mode.
- No unnecessary capability, secret, file, network, or process access is used.
3C.16 Submission package
- validation_scoring_engine.pea in canonical formatting;
- a data dictionary and scoring-policy note;
- prediction and result records for cases A–K;
- per-record control-flow traces for representative cases;
- invariant proofs and maximum-work calculation;
- format-check, source-check, and execution evidence;
- a defect-and-correction table; and
- a security statement confirming bounded, capability-free execution.
3C.17 Assessment rubric
| Area | Evidence of mastery |
|---|---|
| Validation | Malformed and out-of-range records are rejected consistently. |
| Scoring | Every component, score, and threshold result is correct. |
| Loop control | Continue and break preserve required state and target the intended loop. |
| Invariants | Accounting, priority totals, scores, and bounds remain consistent. |
| Termination | Finite input, record bounds, and completion evidence are explicit. |
| Testing | Normal, invalid, boundary, early-exit, empty, and limit cases are covered. |
| Output | Results are labeled, deterministic, traceable, and safe. |
| Workflow | Formatting, checking, execution, diagnosis, and retesting are recorded separately. |
Completion standard: Every required test passes, all invariants hold, and the learner can explain each exit mode without guessing.
3C.18 Reflection questions
- Which invariant caught the most useful defect?
- Why must invalid records increase inspected_count?
- Why is a critical record counted before break?
- Which threshold boundary was easiest to implement incorrectly?
- How does the record limit complement collection exhaustion?
- Which output fields make an early exit auditable?
- What validation would move to the host in a production integration?
- How would functions improve this program in Chapter 4?
Chapter 3 summary
You can now design ordered decisions, reason about truthiness and thresholds, build controlled while loops, iterate through ranges and collections, and use break and continue without losing state or termination guarantees. Reliable control flow combines explicit policies, loop invariants, progress measures, boundary tests, completion codes, bounded work, and safe output.
Transition to Chapter 4
Chapter 4 turns repeated logic into structured, reusable programs. The next lesson covers declaring and calling functions, including function syntax, argument evaluation, local scope, return behavior, and small reusable contracts.
Checkpoint status: Approved.