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

@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. 🤔

@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.

@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. :/

@GrahamDowns
Third option: use #define to create a macro of your 4 lines of code, so you only have to type a few letters each time! Best of both worlds! (As long as you aren't worried about the size of the resulting compiled program).
@jk

@dheadshot There are always going to be minor changes in each implementation, though, to make it fit the context. Variable names, mostly. I don't know the tool you're talking about though, so I don't know if it's able to handle that. And when it comes to maintenance/enhancement/bug fixing, will I still be able to make the change in only one place and have it automatically propagate to all four places?

If so, then what you're describing is basically creating a single, generic, abstract function that you call from four different places. In which case, we have an accord. :-)

If not, then see my other replies: one of the biggest issues with duplicating code like that is that people change it in one place but don't know or forget that they need to change it in three others. The three other places maybe affect functionality that's not used very often or not used by very many clients, so it gets overlooked... until the next release, maybe months down the line, where it causes a show-stopping bug for a client and now we have to drop everything and work all-nighters to get it fixed. It's happened. @jk

@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
@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.
@jk
If you are pressed to repeat lines of code, you should consider using a better programming language.
@janneke @jk -- Lisp advocate decloaks off the port bow. heh
@janneke @jk
designing* a better programming language
@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!

@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

@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”

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

"Time to kill -9"

Dare you enter my abstract factory?

Posted in r/ProgrammerHumor by u/jetsparrow • 4,857 points and 413 comments

reddit

@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

@jk ChefsKissAbstractBuilderBuilder
@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.
@Naadiyaar @jk That’s why these problems exist. Developers imagine it will grow and be something else in the future. In reality, it probably won’t. And if it did, that’s when you refactor, not before.

@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

@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.

@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.

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

@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 also make sure the behaviour depends on the return type, which is infered by the compiler for every call site separately.

@jk Do repeat yourself!

DO. FUCKING. REPEAT YOURSELF!!

If it makes your code more robust and easier to debug, DO REPEAT YOURSELF!!!!

@jk I am in this picture and I don't like it. 
@jk somewhere between DRY, YAGNI, and premature optimization lies my laziest most perfect code.
@jk Glad I'm laughing so hard, otherwise I would just be crying.
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.
@jk And then use it to replace one of the two lines because the other one doesn't fit the abstraction conceptually. 😜
@jk In my experience, making 4 lines parametrizeable takes 4 lines. Maybe slightly longer, since you now have speaking variable names instead of values. If you make that 270 lines, you are not lazy enough to be a programmer. I suggest a career in human language writing.