Do they use money on Star Trek or not?

Sometimes I think the main pleasure that Star Trek affords is the game of explaining the horrendous logical problems that crop up so often.

Many people have observed an inconsistency in the attitude of Star Trek characters towards money. In episodes of both the original and the more recent series, the characters often describe the Federation as if it were a perfect socialist, or at any rate post-capitalist society, where there is no money and nobody wants for material things. In the movie Star Trek IV: The Voyage Home, Captain Kirk says that they don't use money in the future. In the movie Star Trek: First Contact, Captain Picard says that in the future no one is motivated by the desire for material wealth. And so on.

But at other times, capitalism seems alive and well in the Star Trek universe. There are money-grubbing space traders like Harry Mudd. The Enterprise crewmembers sometimes spend "credits.” The inhabitants of Deep Space Nine gamble in a casino, winning and losing bars of precious latinum. So what gives?

In this case I think there is a logical explanation for the apparent inconsistency. Perhaps the Federation proper is a perfect socialist or post-capitalist society, where there is no money. But aliens outside the Federation, and quasi-outlaws like Harry Mudd, continue to operate in a capitalist manner. Starfleet personnel, out on the fringes of the Federation, are understandably in a gray area. It may be useful for them to accumulate "credits" and "bars of latinum" to trade with people who care for such things, even if money wouldn't be of any use back home on Earth.

I think this fix works pretty well, because arguably almost all we see of the Federation are the crews of starships and starbases and scientific outposts on the wild frontier. We see little or nothing of ordinary people living their lives in the heavily-populated core worlds of the Federation. Kirk and Picard may be thinking primarily of these people when bragging about how their society has evolved beyond the need for money.