Prison serves 3 purposes, or at least should.
The first is a deterrence. This is quite a yes/no thing however. Longer sentences, or worse conditions don't increase its effect.
Second is re-education. This is where most effort should be focused. You need to simultaneously break the bad habits causing issues, and implant good ones (in the form of skills, and improved social situations). The aim is to make them a productive member of society again.
Last is containment. Some people either cannot or will not function safely in society. These people either need to be contained indefinitely, or killed. Given the unreliability of the justice system, the latter is a dangerous route to walk, and often more expensive.
I'm personally of the view that we should all have free (tax funded), access to retraining courses and resources, along with physical, mental, and social health systems. Prison should mostly be focused on the enforced use of these. They are contained while they retrain and get the help they need. They are then released in a better state than they went in. It's the most cost efficient option. The Scandinavian countries already use something similar (for convicts), and it seems highly effective.