According to third edition designer Skip Williams, in his article Attacks of Opportunity (Part One), D&D uses attacks of opportunity to add tactical complexity and danger, to discourage certain actions in combat without banning them outright, and to balance out useful or powerful combat manoevers:
Skip Williams:
The D&D game uses its attack of opportunity rules to add some spice to combat. These rules offer characters more options in combat than just standing there and exchanging attacks with foes, while at the same time making sure that characters involved in a fight have a proper appreciation for the dangers they face.
Many rules in the game would be very different (and probably much harder to use) without attacks of opportunity to balance them. The rules for spellcasting, ranged attacks, movement, and special attack actions such as disarming, grappling, and tripping all depend on the existence of attacks of opportunity.
In some cases, attacks of opportunity mimic the rules of the miniature wargames that inspired D&D:
- In ancient warfare, when two units were engaged in hand-to-hand combat, the first to turn and flee suffered losses as the enemy took the opportunity to press the attack. A unit had to withdraw very carefully to avoid this.
- Archers fought poorly against adjacent melee units.
- Unarmed men are at penalty against armed men, whose weapons typically grant longer reach.
In other cases, they enforce the thematics of D&D's fantasy tropes:
- Wizards aren't front-line fighters, so they are penalized for casting when threatened by an enemy in melee. (For the same reason, wizards can't wear armour without penalty and don't have martial weapon proficiencies.)
Sometimes, the attack of opportunity exists to balance out special attacks:
- Mike Mearls' Book of Iron Might (2004) analyzes D&D 3.5's combat system and concludes that without the attack of opportunity, most special attacks like trip and disarm would require an attack penalty of -10 or more to balance them out.
- Attack penalties are bad, because missing is dull. Failure as a penalty is boring; danger as a penalty is exciting. Danger penalties reward you for taking risks, while attack penalties discourage you from trying.
- If there were no drawback to trip, disarm and the like, players may use these attacks more frequently than the game's designers intended.