Once upon a time, I studied chemistry. When you mix clear liquids that all look like each other and their exact temperature and precise quantity directly impacts results, you quickly learn the value of laboratory notes. So that's what I do with programming now: I do my thinking on paper.
I write what I want do to, why I want to do it and my hypothesis on how I want to solve it. I call each of these my "experiments". I then chronologically describe what I am doing, referring to the sections mentioned above to keep myself on track. If new ideas come, I write them down but pursue them only if they are aligned enough with the goal, otherwise they are postponed.
Sometimes, you discover that your goal is bad or that some prerequisite is necessary. I then stop or cancel the experiment and start a new one. By doing so, I get a lot of structure in the way I do things. The produced programs usually reflect that.
Writing down the encountered problems and having proof black on white of what I already have tried and if it was successful is worth its digital weight in gold as well. If only for the emotional relief of venting on a page along with the problem's description.
Also later on, when an issue with the program pops up, I can usually go back to why things are the way they are. The notes read mostly like the ramblings of a crazy person, but they prove terribly useful to me nonetheless. Especially now with LLMs able to summarize and link to them in a couple seconds. I don't know if it's for everyone, but I recommend trying it out. It made my life much simpler.