Properties Are a Programmer's Compass ✛ ✎

Original: "Accurate Programming: Thinking about programs in terms of properties"
· Walid Taha, Veronica Gaspes, Rex Page
arXiv:1109.0786 · 2011-09-05 · arXiv non-exclusive · independent analysis · 1 min
You can test a program not with examples but with rules that always hold.

A programmer looks at the function max, which returns the larger of two numbers. How can you be sure it works? You could write tests: max(2,3)=3, max(5,5)=5. But what if there's a bug deep in the code that only shows up with the number 65536? Instead of guessing, you can state a rule: "the result is never less than either argument." That's a property: it must hold for any input.

ScalaCheck automatically generates hundreds of random numbers, plugs them into the property, and reports if the rule is broken. Properties work like a compass: they don't check a single route but set a direction that works for any path. For example, for the function sum (which adds a list of numbers), the property "if you add a positive number, the sum increases" catches an overflow bug that ordinary tests miss.

Properties can be refined, like adjusting a compass. First you say: "the result is non-negative" but even a function that always returns zero satisfies that. So you add: "for a list with one element, the result is 1" and zero is ruled out. Step by step, the properties converge on the one correct program.

Precise programming means thinking about code through its properties. Instead of getting lost in a fog of complexity, you hold a compass of rules that can be checked automatically. That makes programs more reliable and changing them safer.

The name ScalaCheck alludes to the idea of automatic property checking, not to 'checking Scala'—it is a tool for checking properties in Scala.

In the series 'Westworld', robot behavior is governed by complex rules, but their correctness is not guaranteed. Precise programming could help formulate and check behavioral properties, preventing unexpected failures.

Scientists
Erwin SchrödingerHugh Everett IIIMax BornAlbert EinsteinJohn von NeumannAugustin-Louis Cauchy
Concepts
accuracy automation optimization diagnostic accuracy Natural Language Processing real time quantum algorithm drug design open source convergence Machine Learning computer simulation speech recognition personalized medicine artificial intelligence Robotics computation classification exact solution precision