I recall from somewhere that a 20" barrel is generally considered optimum, but there's more to it than that, especially with a .223.
For some reason a .223 doesn't lose as much velocity with a shorter barrel as most centerfires, meaning you can go shorter (more inherently accurate) without losing much velocity. A 16" "M4 length" barrel still works pretty well for velocity, allows a larger diameter barrel within the same weight limitations (even more
accuracy), easier handling in close quarters, etc, but it's loud
Interestingly enough, ballistic tests
of the 5.56/.223 have shown that (with some ammo) penetration and wound damage
actually increases with a lower velocity.