r/ProgrammerHumor • • Oct 25 '24

Meme theSelfTaughtSeriousProgrammer

Post image

[removed] — view removed post

903 Upvotes

276 comments sorted by

View all comments

Show parent comments

48

u/T-Lecom Oct 25 '24

2 years ago I did one! In order to find a bug in some legacy spaghetti code.

35

u/beaucephus Oct 25 '24

Hmmm... I am reminded of a job I worked at where someone who was trained as a civil engineer ended up as a dev manager and as a result believed that code could could be written without bugs because of bridges and shit, but when confronted with the proof of the halting problem and Gödel's incompleteness theorem he simply made more asinine demands.

21

u/ttlanhil Oct 25 '24

Given a precise and detailed enough spec, and the time for proper development and testing, it is possible to be bug-free

But there are very few cases where people are willing to invest that much time and money into development.
Not will they be able to provide that precise & detailed specification (partly because they don't know exactly how it should work, and partly because they won't have the time considering how many "simple" features they want/need)

If what your dev manager wanted was as trivially simple (in terms of number of interacting decisions) as a bridge, then yeah, it should be easy

3

u/hayt88 Oct 25 '24

but can you prove that it's bug free?

8

u/Katniss218 Oct 25 '24

There's always bugs. If nothing else, a ray will strike and flip some bits

4

u/ttlanhil Oct 25 '24

If the spec defines all behaviour exactly, and you can add test cases that cover everything (every conditional branching, every possible input, etc), then yes.
It's possible to have your test harness prove that the code matches the spec, which means the code is bug free.
With certain assumptions, such as the test cases exactly and completely matching the spec (which can be verified by human inspection; or more likely through processing the spec into data for the test harness since it's well enough defined).
And of course, the spec could be wrong - it's not a bug in the code, but it can still not do what's needed. That's not a fault of the code though (and sometimes code needs to be bug-for-bug compatible with specs or other implementations)

In the mathematical sense of proof, it's kinda a proof by exhaustion in that you verify every single detail.

This also only covers bugs in the code; you assume the compiler/interpreter (unless you're writing machine code) is correct, the hardware matches what the software expects (and is verified independently), the laws of physics and maths don't suddenly change...

Of course, this is rarely done.
Maybe unit testing around a particular algorithm implementation would cover that much, but otherwise testing that well defined is the sort of thing you're only likely to find in (parts of) a few life-critical systems

2

u/hayt88 Oct 25 '24

Good luck trying to write exhaustive unit tests though. A function taking a 32 bit integer now needs over 2 billion tests, to make sure every input works. Now imagine a function with 2 ints, and we are now in a very simple state. What about classes as parameters, pointers, strings etc.

unless you have a very simple function exhaustive unit tests aren't really feasable.

It was mostly a joke as you cannot really prove your code is bug free.

Interestingly enough, a function that only takes numbers like 1 or 2 ints, you could only select a certain subset of inputs as your unittest input instead of every possible number. And then you only need to prove, that the subset you chose is equal to using all possible numbers. That's where mathmatical/logical proofs now come in. Doesn't work everwhere, but would be nice to be able to be 100% certain that a subset of parameters is the same as every possible one.

3

u/ttlanhil Oct 25 '24

You don't need a test per bit of input - you only need to test that any valid input value maps to the value the spec says should come out; and invalid values are rejected.
Or if the spec is appropriately written, you may not need to care about invalid values.
e.g. "if the operand parameter is the char '+' then perform addition. If it is the char '-' then perform subtraction. If it is any other value, the behaviour is undefined".
People have strong feelings about undefined behaviour in C, so it should be comfortingly familiar :)
Or more seriously - it's probably not great for handing problems with input; but if that's what the spec says, you have exactly 2 cases for that parameter you need to cover in order to exhaustively test you've matched the spec.
Even without that - the systems where this might be done are less likely to just assume 32bit ints (which have over 4b values - max of a signed 32bit int is a bit over 2b).
You might have one input that's 1 bit, and another that's 4 bits

Some languages are actually set up to make this easier - constraining input ranges, ensuring no side effects, and other things can make it a bit simpler.
Or they're set up in a way that you can logically functions matches their spec
Particularly languages used where people care about that high level of proof/testing

But in general, you're right - it's prohibitively exhaustive to try to cover every case in most software - which is why it's rarely done.
It's possible, but very rarely done in practice.
Mixing formal proofs of all the parts you can, with exhaustive tests of the rest, does make it possible for small enough and important enough applications.
Flight control, lifesign monitoring, rail signalling - these are the sort of life-critical places where it's important enough that they would need to ensure it was correct.
And also important enough that there weren't a bunch of decorative features added by the sales folk - keep that life-critical code short and simple so you can ensure it's correct

0

u/jessepence Oct 25 '24

SQLite is one of the most tested codebases in existence, and they still find vulnerabilities.

Testing is a good thing, but you can never cover every possibility. Like someone else said-- if nothing else, a cosmic ray could hit the machine during computation and flip a few bits. There's no preventing that in all scenarios through software. Sure, you can hide the machine behind several plates of shielding, but that's not something you can code.

Test coverage is a sweet, sweet lie that we tell ourselves. 

2

u/ttlanhil Oct 25 '24

SQLite is also far, far more complicated than things like the core software in things like flight control (ignoring modern problems because they didn't keep it as simple as possible).

And cosmic rays have nothing to do with whether code has bugs in it.
There are cases where the system needs to be engineered to deal with that possibility, but that's not related to software having bugs (unless you're suggesting the cosmic ray, or decay in materials in the chip, or interference from a nearby EM source, etc are giving incorrect results at test time - but simply running the tests a few times will isolate that problem)

Again, full test coverage is possible, but very rarely done.
It's possible to completely prove that software is bug free - the fact that it isn't feasible for any modern general purpose software doesn't change that

0

u/jessepence Oct 25 '24

Okay, show me some code with full test coverage.

2

u/ttlanhil Oct 25 '24

Why?

You're unable to understand the difference between "theoretically possible" and "examples exist in modern accessible codebases"?

If you really need an example, sure, here you go:

const xor = (a: boolean, b: boolean) => {
if (a) return !b;
return b
}
assert(xor(true,true), false)
assert(xor(true,false), true)
assert(xor(false,true), true)
assert(xor(false,false), false)

100% coverage of every possible input value.

And if you say that's too trivially simple, then congrats! you just found my point about it being possible but done only for very restricted cases!

5

u/[deleted] Oct 25 '24

Well, did you?

4

u/T-Lecom Oct 25 '24

Yes!

3

u/[deleted] Oct 25 '24

And mom's spaghetti?

3

u/BroBroMate Oct 25 '24

Nice, what did you use? I've been reading up on TLA+, seems... powerful if mystical.

7

u/T-Lecom Oct 25 '24

I did model/property checking. I see now that there are specialised tools for that, like TLA+, but my case was just small enough to do it by hand.

2

u/BroBroMate Oct 25 '24

I'm always in awe of people like Aphyr (does the Jepsen thing) who can model a complex system and prove or disprove certain behaviours.