Good point. I always pinned it down to people lacking self confidence. I assume programming can be pretty scary when your bulk experience has been non-stem.
The number one tip I always give is to write down everything you think off when you get off topic and start drifting around. I don't know what's happening neurologically, but it trains you to snap back once your train of focus breaks, it might not work for everyone, but I got pretty good results. It's also helped me with forgetfulness.
I use apple's native note application, but evernote looks about the same or better.