There are 1.4 billion insects for every human, most of them on your code
The concepts on this post are useful for any language, examples are from Nim.
Defensive Programming usually takes the form of a Unittest,
Unittest checks a "unit" of code, validates using assertions on special test runs,
runs a code block and asserts that the result equals a predefined "sample" result.
This is good, but is has some limitations,
the input, data and context is very often faked (mocked),
the input is usually not asserted because is kinda fixed,
the output is result of the inputs so neither is completely real world,
it runs on "dedicated" automated tests runs.
- How can tests be more integrated and close to the code where the bug lives?
Bugs is the entropy of the universe trying to reverse engineer your code while you are still writing it
Side Effect Free programming is focused on eliminating side effects on the code,
and monitoring or controlling them where you can not eliminate them.
Functional programming tries to reduce Side Effects,
without Side Effects the Bugs can not spread on the code, helps reducing Bugs.
Side Effects examples:
- Writing to terminal or files, reading from terminal, files or user input.
- Mutating in-place of variables, globals, objects, types, data structures.
- Date/time functions, Win32 API/Registry, Databases, HTTP, GUI, exceptions.
Immutability changes everything!
Immutability of variables helps to fight Bugs by not allowing unwanted mutations,
using Immutability whenever possible is very encouraged.
Nim is immutable by default and has 3 kinds of variables:
letits a runtime immutable variable.
constits compile-time immutable variable.
varits runtime mutable variable.
TDD is Poor-Man's Contracts
Contracts can assert on input, data and context on the beginning of a code block,
and can also assert on the result at the end of a code block,
it lives in the code block as close as possible to it, it runs an actual real run,
everything is real inputs, data, context and whatnot,
on programming languages that support compile-time code execution all this can happen at compile time if desired.
The night is dark and full of errors
Contra reimagines Design By Contract in a modern KISS way,
all the benefits of Assertive Programming on statically typed compiled language,
like everything on Nim it takes a lot of care about performance,
it allows fast contracts with zero cost at runtime with a comfy API,
requires minimal modifications on existing code, just 2 lines to add a Contract.
Lets take a buggy example code and add a Contract to it:
proc myFunction(mustBePositive: int): int = result = mustBePositive - 1
mustBePositive must be Positive, lets use the function:
proc myFunction(mustBePositive: int): int = result = mustBePositive - 1 echo myFunction(0)
import contra func myFunction(mustBePositive: int): int = preconditions mustBePositive > 0 # Require postconditions result > 0 # Ensure result = mustBePositive - 1 echo myFunction(0)
The Contract shields our code from the Bug, wont allow the Bug to spread.
preconditionstakes preconditions separated by commas, asserts on arguments or local variables.
postconditionstakes postconditions separated by commas, must assert on
result, can assert on local variables.
To keep things simple, no
invariants blocks are required,
you can pass invariants mixed on the
postconditions but is optional,
Your CI service can compile and run a binary with all assertions enabled.
0 lines of code to Release build and
9 lines of code to Debug build.
Make your users work for you
You can now delegate the bug hunting on collaborators and trusted users,
encouraging them to try development binaries compiled with all assertions enabled.
But the same code can also produce a binary with all assertions disabled,
Contra produces no code when build for release, then you pwn on all Benchmarks.
Nim has automatic Dead Code Elimination.
- Convert your users into being your Gulag CI!.
nimble install contra
- Star Contra on GitHub!
- Learn Nim today.
- Contra on Nimble.
- Nim Telegram Group
- Nim Sticker Pack.
- Grupo de Telegram en Español.
- Say Hi on the bridged Gitter, Matrix, Telegram, IRC, Discord.