I'm 40 too and haven't felt more sharp in coding anytime before. However I started to code in my early 30s so maybe it contributes here somehow. It sounds like the task you have is a misfit for you or the code you have been given is way worse than you think.