Hacker News new | past | comments | ask | show | jobs | submit login

Question coming from a long-time sysad whose experience is almost entirely batch and PowerShell, what does it mean to write "testable" code?



You write code that can be broken up finely enough into discrete, stand alone modules that can each be run through a series of tests.

For instance, you might create a model class, and then you know that model should have a name, shouldn't be able to be saved without a name, the name should be x-number of characters.

Then you can write a series of tests that makes sure that, regardless of how the model implements that name, all your assumptions about what that name should look and act like don't change without throwing a red-flag up to whoever is changing that model.


Say you want to test how a program fares when strings are malformed, or the disk is full, etc. It can be hard to simulate this if the code refers to variables and results from functions from all over the place.

Making code into reusable modules code is generally a good thing, but it's even better if you make them in a way that will help you test those chunks independently as well - that is testable code.


Usually it's about keeping it granular enough that various sub-activities can be tested in isolation. If you have one function with 10000 LOCs, that's not really testable beyond "something doesn't work".

Then of course you need some way to automatically run these tests, but that's usually provided by IDEs or standard libraries these days.

Edit: most curious downvote ever...




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: