Age discrimination isn't about competence (programmers peak much later than 40) but control. It exists because the VC man-child oligarchy (oddly enough, software inherits the age-discrimination culture from business-culture colonists who'd be "too old" to write code) thinks that people will work harder if this shitty, artificial time pressure is put on them. And, of course, much of the ageism and sexism is unintentional but still exclusionary. See: open-plan offices and the aggressive micromanagement of Scrum. I don't think Scrum and open-plan were devised with ageism in mind, but they contribute massively to the attrition. As people get older, they have less tolerance for macho-subordinate bullshit.
This macho-subordinate culture that is imposed on software engineers needs to die. It drives out an incredible amount of talent, makes us an HR nightmare as far as diversity goes, and it has nothing to do with programming; in fact, it detracts from the quality of what is produced. We have to kill it at any cost and professionalize our industry because we can't trust our financial backers.