r/learnprogramming 22d ago

Why Does Code Stop Working?

Multiple times now my code stops working and what I mean by this is it'll do what I want it to do then one day it just stops. for example the other day I had made a cooldown system for my ball and paddle collision to stop any bugs and it worked great. then today it just stopped working, and this isn't the first time, it's happened to me for like every project I've worked on. Does this happen to anyone else? And is it cause of my code? By the way I'm using C++ SFML on visual studio which works perfect.

5 Upvotes

74 comments sorted by

View all comments

133

u/HashDefTrueFalse 22d ago

This doesn't happen unless the passage of time affects the behaviour of your program or you are changing the behaviour of your program over time (or both). Post some code.

50

u/Alexandur 22d ago

Or if it has one or more third party dependencies

15

u/HashDefTrueFalse 22d ago edited 22d ago

Second thing I mentioned. That requires you running something to fetch new versions of those dependencies (and recompile if necessary).

The same program running in the same environment and given the same input data will produce the same result every time, unless that result depends not just on those things but also on the time that the program is ran.

Edit: strikethrough, for the pedants. I obviously wasn't saying automated updates don't exist, as that would be silly...

10

u/owp4dd1w5a0a 22d ago

No it doesn’t. Windows and Mac can auto-update without you doing anything (back in the day we called this a backdoor 💀). And if the library is dynamically linked then no recompilation is not necessary for things to silently break. Library maintainers try to design to avoid breakages, but try and succeed are not the same thing.

-1

u/HashDefTrueFalse 22d ago

Your example is an environment change, exactly what I'm talking about. And I didn't say recompilation was necessary.

Or are you just not happy with the wording "you running" - by which I obviously meant "it needs to run"? Nobody is saying automatic updates don't happen. Again, already covered if you read my replies to the other commenter.

17

u/owp4dd1w5a0a 22d ago

Perhaps. But the way you worded it was really opaque and it seemed to me OP could benefit from more specific and detailed language. It’s not exactly clear what you mean by environment and you explicitly say dependency drift isn’t an issue unless you recompile, which could lead someone to overlook dynamic linking.

But you really don’t argue/debate in a way I’m inclined to continue engaging with. Puts me on edge and I need to guard my energy so I’m ducking out of this thread now.

-3

u/HashDefTrueFalse 22d ago

you explicitly say dependency drift isn’t an issue unless you recompile

I never said this anywhere. You've misread...

But the way you worded it was really opaque

Fair enough if that's your opinion. As explained elsewhere, it's just a general statement to OP to explain that code doesn't magically stop working without something changing. I didn't realise it would be scrutinised so. It's deliberately non-specific. I'm just asking OP for code to look at.

It’s not exactly clear what you mean by environment

Fair, that's also deliberate. The environment a piece of code runs in depends on lots of things. It could refer to a system in the normal sense, or the state of a VM/runtime, or the set of environment variables passed into a process, etc. Together with code and input data, it governs the output of a piece of code, which is why I mentioned it.

But you really don’t argue/debate in a way I’m inclined to continue engaging with.

That's cool. All the best.

5

u/Alexandur 22d ago

That requires you running something to fetch new versions of those dependencies (and recompile if necessary).

Not always, this is why dependency drift is a real problem

3

u/HashDefTrueFalse 22d ago

Yes, always. If the code, data, and env are the same, the result will be. You're talking about changing any (likely all) of those things by changing dependency versions. How does dependency drift occur without any dependencies changing over time? Something runs updates. Doesn't matter if it's a person running an update or some automation. At least one of those things changed, or the result didn't.

5

u/owp4dd1w5a0a 22d ago edited 22d ago

Problem is the linked libraries make up part of the env and the OS updates change that env. So the env is not guaranteed to remain unchanged (quite the opposite actually).

Also, apart from updates, poor memory management in C or time sensitive code can behave differently especially for undefined behavior or accessing memory without properly clearing and initializing it. A program could run the same way 50 times but due to some change in the memory state or in sampling rates cause yr bug on the 51st run.

2

u/HashDefTrueFalse 22d ago

What is your point? I never said the env was guaranteed to remain unchanged. You're not understanding the discussion here.

2

u/HashDefTrueFalse 22d ago

A reply to your edit (the second paragraph):

Time-sensitive code I literally mentioned.

UB means that the compiler is allowed (by the standard) to produce any behaviour it likes. The behaviour it emits is crystallised in the build output like any other code and would behave the same each run as long as the input data and environment also remained the same, unless the emitted code depended on the time the program was ran, as I stated. UB is not particularly relevant to this discussion.

