Why Bad Code Exists: It Is Not Just a Lack of Language Knowledge
What is bad programming code and why does it exist? Is it simply that some programmers don't know the whole language of the code they are using, where one "word" can do certain...
What is bad programming code and why does it exist? Is it simply that some programmers don't know the whole language of the code they are using, where one "word" can do certain things easier than the one they're using?
Design is hard.
I tend to think of the analogy to writing novels. Or even non-fiction.
No one knows every word that's used in English. It would be near impossible, simply because of the rate of introduction of new words in various jargons, new slang, and words invented by obscure authors that barely anyone reads.
But a good writer can write well using the vocabulary they have mastered. A novelist can spin a story that includes someone working on a bridge without needing to know engineering or understanding all of the specific terminology concerning weight distribution and material properties.
And a technical writer can describe a process in “plain English” that a scientist might use a lot of specific terminology to describe, but a good technical writer will communicate what's necessary for their audience to understand without needing to use obscure words.
In programming, in order to produce clean code you need to:
Have a clear understanding of the problem you're trying to solve.
Be able to design a clean solution that will solve the problem.
Understand enough of the language you're using (and its idioms) to be able to express your solution clearly.
Have enough experience to avoid common bad practices like the use of “magic numbers,” unnecessarily convoluted algorithms, DRY violations, and a hundred other bits of wisdom about software development that the best developers learn over their career.
Have enough skill at programming in general to be able to mentally model various solutions, and the judgment to know which of those potential solutions will end up being the best.
Only #3 has anything to do with the language. Yes, people will sometimes write code that's using an idiom from another language they know, and someone might describe that as bad code.
But honestly, most “bad code” is written by developers who have been using the same language for years, so they can't really make the excuse of unfamiliarity. Instead it's much more frequently the lack of one or more of the other traits that allows bad code to be written.
Sometimes short deadlines are blamed for bad code. But short deadlines don't cause bad code. Developers willing to take shortcuts to satisfy unreasonable managers write bad code.
As an industry we lack a general sense of professional ethics. We write crap code because we were “just following orders.” We hire developers who don't have the skill to write a for loop without Googling it. We blame bad management, even though other kinds of engineers would scoff at the idea of taking shortcuts “to get it done faster” when designing a building, or a bridge, or a freeway.
And then we wonder why our industry is mocked for being so terrible.
XKCD.
We really need to take responsibly for quality, and push back to managers who demand unreasonable results. We need a sense of esprit de corps that demands we create quality code instead of shipping whatever garbage we can get to barely run on our own system.
We need to do better.