Exactly!
I have been running a laboratory in the last course that tries to fix that. I give them a working project, and I make them to evolve it each week during a couple of months in teams of 5 or 6. I have run the laboratory only twice, but some results look promising. Students have realized how important it is to structure code well, and how things can go badly if there is no a minimum quality control.
But the theory classes, and the rest of the degree, accumulate the problems that you have explained.