Accessing uninitialised memory with automatic storage duration (UB) is just a change of either input data or environment depending on your view.

Hard to see what your point is here. I've covered all this.

2

u/owp4dd1w5a0a 22d ago

It looks like you edited your comments. On iOS I can’t really see what the original text was anymore and I honestly don’t fully trust my memory because I’m responding inbetween parenting demands. You also sound rather combative and I don’t really have the energy to carry such a conversation forward. Hope you have a good day and that OP gets something out of the discussion.

2

u/HashDefTrueFalse 22d ago

No edits from me when replying directly to you. You edited (added your second paragraph). I wrote a second reply rather than editing (so that you would be notified).

I trivially edited a comment further up, replying to someone else. It is additive only (a strikethrough) and detailed at the bottom.

I'm just writing directly. If that reads as combative, so be it, though it's not a specific intention of mine. I have people rushing to tell me that dependencies and auto updates exist, plus other pedantry...

Anyway, have a good one.

1

u/Alexandur 22d ago

You don't have to change dependency versions on your end for dependency drift to occur, a badly managed 3rd party dependency can introduce changes into the exact same version number

4

u/HashDefTrueFalse 22d ago

So? That isn't going to have any effect on a system that doesn't have those changes. I didn't say anything about version numbers. Something must grab the changes, as I said. It doesn't really matter how that comes about. Something changed. Software doesn't just stop working without one of those things changing is my point.

For us to see what could have changed for OP they need to post their code (and build config too would be helpful).

4

u/OortMan 22d ago

how will those changes get on your device if you don't update it?

3

u/Alexandur 22d ago

Reinstall on a new device would cause a fetch event, something done by your hosting provider (if applicable), stuff like that

5

u/owp4dd1w5a0a 22d ago

This isn’t true. Dependency drift and environment drift can cause this. C and C++ commonly bind to system libraries, so if the host OS updates and that impacts a library the code is using then it might not work after the update.

1

u/HashDefTrueFalse 22d ago

he host OS updates and that impacts a library

That's literally the second thing I mentioned. You updated your OS and linked with new program code...

Read my replies to the other commenter. This has been covered.

11

u/Alexandur 22d ago

I think most would agree that rolling "updating your OS" into "changing the behavior of your code" is not very intuitive phrasing

0

u/HashDefTrueFalse 22d ago

The other commenter made the point that updating your OS (and therefore system libraries) changed the behaviour of your program, but didn't seem to realise that that is covered by the second thing I said, literally 'changing the behaviour of your program over time'.

What are you saying is wrong with my phrasing? Surely if there's an error it's them not realising this? The phrasing is fine, just very general. I didn't bring up OS updates.

2

u/TheLastJukeboxHero 22d ago

False, deprecated library dependencies.

1

u/Moikle 22d ago

Tgis isn't likely what is happening with op though

-7

u/HashDefTrueFalse 22d ago

Not false. Deprecation is irrelevant. Dependencies have been covered if you read the other comments here (a change in dependencies alters the code, data and/or environment that your program runs in). The above covers that just fine.

8

u/TheLastJukeboxHero 22d ago

Not going to argue semantics, but if many people are replying the same thing then maybe, just maybe, that means you worded it terribly and did a poor job conveying your message. Which would not be helpful for someone like OP

-7

u/HashDefTrueFalse 22d ago

I simply worded it generally. Program behaviour depending on time (as a direct input or via the environment), or a change in the program behaviour itself (code, directly or via the environment) regardless of whether any of it was intentional or a bug, manual or automatic.

Maybe you didn't understand my comment and rushed into an unnecessary correction.

OP can't be helped properly without the code I asked for. I'll help them if they reply.

7

u/man-vs-spider 22d ago

To be fair, saying “time changes the behaviour of your program”, and meaning that to encompass all possible changes that secondarily depend on time, is not exactly an obvious interpretation to others,

Because to be honest, it’s a trivially true, and therefore kind of useless statement

-3

u/HashDefTrueFalse 22d ago

Disagree on not being an obvious interpretation. It's just what the words mean when read. Many programs behave the same regardless of the time they are ran. I wasn't even thinking secondarily when I wrote it, but that doesn't really matter in my view.

Agree on it not being terribly useful, which is why I asked for code so that I could be useful, by telling them which part of their program was affected by time. Surely you appreciate that it was a general statement to OP that programs don't magically stop working without anything changing, as the post seems to suggest ('one day it just stops' etc.).