In my experience bad code isn’t written for the sake of being bad. A lot of companies don’t value IT and software they work with almost daily as much as they should. The idea is that if they click a button and it works, it works. Next project. Not enough time, pressure from everywhere to develop new features and behaviours and there you go, badly written code.