Nominally 24" is a good length and is also a decent sight length for iron sights But it depends on the chambering & use.
For most chamberings, most rounds gain little or nothing from going longer than 24". However a large caliber, large capacity round (eg 458 Win Mag, 375 H&H) uses slower burning powder and can benefit from a longer barrel so a 26" barrel is preferred. Alternatively for lighter rounds (eg rimfire, pistol calibers in a rifle) a 22" barrel works fine.
Then the choice is modified per intended usage, A brush gun would certainly be fine in a shorter length for ease of handling & shorter shots. And for a rifle intended for long difficult hikes I'd consider compromising with a shorter barrel though I'd do everything else to reduce weight first.