Live data from Hacker News

DRY is an over-rated programming principle?

gordonc.bearblog.dev

371–380 of 501 posts

Re: DRY is an over-rated programming principle?

#373

Every time I read an article like this, "why is overrated", I think, yeah you are right in theory. But most places I have worked, these best practices were not overused, but underused. If you have the problem that your coworkers create unneccessary abstractions, I envy you, because I have so often had the opposite problem. Maybe this is not the case if you work in a great software development team. But if you work so…

> People not able to factor out functions or structure their code in a readable way. Variables are called v1, v2, v3. Unit testing seen as a waste of time. CI seen as a fun toy. They lack the experience to even notice the difference. Had a colleague work under a 'team lead'. Needed to take a form with variable amount of rows of input data - max 50 - and take data, parse it, and store it. Took 20-30 lines of code. Nex…

One time I needed to sort some data arbitrarily — the resulting order did not matter, it only mattered that it was the same for the same data in different orders.

My senior engineer advised me against using Java .sort() because “we didn’t write it so we couldn’t be sure it would do the same thing every time.”

Re: DRY is an over-rated programming principle?

#375

Earlier quoted context omitted.

make_pepperoni_pizza() is bad code compared to make_pizza(toppings=[PEPPERONI]) How would you make Hawaiian pizza? I forget, does it include Ham? or just pineapple? you're forced to the remember that nuance in your suggested implementation, but not with "make_hawaiian_pizza()"

It's data. make_pizza(toppings=HAWAIIAN_TOPPINGS) or make_pizza(HAWAIIAN) or similar. Data should generally not be hard-coded, both because it changes and because it wants to be validated. Starting with: HAWAIIAN = { TOPPINGS: [ PINEAPPLE ... is okay. That can later be loaded from a config file, a database, or otherwise, as the system expands.

There's an interesting architectural decision here: what form of the pizza recipes database strikes the right balance between too hardcoded and too complex. I'd use some kind of configuration file or RDBMS, constants are more readable but still out of place as part of code.

Re: DRY is an over-rated programming principle?

#376

Earlier quoted context omitted.

Yeah there are a lot of definitions out there that are along these lines and they do hollow out my argument. But why call it "Don't Repeat Yourself" if it actually means something somewhat more subtle than that. I firmly believe many junior developers don't grasp the nuance and based on the comments I'm not the only one who thinks this. So if DRY is widely understood by developers to mean literally "don't repeat your…

I think the rule should be "Try not to repeat yourself" Rules are like alarms they draw our attention to some peculiar condition which gives us pause to think about if it's kosher and if not why not.

The art of programming is finding the fit and exceptions to the rules. It's just, frankly, a lot easier to be dogmatic.

Someone says "never do this" or "always do that" and you can apply those rules with abandon (often leaving a maintenance nightmare in your wake).

There are no rules to programming.

Re: DRY is an over-rated programming principle?

#377

Earlier quoted context omitted.

> People not able to factor out functions or structure their code in a readable way. Variables are called v1, v2, v3. Unit testing seen as a waste of time. CI seen as a fun toy. They lack the experience to even notice the difference. Had a colleague work under a 'team lead'. Needed to take a form with variable amount of rows of input data - max 50 - and take data, parse it, and store it. Took 20-30 lines of code. Nex…

It's not usually needed (especially these days), but there are times that it is better to repeat every possible iteration by hand and not have a loop. This is a technique called loop unrolling. It is done for performance reasons. This is something we used to do at a company working on games for the old feature phones (think Nokia 30/40/60 series stuff). The devices were very limited, there is no direct control over J…

Loop unrolling seems like something that should be done by a compiler when you turn on aggressive optimization flags, and not something you need to code explicitly.

Re: DRY is an over-rated programming principle?

#378

The article is actually good. The criticisms of DRY here are valid! For criticism 1, I had a coworker once say something that resonated with me: "Just because two things are the same right now doesn't mean they _should_ be the same." So that criticism is totally valid - DRY has to be applied only when things _should_ be the same, and that can actually be hard to identify. That being said, of course the title of the a…

No, this article isn't good because it discusses alternative options within the boundaries of seriously wrong premises (write nonsensical hardcoded recipes "right"), and unsurprisingly all the options are bad.

Re: DRY is an over-rated programming principle?

#379
Speaking from many painful experiences, DRY is underrated. Duplicate code is a major liability, and due to the natural entropy of code, duplicate sections will slowly drift apart over time.

Yes, sometimes you may discover that you prematurely DRY'ed the code, and it was just accidentally similar. Easy, you just un-DRY the code. This is a trivial operation. In an IDE it might be a single keyboard shortcut. Going the other way is a difficult and error prone process.

Post reply on HN