During my last project, I used to have the same problem. I was thinking about the code during my commutes to home, before going to sleep and even as I was alone in the room with my girlfriend.
That's when I knew I had to stop.
I have pretty much figured it out now, and here's my advice to you.
Get Positive
First, accept that you can't just stop thinking about the work out of the blue. This is a habit you have acquired, and habits don't disappear at your immediate will.
However you can start by reshaping your experience into a positive one.
Consider this example of negative thinking:
I don't know which database design is better. The deadline is coming next week and I feel I can't make the right decision at the moment. We can't afford to hesitate and If I fail now, I will be the one to blame.
This leads you nowhere.
It may sound trivial but if you suspect you don't work well enough, or worse, procrastinate, your conscience will revenge on you. It's five times as hard to think about work when you feel like you fail at it.
If you're in this kind of situation, there is something you can do about it right now.
Essentially, there are two points to add to your workflow:
- Make sure you have something to be proud of every evening.
- Make sure you have something you strive to work on every morning.
The ultimate goal of this is to switch your obsession to a positive tone. You know the feeling when you go to sleep thinking about that awesome code you wrote in just about three hours that solved all the world's problems and made the bunnies happy? You're still obsessed but now you've made a major and very important shift.
Get Productive
Once you're in the positive stream, you'll find it easier to effectively constrain your tasks to the working hours. Try to plan them in such way that thinking about the problems in your free time doesn't add any value. Consider this example of positive thinking:
This database design problem is an interesting challenge and I'll try my best to solve it. I know I'm usually more productive in the afternoon so I'll just have some tea now and fix a couple of bugs so I can give it my full attention when I'm at my best. Before leaving, I'll evaluate my results, and if I don't make a considerable progress, next morning I will ask for some advice from the more experienced colleagues and post a question on StackOverflow as well. I'll make the final decision by tomorrow evening.
What has changed? Now you pick your challenges and organize your working time in a way that makes sense to you. These eight hours are not just eight hours in your life, they are special, and you need to take advantage of them. Specifically, you need to:
- Turn these eight hours into the Perfect Time™ for problem solving.
- Make sure that physically being in the office empowers you.
The second point is a kind of trick you can play on yourself. Ask your company to provide you with the best hardware. Do you have three monitors yet? I keep my favorite teacup at work, and I just love my armchair. I'd never want to solve a problem without it again. Okay, I made up the armchair thingie but the point is:
If you learn how to get really productive at the office, you will see that the habit of bringing work home eventually fades because nothing really justifies it anymore.
Get a Life!
There is a great answer by pydave that does a better job of suggesting after-job activities.
You must check it out.
No, seriously.
Thinking about a database late night? How about going to a club instead? Watching a movie?
If you're not the type of person to know how to spend time, ask your friends to take you out.
I can't possibly remember how many times I was initially resistant to my friends calling me somewhere and then realized what a great time it had been and how I could've easily missed it out of the passivity. Now, even when work-related thoughts are buzzing in my head, if somebody calls me in my spare time, I just say “I'm in!” and get going.
A great relief comes when you realize you're still going to do what you love the next morning and there isn't a single reason to think about it right now. So go ahead and find something else to muse on!
Even when you're in love, after some time, you stop thinking about your significant other every single hour. This would have exhausted you. Instead, you split your free hours so there is a time for her (or him), there is a time for your friends and there is a time for you to be alone. There is absolutely no reason why you shouldn't apply the same principle to work.
Divide and conquer!