I went 24" on my 6.5 Creedmoor and run it with an Allen Engineering suppressor. I didn't want too long a barrel for balance reasons when running the suppressor. I'm very happy with the setup and wouldn't change it. Velocity affects trajectory. Decide how far you realistically want to shoot the rifle. I'm getting to 1000 with ~31 MOA (depending on conditions) dialed into the scope using factory Hornady 140-grain.
We've built dedicated light-weight hunting rifles with 18" barrels and those shoot as well as longer barrels just with more of a trajectory arc especially at distance.
Bottom line, I don't think there is a "one correct answer" for "ideal" barrel length. You need to define "ideal" and "long range" I guess. If it is just target shooting, a longer barrel will help increase velocity.
Best regards,
Mark