I spent a year in the Bay area, but moved back to Ohio for family reasons. That helped, because things are so much less expensive out here. (Bear in mind, though, you can work in the Bay Area and then retire somewhere cheaper ;)
http://www.mrmoneymustache.com/ has some great articles on how to save money while still enjoying life.
My top piece of advice is: do not buy a brand new car. Don't buy any car if you can manage it, but if you must own one, get something that's a few years old and half as expensive. And even then, try to ride a bicycle when possible.
Yes but, while working in the Bay Area you have to live in the Bay Area (assuming you aren't remote). I'm just super curious how we're supposed to save enough to be safe by 40.
* Don't spend more than about 8K on a car. Drive it till it dies. (I drove a $2140 15 year old Saturn without AC or working windows for 7 years).
* Learn how to save money while still in college.
* Read "The Idiot's Guide to Getting Rich". Ridiculous title, but plenty of sound advice throughout the book.
* Use Excel or C to figure out what your savings will be by age N if you save X/mo with whatever inflation/investment rate. You're a programmer so this should be straightforward.
* Take those results and save X/mo. Stick it into an index fund with automatic reinvestment. Set it up to withdraw X/mo from your bank account every month. This last part is probably the most important part! It forces you to live like your salary is $Salary-X, and eventually you just come to accept that.
* Monitor those numbers and watch it go up. Discover you prefer doing that to buying things. Increase X.
* Travel! Okay this is odd advice. But when you take time off work to travel a bit you really learn how to stretch every penny. I spent three years overseas sometimes working odd jobs and it was worth every second.
* Don't incur "bad" debt (duh), and pay high interest rates off ASAP. Low interest rates 3% or so you don't have to worry about so much.
* Don't play the lottery. Don't expect that there's a "trick" to building wealth. It's just a long arduous boring process of living below your means.
* Read "The Idiot's Guide to Getting Rich". Ridiculous title, but plenty of sound advice throughout the book. Yes I said it twice.
Bay area housing costs seem to make saving very difficult. I'm not sure I'll be able to retire before 40