Every year I get many requests from students asking me for recommendation letters. This page contains my advice to you, based on my experience as someone who has written letters for students over the years, as well as read letters from prospective graduate students.
The first thing you should realize is that if the only thing I know about you is your performance in my class, my letter is likely going to look like the following:
"I know X because s/he took my class C. X's performance in the class was [insert words here], and his/her rank was Z. Overall, I believe X will do [insert words here] in a graduate program. I [insert words here] recommend him/her to you."
This sort of letter is useless, and does not count for anything. Everything I've said in the letter can be found (more or less) in your transcript. Also note that giving me a copy of your statement of purpose will not change this letter in any way. Even if you have done an outstanding project in another class or a project with another professor, I will not comment on it unless I have some personal knowledge about it. Any information of this sort will be in your statement, and so I am not going to repeat it. If you have taken my class but you stood out in some way that is not clear from your grade, then I can explain that in the letter. This is not easy if the class is large, but it has happened on a few occasions (few = less than 1% of students I've taught). If the class you took was small and/or had a final project that was open-ended, that would be something I could potentially comment on.
A strong letter is one where I can say specific things about your performance potential. An example of such a letter is:
"I know X because s/he took my class C. X received an A- in my course and was ranked 24th out of 90 students. Toward the end of the semester, I was approached by X who expressed interest in studying the predictability of company profits using machine learning, as well as the subsequent effect of such predictions on stock price. X had many ideas about the subject, and after an initial discussion, came back a month later with a proposed research design. Since then, s/he has performed multiple data analyses and interpreted the results in light of his/her initial predictions. We are in the process of preparing a paper on the subject that will be submitted to Z. I think X is an outstanding candidate for any graduate program/job. I would perform additional research with him/her without hesitation. I give X my highest recommendation."
This letter is strong because it contains information about the student's creativity, research potential, independence, initiative, and drive. It also says I was sufficiently impressed to work with the student even though the student did not do that exceptionally well in my class. Ideally, all your letters would read like this one. To have a good chance of admission to a top graduate program or job, you should have at least one letter of this type.
Doing a project with me but not doing a good job is probably worse than my not knowing anything about you. Even if you couldn't do a good job because you were too busy taking classes, it shows that working with me was not a priority for you.
In summary, I like to help students by writing them letters of recommendation. However, in order for my letter to be meaningful, I need to know more about you than I can learn from regular classroom interaction. You don't have to do a research project with me, but I do need more observations about you than I can obtain in the classroom.