It's about prevention and lifestyle choices.
By applying ideas from performance psychology and wellness, and by learning to think about work a little differently in general, I believe it's possible to make huge strides in your stress level and your productivity.
I take a 5 to 10 minute break every hour. Yes, that's a good chunk of my day and on a large scale it adds up to countless lost man-hours. But you can't consistently go full blast 8+ hours straight, it's just not sustainable.
Perhaps counter-intuitively, the breaks make me more creative and productive. There are countless benefits. Even just taking an hourly walk to the water cooler could make an impact.
These are not popular ideas in most corporate cultures, but pay attention to yourself. Do you feel like a zombie? Are you having trouble focusing? Have you been banging your head against a single inane problem for over an hour?
That's what happens to me when I don't take breaks. My experience is that once you're past the point of crashing, it takes about 20 minutes of non-activity to recover. You could try to cure this malady with the ole' programming fluid (that's coffee, son) but that's not a sustainable way to do things either. Stimulants are for emergencies only.
The most shocking discovery I've had here is that most developers are not aware that they crash in this way, even when it's obvious to observers. So be aware of yourself and your state. Learn to prevent the crash.
One last thing here: the worst thing I did to myself as a developer is the destruction of my eyes. I completely screwed them up and it was preventable. Computer vision syndrome is a serious issue and all knowledge workers should take steps to avoid it. Frequent breaks is one of the best things you can do to save your eyes.
I used to play competitive chess. I would go to a hotel across the state and spend the weekend playing five games of chess that lasted up to six hours each.
This is intense physical exertion. If you've never done it, you have no idea. In my last tournament, I showed up thinking it was a class event where I could play against people of my own level. I was way under-rated and I had been training hard, so I expected to dominate. Well, as it turns out, it was actually an open tournament and I ended up playing against masters and senior masters.
I poured everything I had into those games and I did okay. I surprised everyone and upset a strong player who underestimated me, but then I was so burned out that I spent the rest of the weekened getting mentally pounded. It was brutal.
Afterwords I got the flu and was sick for a week.
How is that possible? Well, as you know, mental stress has a huge physical component and in this case I pushed myself way too far without preparing for it correctly. Top level chess players train their bodies (cardio, etc) constantly to sustain the strain of these events, which are even more of a grind at their level.
I'm not suggesting that you start training like an elite athlete to write code, but there are some tricks that will help. Consider checking out Josh Waitzkin's book, The Art of Learning - he's a fascinating guy because he went to the highest levels in chess and then switched disciplines and is now a world class martial artist. Not only is his book entertaining, it presents an informed perspective on the psychology and challenges of excellence that are common to all fields. And that includes software.
Waitzkin gives tips like this: After studying at a performance psychology institute in Orlando, he learned that if he starts to lose focus in a chess match, he can go outside and sprint 50 yards. After that, he walks back in completely refreshed every time.
Is that an extreme thing to do? Well, yes. Especially if you happen to be wearing a suit. But these things are situational, and ideas like this have helped me a lot, partially because I have a lot of control over my work environment.
I don't play serious chess any more. I went through a phase where I wiped out all the things in my life that were holding me back professionally and personally, and the intense battle of wills that is chess was the first one on the block.
All that mental work at the board was stealing from my ability to be my best elsewhere in my life, to show up for the people who needed me. I was out of balance because my whole life was built around my mind.
So, create a life that lets you use more than just your ability to calculate. Set some boundaries and learn that it's okay to stop coding at midnight. :) Not only does that make you a more interesting and adaptable person, it keeps you sharp, balanced, and resilient.
These days I practice kung fu, which is fascinating and primal. I also do yoga (actually, I ended up marrying a yoga instructor!) and I meditate an hour a day, which has been huge for me. I'm in touch with my body, which is a big deal that many of us cerebral types miss out on.
To wrap this very long answer up, I feel that I owe a lot to these ideas and I sincerely hope that some of you will find value in them. Thanks for reading!