The truth is the only acceptable way to perform a layoff is to not need to perform a layoff, be it more profit or not over expanding. Though as a warning, there will be upset if the company doesn't expand fast enough or doesn't invest the profit too. If reality hits it's a matter of avoiding ways of doing it which are worse not finding a way everyone is going to agree was a great way to get rid of a bunch of jobs.
I think layoffs leave a bad taste in the mouth because they seem to suggest that some employees weren't necessary. If you aren't cutting programs or products, you are either saying some employees did nothing, or the remaining employees will shoulder more work for the same pay.
I wonder: if layoffs were accompanied by a list of programs/products that are being sunset, if it would be more palatable.
But you say "sometimes... It's close". I think you have to allow for the scenario where it's not close, and management mad a bad call, and need to suffer the consequences. But THEY don't suffer the consequences... And their subordinates do.
However, just as it doesn't make sense when people call for blood/resignation/pay cut/etc of an engineer the instant they make a costly mistake (even though some poorly run places of course will), it's not a given that a mistake in management was made therefore we need the blood of some in management. The question on consequences, in both cases, is usually "would it be expected someone else in that role is doing significantly better at avoiding these problems" not "has this employee ever made a mistake".