1405
Tests are code too (lemmy.world)
submitted 10 months ago by jroid8@lemmy.world to c/programmerhumor@lemmy.ml
you are viewing a single comment's thread
view the rest of the comments
[-] ChickenLadyLovesLife@lemmy.world 28 points 10 months ago

the code they’re testing has painful hardwired dependencies on expensive external resources

I've told this story elsewhere, but I had a coworker who wrote an app to remote-control a baseball-throwing machine from a PDA (running WinCE). These machines cost upwards of $50K so he only very rarely had physical access to one. He loved to write tests, which did him no good when his code fired a 125 mph knuckleball a foot over a 10-year-old kid's head. This resulted in the only occasion in my career when I had to physically restrain a client from punching a colleague.

[-] jbrains@sh.itjust.works 6 points 10 months ago

Wow. I love that story and I'm glad nobody was hurt.

I wonder whether that happened as a result of unexpected behavior by the pitching machine or an incorrect assumption about the pitching machine in that coworker's tests.

I find this story compelling because it illustrates the points about managing risk and the limits of testing, but it doesn't sound like the typical story that's obviously hyperbole and could never happen to me.

Thank you for sharing it.

[-] ChickenLadyLovesLife@lemmy.world 15 points 10 months ago

It happened because the programmer changed the API from a call that accepted integer values between 0 and 32767 (minimum and maximum wheel speeds) to one that accepted float values between 0.0 and 1.0. A very reasonable change to make, but he quick-fixed all the compiler errors that this produced by casting the passed integer parameters all through his code to float and then clamping the values between 0.0 and 1.0. The result was that formerly low-speed parameters (like 5000 and 6000, for example, which should have produced something like a 20 mph ball with topspin) were instead cast and clamped to 1.0 - maximum speed on both throwing wheels and the aforesaid 125 mph knuckleball. He rewrote his tests to check that passed params were indeed between 0.0 and 1.0, which was pointless since all input was clamped to that range anyway. And there was no way to really test for a "dangerous" throw anyway since the machine was required to be capable of this sort of thing if that's what the coach using it wanted.

[-] Duralf@lemmy.world 5 points 10 months ago

API from a call that accepted integer values between 0 and 32767 (minimum and maximum wheel speeds) to one that accepted float values between 0.0 and 1.0.

This would cause alarm bells to ring in my head for sure. If I did something like that I would make a new type that was definitely not implicitly castable to or from the old type. Definitely not a raw integer or float type.

[-] marcos@lemmy.world 4 points 10 months ago

That kind of code usually is written on a restricted dialect of C.

C is not a language that allows for that kind of safety practice even on the fully-featured version.

[-] Duralf@lemmy.world 3 points 10 months ago

Even in C this is possible. Just wrap the float or whatever in a struct and all implicit conversions will be gone.

[-] jbrains@sh.itjust.works 1 points 10 months ago

Indeed, this is a time for naming conventions that communicate the details that the type system can't clarify. This leads to the long names that senior programmers make fun of. Don't listen to them; let them laugh then make this kind of mistake.

[-] marcos@lemmy.world 1 points 10 months ago

This leads to the long names that senior programmers make fun of.

Hum... The notation that I've seen people making fun of is one where the long names encode the exact same information that C types can handle for you and nothing else. But YMMV.

Anyway, I don't think any naming convention can save you after somebody goes over your entire codebase converting things without care for the semantics. If you are lucky, it's one of the lazy people that do that, and you will "only" have to revise tens of thousands of lines to fix it. If you are unlucky, the same person will helpfully adjust the names for you too.

[-] jbrains@sh.itjust.works 1 points 10 months ago

Yes, any programmer who doesn't care will do damage, but when I see durationInMilliseconds, I think more about what the int means than when I see merely "duration". I don't know how to help the people who read that and ignore it.

The story of the 125 mph knuckleball might help.

load more comments (7 replies)
load more comments (7 replies)
load more comments (8 replies)
this post was submitted on 02 Nov 2023
1405 points (98.5% liked)

Programmer Humor

32042 readers
1299 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS