Why would you want to go into a room? The DM has probably filled it with lots of traps to get you the moment you go in, and monsters are waiting to surround you at a moment’s notice.
Far better to stay in the doorway, adopt a defensive posture, and let your spellcasters and archers rain down destruction on the monsters while the fighters in front protect the rest of the party. If the monsters close to melee, great!
The barbarian that runs unheeding into every melee is doing the party a disservice. What they’re doing is leaving the middle ranks of the party vulnerable to attack. A smart (or evil) DM will bypass the barbarian and move to attack the wizard and rogue the barbarian was meant to be protecting. Or the barbarian, alone in the centre of the room, will be surrounded by monsters and overwhelmed.
The smart move is to stay back. Use the doorway to prevent the party from being attacked from several angles and pick off the enemy.
Does this always work? No! D&D would be a very boring game if one strategy worked every time. It works in most circumstances because many common monsters in D&D only have melee attacks or relatively weak ranged attacks. The times when it doesn’t work are when the enemy has strong ranged attacks or area effect spells. At that point, you need to try other tactics.
And one of those tactics is closing the door!
Not every encounter needs to be defeated the moment you meet it. Some will still be there when you come back, better prepared, for the next time. Of course, the DM may do preparations as well, but some divination spells can help foil that.
Perhaps you can let the monsters chase you and set your ambush. Perhaps you can sprinkle caltrops or flaming oil to deter pursuit. Or drop some gold coins – goblins and kobolds might just pick them up.
And then there are the times when you need to move quickly into the room to engage the archers and spellcasters, spreading out, so a fireball spell doesn’t kill you all.
Use the view from the doorway to get an understanding of the situation. Then act!