DEV Community

Discussion on: Bad, Bad Code!

Collapse
 
simbo1905 profile image
Simon Massey • Edited

We should not conflate saying the code is bad with saying the developer is bad. A dev might have to rush out a ux prototype. We can acknowledge it is possible a developer has to make compromises and maybe did a great job that resulted in less than ideal code. There is no contradiction there. In a blame free postmortem culture you acknowledge that people don't cause bad outcomes it is the situation. In the case of tactical code used in a prototype that tested a UX hypothesis it could be a good outcome. If so no postmortem.

Eventually everyone encounters a ”throw away prototype” that makes it onto production and causes dismay. At that point you won't say ”this is okay code that has me working late because the developer made necessary tradeoffs” you will say ”this code is bad”. Keep coding long enough and you will hit a situation where you were the author of the code. I don't think of myself as a bad person when I encounter that situation. I simply wish i had written better code. A mentor of mine told me he doesn't write throwaway code as he never had the time to learn how to do that as he is too busy every day trying to figure out how to write good code. I decided to be like him and when someone pressures me to meet a deadline I maybe put some more hours in but I never ship code I don’t feel a sense of pride about.

A huge amount of effort is expended by the industry and by developers trying to write better code. To argue that there is no such thing as bad code implies that there is no such thing as good code. That implies that all the effort that we individually and collectively expense to improve how we code or improve the code base before us is wasted effort. It isn't.

In my experience on large codebases with big teams the majority of devs believe that there is such a thing as bad code that they encounter with some frequency. It is contextual and relative and hard to measure. It has been argued that because it is contextual or relative and hard to define it doesn't exist. This isn't a compelling argument.

There will be some set of criteria or metrics, even subjective, that can rate the success of a piece of software. For my hobby projects it could be stars on my GitHub repo. At the office it could be billed hours to ship a story and maintain it over one year. We can perform a thought experiment and imagine that someone decided to sabotage the hobby or work project. That person slips in code with hidden bugs designed to be as hard as possible to fix. They deliver a masterpiece of deception. People don't recognize it as harmful code. It passes the reviews and appears to be like the rest of the software by any qualitative or subjective measure. The early bugs are apparently fixed but more issues are encountered by users due to the way fixes interact with the original code. To the saboteur the deceptive code is great code. It maximises their preferred outcome of bugs, lost revenue and developer misery. To everyone else it is bad code.

That thought experiment demonstrates that even if it is relative, hard to define, and hard to distinguish, bad code exists. This is a fictional example of someone deliberately trying to write bad code. Yet many devs will have encountered such deceptive and tricky to fix code where the author was just trying to do their job.

Collapse
 
stereobooster profile image
stereobooster

you will say ”this code is bad”

I don't say this is a "bad code". I say it is hard to work with this code (for me). And the next thing I try to figure out why is it hard, is it bad abstraction, is it due to changed requirements, is it misleading naming (maybe I interpret code differently than author), maybe I don't know something, maybe I have poor understanding of business model, maybe there are some business constraints, etc.

Think from this side: "bad code" is possibly a false claim (there is always a chance, that this isn't bad code, but you don't know something). Where is "I have troubles working with this code" is always true claim.

That thought experiment demonstrates that even if it is relative, hard to define, and hard to distinguish, bad code exists.

To me this is not bad code, this is malicious code.