Unfortunately, in computer programming you will run out of experience and depth that you can accumulate after about 8-10 years of work (say ages 22 - 32). Every new technology is just a variant of the previous 999 technologies. But you still keep getting older every year, and incurring the downsides of getting older!
Compare this with, say, being a doctor. A doctor at 32 is just finishing residency, so he or she has just finished formal education. At 42, she has 10 years of experience, and a doctor with 10 years of experience is better than one fresh out of school. The doctor keeps getting better at 52 (20 years of experience) and 62 (30 years of experience).
The big picture is that computer programming is a field with low barriers to entry and where young people have a significant advantage. Staying in a field like that for the long run is going to lead to a brutish brutish existence. There are many posts here along the lines of "I'm 39 and still doing ok". That's beside the point, because in the future you will be 49, then 57, then 65. Remember, Social Security full retirement age is 67.
It doesn't make sense to wait until you are 40 to switch. Every career field accepts young people easier than old people, which means that the time to start planning your switch is today.