> I discovered that after a certain level of fatigue my coding became garbage, and after a night's sleep I had to delete it and redo it.

My best work happens at 2am, at about 4am I am too tired and get slow and get stuck, I think even then code quality suffers only a little bit.

That's just my experience, I believe it happens because if I am working at that time, I am hyped and or in the zone. There is a sort of second wind involved. The lack of distractions also helps I guess.

Exactly the same here, late night would be my most productive. I don’t know how sustainable this is as I mature lol.