Brett Code

printassert

Stepwise Development


Scripting Made Easy
  1. Write Code!
  2. Check with print!
  3. Contract with assert!
I never was much of a programmer. And for the most, I can't be bothered any more. But towards the end there (or is it, here), I started to see the value in assert's. And that's what this page is about.

All hail printassert!


An Inane Code Example

import random
x = random.randint(0, 9)
y = 2 * x
#print("x: %i, y: %i" % (x, y))
assert(y/2==x), "This is the definition."
assert(y%2==0), "y should be even."
assert(type(y)==int), "Very Bad News!"
  1. Number Doubling Code
  2. Visual check that all is well with print.
  3. Make a lasting contract with assert.
  4. Comment out print.
  5. Move On!

It's not production grade. But then, I am not writing production grade code. I am writing scripts.


And that's about what I had to say. For years, I had been using print as my primary debugging tool. And now, I've added assert as a built-in self-check, making it that much harder for me to break my own code.

Also, I think it's more elegant.


Brett Code


And from there, it's a short jump to type declarations, which as grand as those are, tend to be overkill for one such as I.


© copyright 2022 Brett Paufler
paufler.net@gmail.com