It’s a good question and not one I think has any simple answer. No solution is going to fit every dog or every family. It seems pretty obvious to say that getting a dog is not something anyone should ever do on a whim but it’s also obvious that not everyone thinks about it before they do it. And I get that it’s not always that simple.
Since others have brought up invisible fencing I’ll add this warning: not only does it subject your dog to shocks it also doesn’t prevent other dogs or other animals from entering your yard.