Aside: Professional correspondence
Update: A few readers have asked if I have general recommendations on writing professional emails and correspondence.
I have an article on how to write an email.
For professional correspondence, I keep a copy of Business Notes by Florence Isaacs on my desk:
It's a valuable reference.
Do not wait until right before the deadline. Ask at least three weeks in advance, and perhaps six.
Don't be shy
Do not be shy about asking for letters.
Remember that everyone you ask for a letter once had to ask for letters themselves. We remember what it was like.
I hated asking for letters because it seemed like such an imposition, and I hate to impose. So, I'd often end up waiting to ask.
Sitting on the other side of the desk, I realize that writing letters is something we sign up for with the job. Asking a professor to write a letter is asking a professor to do his or her job.
Personally, I don't mind writing letters, and for good students, I enjoy it.
Pick your letter-writers well
If you're applying to grad school, you should seek letters from strong names in the field in which you wish to do research.
You can get a sense of strength from recent publications: Are they publishing frequently and/or recently in top venues for their field?
If you're applying to a job, the names are almost certainly going to be meaningless to employers, so you'll want letters from the best writers.
Writing quality and lecturing ability are roughly correlated.
Don't get n letters
Too often, students need n letter-writers, so they ask any n previous professors for a letter.
This will get a student an "nth" letter.
Professors don't mind writing these letters, because they take about ten seconds. But, deep down, we know we're doing the student a disservice.
We have templates where we drop in the applicant's name and grade, and coded pleasantries signalling that we have no substantial knowledge of the applicant other than their grade.
These letters won't hurt you, but they won't distinguish you.
Your letters should not rehearse your transcript in long form.
For applications to grad school, if all letters read like this, it guarantees rejection. The message is: "This is a student that did well, but never took the time to go above and beyond."
How to get a good letter
The more I know about an applicant, the better the letter.
When you take a class from someone that you want to be a letter-writer, distinguish yourself with your level of effort, your helpfulness to other students, your interaction inside and outside the classroom, and your enthusiasm for the subject.
At a minimum, provide a recommender with some context: a capsule summary of your interests/accomplishments, your resume/CV, any application materials you are submitting and a list of places to which you plan to apply.
How to get a great letter
If you want a great letter, do independent study or research with the letter-writer for a semester or two before asking for the letter.
Those one-on-ones will give the letter-writer valuable insight into your character, and if the projects succeed (yielding a publication), then your letter-writer will be able to write an enthusiastic endorsement.
Research is always risky, so if you want a publication with your letter-writer, start working with them at least a year (and probably two) in advance.
Beyond netting a great letter, doing one-on-one work grants indirect access to the professor's network of personal contacts at other universities.
How to ask for a letter
There is a debate about whether you should ask for a letter in person or over email. Personally, I don't think it matters that much.
Whether over email or in person, what you should ask is, "Would you be comfortable writing me a strong letter of recommendation?"
Almost no professor will turn down a request for a letter, but if you ask for a strong letter, you might hear back, "I don't think I can write the strongest letter--who else are you asking to write letters?"
When I turn down such requests, I then help the student find who their strongest letter writers would be.
Over email, include a brief capsule summary of interests and accomplishements, as well as a link to your resume/CV and application materials.
If a letter must be mailed, include a pre-addressed, pre-stamped envelope and any other materials required.
How to follow up
Professors generally won't forget to write a letter, but we are never annoyed by a brief reminder (or two) from the applicant.
Do send reminder emails a week before each deadline.
Lately, I've asked applicants to create a shared google spreadsheet with organizations and deadlines that I can update when I submit letters. Feel free to offer this up front.
This will give you peace of mind, in that you can verify whether or not a professor has submitted a letter. It also gives you a polite way to ask a follow-up: "I see that you haven't checked off the spreadsheet for X and it's due in three days. Did you forget to check that off?"
It's been a while, can I still ask?
[Added in response to questions on HN.]
If you've graduated and gone to industry, you can still ask for a letter.
When you email the professor, include a short, bulleted update with what you've done since graduating.
Emphasize how the specific techniques learned in their class or in independent study have been invaluable in your career. Include examples.
If you don't hear back, don't give up. Email again after a week.
Most professors have hopelessly swollen inboxes, and expect to be re-emailed with anything critical.
Should I draft my own letter?
[Added in response to questions on HN.]
If the professor requests a draft, yes.
Do not offer to write the letter up front, but if you know the professor is particularly busy, you can append "Please let me know if there's anything else I can do to help you draft the letter" to your request.
Typically, what the professor is looking for is a list of bullet points about interests and accomplishments that they can massage into a letter.
To expedite the letter, be sure to ask in what format they want the draft, e.g., Word, LaTeX or plaintext.