If you are asking a professor for a letter, it is best to ask him/her after you have finished taking his/her class. Ask him/her the following quarter or semester. You want to make sure that your professor remembers you when he or she writes your letter. If you took the class much earlier than the year you were planning to apply, you can store the letter using a letter service. Most schools provide letter services that store your letters so that you can use them whenever you need to. Another good letter service is Interfolio.
Related Articles:
Letters of Recommendation for Medical School || How to Ask for a Letter of Recommendation