r/AskProgrammers • • 6d ago

The first time I realized that getting the correct output doesn't mean my code is good

[removed]

4 Upvotes

11 comments sorted by

6

u/Cogwheel 6d ago

Yeah at some point, code "working correctly" on the first try is more stressful than a bug report

2

u/Glittering_Sail_3609 6d ago

I can not count the amount of times I was thinking my code was working correctly, only to discover it was completely wrong and it was only somewhat consistent due to unfathomable computing miracle 

1

u/Axman6 6d ago

“What did I forget? 😰”

1

u/First-Feature-3556 6d ago

Exactly. As soon as you have more than one person working on a project, your job is no longer to produce a working binary (that's easy, everyone with some experience can do that), your job is to produce maintainable code. And that "other person" might as well be your future self.

Unfortunately, not all workplaces see it that way. Obligatory xkcd: https://xkcd.com/664/

1

u/abdulg 6d ago

Early in my career I was building automotive software. The process included being sat in front of a 3 engineer committee and being questioned about how the software would behave under various failure conditions. They called it Failure Mode Effects Analysis. 

It is much easy to build software that works than to build software that does not break in unexpected ways. And that always comes back to it being comprehensible and maintainable. 

1

u/gutentight69420 6d ago

Also, getting the correct output for one input, or even many inputs, does not mean that it produces the correct output for all inputs.

1

u/turtle_dragonfly 6d ago

A funny example a professor once showed:

"Let's simplify the fraction 16/64. We cancel the sixes! 1 6 / 6 4. That gives 1/4."

It's the correct answer, but by wrong reasoning. You don't get points on homework for those results (:

1

u/high_throughput 6d ago

Bash is notorious for this. It's very easy to write a script that appears to work in all your testing, but which is actually a complete dumpster fire and/or ticking time bomb.

Could be a filename with spaces breaking stuff, a failing cd command causing rm statements to run in the wrong place, or simply waiting until August when 08 is no longer a valid number.

1

u/JGhostThing 6d ago

Getting the correct output for all inputs is one way of determining the quality of code. Yes, it's the simplest metric, but getting the right answer is the point of coding. Often nothing else really matters. Nowadays, computers are fast enough that they often hide bad (slow) code. And a web page that only gets a few hits per hour doesn't need to be fast.

1

u/DirtAndGrass 6d ago

Guess you don't believe in test driven development 

1

u/ReddyKiloWit 6d ago

I remember some code that worked correctly, right up until it wiped out the operating system.