I believe there are many great developers who refuse other positions and still code in their 50s, but an average developer in his 30s could be better than an average developer in his 50s, because many successful developers might transition into higher paid leadership roles or start their own companies when they hit 40s. For instance, Joel Spolsky was still writing software in his mid-30s.
I assume he was better than an average programmer, since he was responsible for designing Excel Basic at Microsoft. I think the very best programmers keep coding in their 40s and 50s, because there is simply nothing they could be better at. It's the "upper-middle class" of programmers, who often transition into new roles at around 40, because they no longer feel the progress in their careers. How many years can one spend being a Senior Engineer at Google?
I know of senior engineers at Google in their late sixties, and I’m sure there are folks in their seventies there. Some of them are top people in their fields. Donald Knuth is 81 (10000 in ternary) and he still codes for research all the time. My understanding is that Spolsky’s job at Microsoft, which was his first job, was to spec out the then nascent macro language for Excel, he wasn’t a programmer himself, perhaps ever professionally. He does have a lot of opinions on what a great programmer should look like, and isn’t too shy to share them.