Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • Thank you! That is a real-life example with which I can identify. I don't think I'm going to be a convert any day soon (I'm 66 and looking at retirement rather than career progression) but I think that the next time I work on a new feature I might try TDD. Commented Jun 13, 2023 at 14:40
  • 3
    @Edwardo The problem is that code only represents flow and structure of a program; but humans working in code generally need a lot more than that to be able to properly reason over it - ideally they need to know everything its original developer knew, which is usually impossible from code alone, so developers in unfamiliar code take guesses based on whatever seems most likely. Good tests can eliminate a lot of guesswork by enshrining real use-cases as a source of truth about its behaviour, giving developers confidence that their future changes aren't based on faulty guesswork. Commented Jun 13, 2023 at 19:39
  • 12
    This also requires the test to be written clearly -- good tests themselves form part of the code documentation (and the best kind of documentation, since you can execute it to see if it's still true). If your "empty list returns ||" test is buried in a number of similar examples without comment then the future maintainer might think that this was just an example of previous behaviour and not an external requirement. If it's in its own clearly labelled test case, or at least is commented with why that particular output is expected, it's less likely to be misinterpreted later. Commented Jun 14, 2023 at 7:15
  • I worked on legacy applications in an environment where unit tests were not possible (it was an EMR's custom scripting language for which no unit test framework existed - or any way to run scripts or make assertions on the output other than displaying it on a user facing page). It was a nightmare of trying to ensure consistent results and bug fixes. Commented Jun 14, 2023 at 19:25
  • That example is a legitimate use case for comments in code. Documenting why something unusual is being done due to external factors. Having unit tests on it is good, but I don't think is sufficient documentation in itself for that kind of thing. The unit test describes the behaviour but doesn't explain why it's like that. Commented Jun 16, 2023 at 3:17