Hacker News new | ask | show | jobs
by mristin 1837 days ago
In my anecdotal experience, it takes very little time for juniors to pick up adding contracts to their code. You need to grasp implication, equivalence, exclusive or, and get used to declarative code, but then it's easy. (I often compare it to SQL.)

I find contracts personally super useful as I can express a lot of relationships in the code trivially and have them automatically verified. For example, when this input is None, then that output needs to be positive. Or if you dele the item it should not exist in this and that registry, and some related other items should also not exist any more.

My email is in the commits of the repository, feel free to contact me and we can have a chat if you are interested in a larger picture and more details.

1 comments

P.S. I think the important bit is not to be religious about contracts and tests. Sometimes it's easy to write the contracts and have the function automatically tested, sometimes unit tests are clearer.

I tend to limit myself to simple contracts and do table tests for the complex cases to reap the benefits of the both approaches.

If a software module maybe has tests and maybe has contracts, I "reap the benefit" of not being able to rely on either.

Do you have in mind some technique to turn contracts into tests or tests into contracts automatically, in order to close the gaps in quality control?

Sorry, I did not express myself clearly. For certain functions you can express all the properties in contracts and have them automatically tested.

For other functions, you write some obvious contracts (so that those are also tested in integration tests or end-to-end tests), but since writing all the contracts would be too tedious or unmaintainable, you test these functions additionally using, say, table-driven tests where you specifically test with data points for which you could not write the preconditions or you check the post-conditions manually given the input data. For example, sometimes is easier to generate the input, manually inspect the results and write the expected results in the test table.

> [...] turn contracts into tests [...]

icontract-hypothesis allows you to ghostwrite Hypothesis strategies which you can further refine yourself.