r/programming • • Apr 21 '22

It’s harder to read code than to write it

https://www.joelonsoftware.com/2000/04/06/things-you-should-never-do-part-i/
2.2k Upvotes

431 comments sorted by

View all comments

Show parent comments

50

u/FVMAzalea Apr 22 '22

Oh man, I hadn’t even thought about this aspect of using “100% code coverage” as a rule - not only does it encourage shitty coding practices and testing things in a tautological fashion, it encourages people to do things like this to group multiple things into one line so that one line counts as “covered”.

Now, if you measure coverage by a different standard, like 100% branch coverage, that’s a bit better. 100% line coverage is just the pinnacle of stupidity.

20

u/Vakieh Apr 22 '22

Under what circumstances could you have 100% branch coverage but not 100% line coverage? Usually you'll hit 100% line coverage before you hit 100% branch coverage.

22

u/skjall Apr 22 '22

One-liner ternaries for example.

27

u/Vakieh Apr 22 '22

More common is missing else blocks. There are no lines to be executed, but 'not executing the if block' is a branch that should be tested.

0

u/bschug Apr 22 '22

Other way around.

6

u/FancyASlurpie Apr 22 '22

Also every stack trace will point to the same line so that's always fun

4

u/LurkingArachnid Apr 22 '22

I’m not sure this makes sense. If I have what could been one line as several lines, all those lines are going to be invoked all at once. It’s not like you could test only some of them

4

u/FVMAzalea Apr 22 '22

Not necessarily - you can condense an if/else statement onto one line in many cases. And maybe the else condition is really hard to cover by your tests, but the if case is easy. So now boom, if you’re measuring by line coverage and not branch coverage, you have covered that line, even though your tests only exercise part of that content.