"Prime Testable" Code

submited by
Style Pass
2021-09-24 14:30:05

Think of the last time you wrote an incredible piece of software... Literally surprising yourself with the beauty, the power, the sheer awesomeness of what you created.

And part of using them effectively is learning how to make your code *easily testable*. Out of the bulging universe of different choices for how to organize your code, choosing a way that makes high-impact, high-value tests easiest to write.

Calculating the 217th prime number, or splitting a string into a list of words, or sorting a sequence - these are all deterministic.

If a function or method you write has these two qualities, it is prime testable. And it is the easiest thing in the world to quickly write powerful tests for.

Often, if you have some code that must be non-deterministic or must have side effects - and every real program has code like that - then you can spin out the prime-testable part in a separate function.

Shove as much of your program's error-prone complexity into that spun-out function as you can. Test the heck out of it. Then any code using it will be reliable and solid.

Leave a Comment