Historically, barn doors were used on the very hot tungsten lighting that we had to use years ago, they were pretty much the only modifier that wouldn't catch fire with the old redhead and blond lights. That's what made them popular.
Yes, they block light from going to places you don't want it to go. A sheet of cinefoil or black card, used as a flag does that too. In fact, it does it better because it can be much bigger and you have the choice of having either soft or hard edges, which you don't have with a barndoor set.
And yes, you can put the flag next to the light, or anywhere else. A flag is more versatile as well as more effective, in most situations, most of the time.
You'll see from
this thread that one manufacturer makes them with doors in a choice of sizes, I don't know how effective they are but a choice is good, although whether you think it worth the cost or not may be another matter.