PROGRAMMING TIP
avoid repeating the same 4 lines of code twice by writing 270 lines of beautiful, abstract, generic code
PROGRAMMING TIP 42 comments
@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 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. @GrahamDowns @slotos @jk So true. DRY is usually interpreted so "by the book" that sometimes it give birth to a monstrous code. @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. :/ @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. @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 @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 @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. @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. 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.
[DATA EXPUNGED]
My threshold for duplicate code is about 15 lines, and only "near" each other in the same text file. 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 class BaseTitle