Live data from Hacker News

It's OK if your code is just good enough

shiftmag.dev

91–100 of 149 posts

Re: It's OK if your code is just good enough

#91

Things like large functions or code duplication are not necessarily bad in the first place. A far bigger problem that I encounter regularly is the invention of extreme layers of abstraction to avoid a small amount of copy-pasting + edit in the name of DRY. But an even bigger problem is lack of understanding of the problem domain and a lack of documentation on how you plan to fix the problem.

I have to admit: I am terrified of WET code. I do stop short of introducing abstraction monstrosities, but I usually do create what others would call unnecessary abstractions, to stay DRY.

Why? Because I tend to write all my code such that a complete stranger should be able to drop in and understand it. I constantly imagine that stranger looking over my shoulder while coding. I imagine the code should be maintainable and speak for itself without me there at all (I do write comments).

So, such a person SHOULD be able to change some value or logic somewhere, and rely on not having to do that anywhere else. That is the magic of local reasoning, as brought about by structured programming, after eradicating goto statements. WET code erodes that. I find it a very important principle though and value it highly.

An example where this falls apart is config files. For example, a port number might be repeated in different places. Comments are indispensable then, but they rot. So if possible, I encode it using actual language constructs.

In summary, I do err on the side of DRY rather aggressively, but don’t follow it all of the time.

Re: It's OK if your code is just good enough

#92
post #48

Earlier quoted context omitted.

Every coworker I’ve had who thinks this way has left a minefield of gotchas and inscrutable interdependencies for the unfortunate developers who come after. Yeah they “got it done” but we spend 80% of our time fighting fires and the 20% left on new development takes ten times longer than it ought to because zero thought or care was put into anything other than “it works for me”. This to me is the difference between e…

> Every coworker I’ve had who thinks this way has left a minefield of gotchas and inscrutable interdependencies for the unfortunate developers who come after. I mean, then they weren't good engineers? Nobody said that approach is good. But I've also seen enough for my share of engineers that knowingly write buggy code that eventually blows up in someone's face because that code was simpler and turned out elegant that…

It’s also littered with the bodies of companies that failed to keep up with their early initial development speed because their development team cranked out two years’ worth of “whatever works” and walled themselves into a corner.

In my experience, that happens way more often than teams failing to produce value because they’ve spent eons polishing something to perfection.

On the other hand, our industry’s culture of not taking the time for anything to be built a little better means we have an enormous number of seemingly-experienced engineers who lack the understanding of how to write well-built software even if they are given the time. Which leads to individuals concluding that time spent cleaning things up is a waste because they end up with something worse and more complicated afterward. So they don’t invest in learning this skill, and the cycle repeats.

Re: It's OK if your code is just good enough

#94

Upfront, I mostly agree with the post. However, I may be a tad famous about striving for perfection in my code. [1] [2] Why? If "good enough" is good enough, why do I go further? For a few reasons: 1. I want the industry to be more professional [3] where it matters, and I need to set an example. 2. The kind of software I write already has alternatives, so mine needs to be far better to get adopted. And it does. [4] 3…

The first file I looked at in that codebase has a “goto” (as well as some IMO hacky-ish logic). Now I’m not going to say this is never right (but it probably isn’t), but it takes a lot of hubris to claim you are “striving for perfection” and I just don’t see perfect code using goto, sorry.

The goto's are for proper cleanup on error.

All of the options for doing so in C are awful; I just think goto is the least bad option. Otherwise, you get if statements that keep nesting, deeper and deeper.

And what's the hacky-ish logic you're talking about?

Re: It's OK if your code is just good enough

#95

Things like large functions or code duplication are not necessarily bad in the first place. A far bigger problem that I encounter regularly is the invention of extreme layers of abstraction to avoid a small amount of copy-pasting + edit in the name of DRY. But an even bigger problem is lack of understanding of the problem domain and a lack of documentation on how you plan to fix the problem.

I have to admit: I am terrified of WET code. I do stop short of introducing abstraction monstrosities, but I usually do create what others would call unnecessary abstractions, to stay DRY. Why? Because I tend to write all my code such that a complete stranger should be able to drop in and understand it. I constantly imagine that stranger looking over my shoulder while coding. I imagine the code should be maintainable…

> I am terrified of WET code.

and

> I usually do create what others would call unnecessary abstractions, to stay DRY.

Seem completely incompatible with

> Because I tend to write all my code such that a complete stranger should be able to drop in and understand it.

Now, I don't know the codebase you're in. It could be that your abstractions are perfectly fine, but DRY code != maintainable. You're sacrificing a ton of locality of behavior (LoB) to get that DRYness, and introducing potential spooky action at a distance. Not to mention the cognitive overhead of the abstractions.

I'm not saying to never abstract, but abstractions really only supply a benefit when the things involved are guaranteed to vary together. Usually via some sort of physical process. If it's just business logic having them vary together in the same place, eventually some dictate comes down from management to change one of them but not the other.

