Perhaps the biggest lesson here: never give someone a gut-level estimate of anything. The challenge is that a lot of clients/bosses/whatever will press you for one. Some people just need to hear a spot-estimate in order to feel comfortable.
In those cases, I've found a fairly effective verbal counter. It goes something like this: "Listen, Bob [assuming the client's name is Bob]. I don't want to mislead you here. I'd rather work up a proper estimate than throw out some number on the spot that might be incorrect. I want to do this the right way, for your benefit and mine."
If Bob presses you further, respond with "I've worked on similar projects in the past. Some have taken 2 days, and some have taken 10 days. It really depends on X, Y, and Z. I want to take a proper accounting of X, Y, and Z in this case, so that I'm not misleading you up front."
If pressed further: "Bob, obviously time is of the essence for you and for this project. I understand that, and I will do everything I can to ensure that I complete the project as quickly as possible. In the end, this will be much quicker if we get it right the first time. Give me a little bit of time to do some planning, and it'll save us a lot of time in the long run."
All of this is true, of course. It's better to aim twice and shoot once, as the saying goes. However, there will be clients or bosses who just won't accept this sort of conversation up front. It'll piss them off. So be it. FAR better to have a little bit of confrontation at the outset than a lot of confrontation later on (i.e., if you toss out a gut estimate and then have to crawl back and amend or delay it).
Which is basically the time-honored "Underpromise, Overdeliver" (UPOD) method. UPOD is pretty reliable in a corporate setting, but it can be tricky in a client-contractor relationship. If you're a contractor, and you have reason to suspect that you're being vetted against other contractors, underpromising to too great an extent can work against you. So it becomes a delicate dance between promising enough to win the bid, but not promising so much that you set yourself up for failure.
"Awesome. We'll have that Perl-> Rails port with MySQL -> Cassandra conversion and 1 million paid accounts migrated in at most 12 weeks, ready to handle 5 million new accounts the day of our grand re-launch? Great! Then I don't have to worry about finding a SDET or QAE or Datacenter admin to help parallelize the work and hit our release date. Marketing has the press conferencne all planned, we just needed the word from you before we filled in the Octo..Nove..Decem..January 24 launch date! Thanks!"
"If you ask Fred, “When will you be done?” have you asked for an estimate or a commitment? What does Fred think you asked for? If Fred says, “Two weeks from today,” has he given an estimate or a commitment? What might happen if you want a commitment and Fred thinks you want an estimate? What might happen if you want an estimate and Fred thinks you want a commitment? How could you make it crystal clear whether you’re asking for an estimate or for a commitment? How could you make it crystal clear whether Fred is giving an estimate or a commitment?"
I agree. That is also why some processes (i.e. Scrum) force you to make an explicit distinction between "estimations" and "commitments" (and that still does not keep some people from forgetting the difference).
Lt. Commander Geordi La Forge: Look, Mr. Scott, I'd love to explain everything to you, but the Captain wants this spectrographic analysis done by 1300 hours.
Scotty: Do you mind a little advice? Starfleet captains are like children. They want everything right now and they want it their way. But the secret is to give them only what they need, not what they want.
Lt. Commander Geordi La Forge: Yeah, well, I told the Captain I'd have this analysis done in an hour.
Scotty: How long will it really take?
Lt. Commander Geordi La Forge: An hour!
Scotty: Oh, you didn't tell him how long it would really take, did ya?
Lt. Commander Geordi La Forge: Well, of course I did.
Scotty: Oh, laddie. You've got a lot to learn if you want people to think of you as a miracle worker.