Should you be putting money in savings or investments at the same time you’re paying off a loan?

That’s one of the most frequently-asked questions we get, and the answer isn’t always obvious. Even if you have run up a balance on a high-rate credit card, you may hear a nagging voice in your head urging you to keep plowing money into savings for retirement, college for the kids or a new home.

The simplistic solution—to invest if you can earn a higher interest rate than you’re paying on your loans—can be downright dangerous. That became clear when in the late ’90s, a wave of questionable advice suggested that homeowners actually create more debt to invest in the booming stock market —by pulling out some equity via a cash-out refinancing or home equity loan. Then came the bear market.

The best answer lies in separating good debt from bad debt. It’s almost always a good idea to get rid of credit card and other high-interest loans before you start setting aside cash. However, you probably don’t want to accelerate mortgage or student loans at the expense of saving for retirement.

Begin by making a list of all your debt and the interest rates on those debts to prioritize which ones you should pay first, says Deena Katz, president of Coral Gables, Florida financial planners Evensky, Brown and Katz. Then look at your alternatives for saving and investing and, if necessary, reset your priorities.

Step 1: Pay off the high-interest debt

If you have high-interest credit card debt, tackle that first. It doesn’t make sense to start saving or investing until you’ve paid off this debt. You’d have to make more than 20 percent after-tax return on stocks, bonds or mutual funds to make them a better investment than paying off a credit card with an interest rate above 15 percent, says Clark Randall, a financial planner with Lincoln Financial Advisors in Dallas.

There is one exception to that rule of thumb: If your employer offers a 401(k) plan and will match your contributions up to a certain level, fund it up to that level —even if you have credit card debt—because you’re getting a 100 percent return on your investment, says Randall. Contribute more than the match level once you’ve paid off your consumer debt.

If you’re drowning in debt, liquidate assets such as stocks and use your savings—but not a 401(k) or IRA—to pay off your credit cards. If you’re in dire straits, you can borrow up to 50 percent of your balance (but no more than $50,000) from a 401(k). Although you pay yourself back with interest, you give up tax-free compounding, and you will have to pay back the loan immediately if you leave your employer.

Step 2: Identify the good debt

For the most part, it’s usually not a good idea to pay off your home mortgage unless you have a lot of extra cash. After all, Uncle Sam refunds part of your interest payment if you itemize your deductions on your tax return.

Use your money instead to invest in liquid assets. However, Randall recommends paying off your mortgage (and any other debt you might have) by the time you retire so you can get by on less money.

Don’t be in a rush to pay off student loans, either. You can deduct interest on qualifying loans even if you take the standard deduction rather than itemizing deductions on your tax return.

However, you can ease the burden of repaying your loans. Thanks to recent legislation, you can now shop around for the best terms. For example, lenders may offer a rate reduction if you elect to have your loan payments automatically deducted from your bank account. And some lenders will knock more off your rate after 24 or 36 months of on-time payments.

Step 3: Save and invest

Once you’ve eliminated high-interest consumer debt, start saving as much as you can. The best place to begin is a 401(k). The next best option is an IRA.

In addition to putting money into a retirement account, you need cash that’s readily available in an emergency so you don’t have to rely on credit cards. (If you are paying down your credit card balances and still paying high rates, it is probably better to keep paying off the cards and borrow from them in case of an emergency, says Katz.)

Set aside enough money to tide you over for three months if your paycheck suddenly stopped. If you have less-than-steady income, such as from a commissioned sales position, or a job that has more exposure to economic fluctuations, consider setting aside six months of income.

Sock it away in a high-yield savings account or money market fund on a monthly basis until you reach your desired amount.