Skip to main content

Thinking Invariant

· 3 min read
Prakash Hegade
Instructional Systems Scientist

Consider an example of a teacher walking down a row of desks, searching for the highest test score in the class. The teacher picks up the first student's paper. Since it’s the only one the teacher has seen, it represents the highest score so far. As the teacher moves to the second desk, a comparison is made between the new score and the one in memory. If it's higher, the teacher updates the memory; if not, the teacher retains the old score. After comparing the scores of fifty students, the value that the teacher is holding on to is always the maximum score encountered up to that exact moment. In computer science and algorithm design, we call this reliable anchor an “invariant”.

The same idea appears in any programs that we build. Consider a simple bank account application where customers are required to maintain a minimum balance of 1000/-. A customer may deposit money, withdraw money, or view the account balance. Although the account balance changes frequently, a rule must always be applied: the balance should never fall below 1000/-. Every operation in the program must preserve this rule. If a withdrawal would cause the balance to drop below 1000/-, the transaction must be rejected. This property remains true before, during, and after every operation. Such a property, which must always hold throughout the execution of a program, is called an invariant.

Invariants appear in all algorithms. Consider a program that searches for the largest number in a list. At every step, the variable max_so_far must store the largest value encountered up to that point. Similarly, when searching for the smallest number, min_so_far must always represent the smallest value seen so far. When calculating a total, sum_so_far must equal the sum of all processed elements. If the task is to count records, count must accurately reflect the number examined. Even when computing an average, the running sum and count must always correspond to the elements processed so far. In each case, the invariant acts as a trusted fact that remains true throughout the execution of the algorithm and guides it toward the correct result.

Invariants are important for AI-assisted programming and Vibe Coding. Human programmers often carry important rules in mind while designing a solution. An AI, however, focuses on generating code that appears to satisfy the requested functionality. Without clear constraints, it may produce solutions that work in some cases but violate critical business rules. Should we therefore include invariants carefully in our AI coding prompts? Consider an AI-powered e-commerce application. The AI may decide which products to recommend or how to personalize search results, but certain rules must always hold true. A customer should never be charged twice for the same order, and the payment collected must always match the order value. These invariants act as guardrails that help the AI generate safer solutions. If AI can generate thousands of lines of code in minutes, are we spending enough time defining the few rules that must never be broken?