Email or username:

Password:

Forgot your password?
josef

PROGRAMMING TIP
avoid repeating the same 4 lines of code twice by writing 270 lines of beautiful, abstract, generic code

42 comments
Graham Downs

@jk So when four things fail, you have to go through the 270 lines of abstract, generic code looking for the correct line to change.

As opposed to fixing it in one of the four places but forgetting the other three.

Meh. It's a subject of debate. I'd still rather have a single place to change it. 🤔

slotos

@GrahamDowns @jk remember, code looking similar doesn’t make it the same.

DRY principle is about knowledge, not code. There’s an expensive difference between the two.

Graham Downs

@slotos That's fair too. Each of those four implementations could be slightly different, and for various reasons, need to stay that way. Not every hammer is suitable for every nail. ;-) @jk

mikeTesteLinux

@GrahamDowns @slotos @jk So true. DRY is usually interpreted so "by the book" that sometimes it give birth to a monstrous code.

Sean Murthy

@GrahamDowns @slotos @jk Nothing a little control coupling can't fix. 😉

Bee O'Problem

@GrahamDowns @jk eh

IME the former = an hour to fix, an hour to notice I missed something then a few minutes to find the copypasta that wasn't fixed. Maybe an hour or two if I feel like refactoring it into a function/class/whatever.

The abstract mess is hour(s) to find the part to change. Changing it, then hours more figuring out why it broke in some weird illogical way instead of fixing the problem.

Graham Downs

@beeoproblem @jk I've had cases where it was months to find the other parts I missed. After the next release. When it was in the hands of a client, and caused a show-stopping bug for them. Because the dev who fixed it didn't know (or had long since forgotten) about the other three.

It happens. :/

Daniel Marks

@jk Or just use preprocess contortions. In the end, it's all about keeping things elegant and easy to understand as perceived by whoever wrote it.

synlogic

@profdc9 @jk the Jia Tan Way. foreign intel hacking ops loooooooove to see the more codegen and preprocessing the better!

Jason Lefkowitz

@jk Real architects start by writing a BeautifulAbstractGenericCodeFactory

minimantis

@jk Why write four lines when you can craft a 270-line epic? After all, every programmer knows that complexity is just simplicity dressed in a tuxedo

Stefan Zitz

@jk I can't tell why but this reminds me on #OpenFOAM 🙄 . The finite volume library is nice and people are able to do stunning things with it, but inherentency hell grows with every solver.

Janneke

@jk
If you are pressed to repeat lines of code, you should consider using a better programming language.

Codhisattva

@jk @lisamelton why write a conditional statement when you can write a DSL that can operate on any values in any way possible? Future prooooooooooooofffffffffff!

synlogic

@jk lol. bingo

the horrors I've seen in legacy Java enterprise codebases where it was nearly futile trying to find a section that did a real task *truly* needed by the enduser back in the real world

Jeroen van Bergen

@synlogic @jk Read in a Rutger Hauer voice: “I’ve seen stack traces you people would not believe. Frameworks stacked on top of frameworks. I’ve used GUI’s that did not really work, but somehow met the requirements. All this code…. will run forever”

synlogic

@jeroenvanbergen @jk lol. nice. "Like tears
.. in the rain."

"Time to kill -9"

Roger Sen

@jk …and complain that the abstraction leaks.

AliveDevil

@jk
Avoid copying a large one-of transposition of mostly static JS to JSON by writing a whole JS parser, which automatically exports JSON.

@seri

Nadiyar V2.7 :anarchy_heart:

@jk the problem is it usually won't remain 4 lines for a long time. And honestly I'm not experienced enough to predict future complexity.
Writing 270 lines of generic code is easier and safer for me.

gkrnours

@jk one of my ex coworker took my code, decided the bind the cli interface and the HTTP request together because they were two pair of theses. This resulted in a tight coupling, making the code hard to work with, especially for anything else than direct call to the API and left us with no client for our API so other coworker were calling the CLI, written in python, from their code, also python.

That was a factor in choosing to leave the company

🇵🇸 Álvaro González

@jk Only to discover, when you eventually need a third occurrence, that the beautiful generic abstraction does not fully accommodate that use case and needs serious refactoring.

Curioso 🍉 🇺🇦 (jgg)

@jk

Be sure to write a class to abstract integers so they can be short, long, floats or even complex in a transparent and dynamic way. And can be persisted in the cloud, database, text files or clay tablets, depending on how much time you need to keep the data.

You know you need that.

Wolf480pl

@jgg @jk
everyone has done that in high school right?

[DATA EXPUNGED]
David Mankins

@jk

I once spent a week trying to find a bug in an obscure set of nested loops — 200+ lines of relatively obscure Cython (“for efficiency”).

Walking home one night, I realized I could replace the whole thing with a combination of matrix operations.

200 lines of buggy code turned into four lines of Numpy that
1) worked
2) was much faster than the Cython

but which felt like it required a 200 line comment explaining *why* it worked.

(Also, while writing the comment, I found the elusive bug in the original code.)

Programming tip: When Guy Steele says there’s something to be gained from learning APL, don’t wait twenty years before following up on it.

@jk

I once spent a week trying to find a bug in an obscure set of nested loops — 200+ lines of relatively obscure Cython (“for efficiency”).

Walking home one night, I realized I could replace the whole thing with a combination of matrix operations.

200 lines of buggy code turned into four lines of Numpy that
1) worked
2) was much faster than the Cython

Ype Kingma

@jk

My threshold for duplicate code is about 15 lines, and only "near" each other in the same text file.

Wolf480pl

@jk also make sure the behaviour depends on the return type, which is infered by the compiler for every call site separately.

Григорий Клюшников

I'm the opposite. Smithereen, the server app I'm writing this from, now has photo albums and comments on photos in them, and just yesterday I removed all duplication for handling federated likes on different object types. Feels so ✨clean✨ to have all likes end up in one single universal method.

Go Up