When this happens, you introduce weird bugs on the other side of the system. That's how a platform gets the reputation of being unmaintainable. In the bad old days, it used to be that it was globals being referenced by many different functions as well as unrestricted gotos being used to jump into the middle of a function, but I've seen it happen quite often with abstractions, even ones that seem like a good idea at the time.

The code we're talking about was actually pretty DRY. You need something that does the same thing as the last half of this function? Just push a different return address onto the stack and jump into it to re-use the code. Why repeat yourself? But it had terrible LoB, changing one function could break a completely unrelated function halfway across the project (and one that didn't even obviously call the function if you're using some sort of computed goto).

You've also identified that structured programming brought about an end to the worst of these abuses, but I think you've got the reason wrong. It's not about reducing the number of places that you need to change something. You can write perfectly structured code (actually, it's hard to write unstructured code these days) and still need to change logic in 5/10/20 places. And local reasoning is still preserved in this case, as locally would consider each of those places by themselves, assuming the logic is all in different functions/modules/etc. Structured programming changed so much because forcing functions to have defined entry/exit points allows for easier preservation of invariants. You can't have meaningful invariant checks if someone can just jump into your function just after those checks. It's also much easier to see what in the project depends on the code you're changing.

Re: It's OK if your code is just good enough

#96
post #70

Earlier quoted context omitted.

It's more subtle that that. There is a great saying "Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live". I've seen countless bright minds wonder in the pursuit of instant pleasure by adding unnecessary complexity. I have seen others outright sacrificing projects that support people's life to achieve an instant goal of learning a particular library or…

A similar saying, which I like more: "code as if your (hypothetical) children will have to maintain it"

Another alternative: "Code as if you'll have to come back and maintain this after you've completely forgotten how it works or that it ever existed, because there's a nontrivial chance that you will.".

Re: It's OK if your code is just good enough

#97
post #39

Unless the code is running on critical systems that put human lives at risk, good enough is the perfect amount of good. Getting things done is more important. Excluding above scenario, either you will make mistakes, or you are not tackling meaningful tasks. And that's okay. Allocate time for clean up when there's less ambiguity. The more you explore the problem, the better the issues become. First implementation will…

Code quality is for developers, not end users. It's fine for code to be atrociously structured if literally no one is ever going to read it, even in medical devices, as long as it works.

As another poster has said, code you no longer touch is dead. Usually, software needs to be maintained and modifying a badly written code is a nightmare scenario. That means that requested features are piling up in the backlog and the resulting mess is growing slower and buggier overtime.

Re: It's OK if your code is just good enough

#98

In the vast majority of cases, writing good, maintainable code does not require more time. The real problem is that the majority of people working as software engineers barely know what they are doing, and use excuses like this because it makes some amount of sense to the incompetent managers in charge of them.

I work with someone who regularly opens PRs for untested code. I'm talking stuff that hasn't even been run once: missing imports, undefined variables, etc... not bugs. I'm fine with bugs. I'm not fine with not testing your work in the most basic sense. PR reviews don't mean throwing crap over the wall and hoping the reviewer figures it out. With this guy, there is so much back-and-forth hand holding, it would be simp…

I'm 100% fine with opening a PR for "I just typed it" code...

with two caveats:

- a HUGE disclaimer at the top saying "DO NOT MERGE: not tested"

- also, you'll probably want to politely ask someone for a review, and be specific about what you're looking for

"Draft PRs" are fine for discussing topics or code or goals with a team mate.

It's all about getting feedback!

Re: It's OK if your code is just good enough

#99

In the vast majority of cases, writing good, maintainable code does not require more time. The real problem is that the majority of people working as software engineers barely know what they are doing, and use excuses like this because it makes some amount of sense to the incompetent managers in charge of them.

I work with someone who regularly opens PRs for untested code. I'm talking stuff that hasn't even been run once: missing imports, undefined variables, etc... not bugs. I'm fine with bugs. I'm not fine with not testing your work in the most basic sense. PR reviews don't mean throwing crap over the wall and hoping the reviewer figures it out. With this guy, there is so much back-and-forth hand holding, it would be simp…

This sounds like what I do, except I open them as draft PRs in some functional state that I've run locally first in order to solicit feedback as early as possible asynchronously.

Most teams I've seen don't practice any sort of iterative development, despite claiming to do so.

Re: It's OK if your code is just good enough

#100

Things like large functions or code duplication are not necessarily bad in the first place. A far bigger problem that I encounter regularly is the invention of extreme layers of abstraction to avoid a small amount of copy-pasting + edit in the name of DRY. But an even bigger problem is lack of understanding of the problem domain and a lack of documentation on how you plan to fix the problem.

I have to admit: I am terrified of WET code. I do stop short of introducing abstraction monstrosities, but I usually do create what others would call unnecessary abstractions, to stay DRY. Why? Because I tend to write all my code such that a complete stranger should be able to drop in and understand it. I constantly imagine that stranger looking over my shoulder while coding. I imagine the code should be maintainable…

Based on your name, I'd expect you'd be quite comfortable with producing WET work.
Post reply on HN