But a 40 year old programmer also can do different stuff better than a new graduate. I mostly do financial software and I have to say that while occasionally we have had recent graduates contribute (and contribute a lot) on the whole most of us who do it for a living are older, have kids of our own, and work hard at it. Many of us are self-employed because we don't *want* to work a corporate job. A few are self-employed out of necessity (things like age discrimination).
The thing is, programming knowledge itself only allows you to solve pure programming problems. Solving anything else requires domain knowledge and that increases with age. Additionally, I find that older programmers tend to be better at smelling where context is potentially problematic and slowing down before a big mess is created.
The problem is actually a lack of sense of history. As Harry Spencer said, those who do not understand UNIX are destined to reinvent it badly. Older and wiser programmers who do understand the solutions that have come before are better equipped to solve tomorrow's problems than the just-graduated hot-shots. But the companies don't know that and so they miss out.