As ever, there's always more than one way to skin this cat.
Another factor to consider in the early game is that you are probably researching at full stretch, so you are not saving cash. If a roaming, restless or raging barb does raid a city there won't be much gold to steal, so the worst it can do is destroy the shields you have invested into the current unit you are building. If that's likely to be less shields than it costs for the defender then you are better off letting them raid than building the defender. Harsh economic facts of civ-life.
At higher difficulty levels there's also no guarantee that a warrior defending your city will survive a barb attack, so you could lose the defender AND the work in progress anyway.
[edit]I've just read a neat trick in a DaveMcW post in one of the Tournament threads. If you maintain a minimum +1 gpt income then barbs will only steal your gold (as long as there's only one attack per turn), and therefore you protect your work in progress on units and improvements. This makes it even more cost effective to avoid building early defenders
Thanks DaveMcW.
A higher priority early defence task against barbs is to keep them from pillaging your nice new fields and roads and mines and killing your valuable work-force and troops outside town. These things take time and effort to replace and you can lose a lot of production capacity while doing so.
As a city and your civ gets bigger there comes a point where it needs a unit or two as military police to reduce the unhappiness factor without spending an arm and a leg on entertaining the population, and that can become a significant motivation for putting defenders in the city. Any cheap military unit will do for this purpose, of course, and I often have to ask myself why I've put an expensive vet spearman in an inner core city when he could be replaced by an old regular warrior on police duty and go to the front line to defend the empire properly. When you have a cluster of cities the capital should rely on the civ frontiers for defence. Unless you are a masochist, by the time you are fighting hand to hand inside your own capital you are probably not going to enjoy the rest of the game.
Of course, if there is a big, ugly bully living right across the street early on then the calculation changes, as he'll take your cities away from you, not just raid them for gold and shields. A strong military will help to deter him from trying, but put the units inside you cities only if that happens to be the best tactical place for them anyway.