DEV Community

Improve Your Stack Overflow Questions With Unit Tests

Scott Hannen on September 02, 2019

Suppose you’re trying to write code to solve this problem: I’ve got a start date and end date. I want to count the number of times between those ...
Collapse
 
moopet profile image
Ben Sinclair

You could do the same thing with the original question and this clarification:

// Here are my expected results

DataRow("1/1/2019", "2/15/2019", 14); // 2
DataRow("1/1/2019", "1/13/2019", 14); // 0
DataRow("1/1/2019", "12/31/2019", 31); // 8

I know there's not a lot of difference, but it's quicker to write and less code for other people to mentally parse. It's not just about providing code that can be pasted into a file and compiled (the "complete"ness), it's also about how quickly and easily other visitors can run through the program using their brain as the interpreter.

I think the users you spoke to were right: people aren't going to do this. I also think it'd be great if people did.

As an exercise, it's also good because the act of splitting it up into testable chunks with descriptive names means you probably solve this kind of question yourself.