Why do (some) engineers write bad code?

I’ve been reading System Performance Tuning, 2nd Edition recently. Though the content is a bit dated as a reference (was written while Linux kernel 2.4 was coming up) but this is still a fantastic book on refreshing the workings of the components of a computer and knowing the tweaks for solaris and linux.

This post though, is focussed on a topic that is touched towards the end of the book. Why do we end up writing bad code?

Bad code could mean many things - bad design or architecture decision, badly written code that is harder to maintain (duplication, tightly coupled components, less modular etc.), code that is bad at performance (time or space) and so on. The book talks about following reasons for badly written code (most of it is verbatim with few modifications here and there):

TIP: Learn to say No to “can we do this quickly” in favor of better thought of and more solid code or design. You will thank yourself for it more often than not.

TIP: Invest in learning about common algorithms & data structures and weigh upon choices when you’re faced with a problem. Almost always the first solution that strikes one’s mind is not the best.

Also, experience wise I find that, bad code gets copied more than good code (remember ‘it is implemented the same way in that file / module / project etc.’? at the workplace). While re-writes are hard, it’s always possible to refactor the code as and when possible to make it more friendlier to improvements.

TIP: The basic idea here is whenever you write a line of code, it should be refactored. If you’re rewriting pieces of legacy code, those should be refactored as well.

Good night!