News for hackers. Stuff that matters.
Re:Repeating Yourself Thrice Doesn’t Turn You into a 3x Deve... by soulofmischief on Sunday July 30, 2023 @01:22PM (#36933566)
When writing tests, my goal is to verify a given routine works as intended.

I don't want to write tests for the same functionality over and over. Repeated functionality should be extracted, tested in isolation and then used in composition with other tested code.

This is how you write correct code without stress or worry. People that take "just write the code" as dogma have produced some of the most untestable, bug-ridden code I've ever encountered.

Re:Repeating Yourself Thrice Doesn’t Turn You into a 3x Deve... by echelon on Sunday July 30, 2023 @01:34PM (#36933721)
You're talking about two entirely different things.

OP is saying don't write a "magic thingcombobulator factory" that "simplifies X endpoints with Y and Z similar behavior". This might be an earnest attempt to try to speed development, but it all collapses under its own weight at scale. The maintainers after you will be left holding the bag and have immense difficulty refactoring or migrating to a new set of requirements, a new data model, or an entirely new service.

Clever abstraction kills.

I've dealt with undoing insane magicality by crazy lazy devs, mostly in magic method dispatch / monkey patch Ruby land.

One person once exposed the entire database as a "safe" SQL-like query parameter DSL. No more endpoints to write - just use the thing.

There are so many problems with this. For example, when millions of transactions per day on mobile clients or via third party integrators bake these assumptions in, you can't easily migrate them away. You have to keep serving the same data assumptions, even while you're gutting and changing everything under the hood.

Simple endpoint logic is best. Your data model should be well thought out, and the CRUD code is a well-defined, super maintainable means to manipulate it.

Re:Repeating Yourself Thrice Doesn’t Turn You into a 3x Deve... by klodolph on Sunday July 30, 2023 @01:59PM (#36934051)
Agreed. I’ve also gone into a codebase and seen the most boring code ever. It looked like examples from an intro to web programming class. The backend did simple parameteried SQL queries. It was a pleasure to work with.

My conclusion is that the real “star” developers will, most of the time, write code that’s so simple, it looks like anyone could have written it. They ship a project on time, with good performance and availability, and then they move on. Anyone can come in and maintain it because the code is so obvious.

Re:Repeating Yourself Thrice Doesn’t Turn You into a 3x Deve... by rightbyte on Sunday July 30, 2023 @07:11PM (#36936980)
So much this. I feel that many developers have some fear of writing simple code. Probably because that is the way newbies do it, i.e. straight forward big functions that do stuff.

Reading function pointer dispatch code, disguised as whatever it is called when not C, can be hopeless.

Re:Repeating Yourself Thrice Doesn’t Turn You into a 3x Deve... by lovasoa on Sunday July 30, 2023 @02:01PM (#36934070)
I think your parent comment was making a good point. The "just write the code" and "don't try to be smart" mentality is good only up to a certain point.

Too much "just write the code" ends up creating huge unmaintainable monstrosities.

When you have a lot of time in front of you and a large team, it's okay to just put two junior developers at work for two weeks, and get a big CRUD REST api in the end.

But when you are trying to iterate quickly with a small team, exposing your database is not as stupid an idea as it sounds. And that's why things like Firebase, Hasura, Apollo, Postgraphile, etc. are so popular.

The post is not trying to convince people to build custom DSLs just for querying their database (sorry you had to work with that). It is saying that there are things that exist today, that dramatically reduce the complexity of full stack applications. And that whether or not we like it, this is probably the direction the industry is taking.

Re:Repeating Yourself Thrice Doesn’t Turn You into a 3x Deve... by CuriouslyC on Sunday July 30, 2023 @03:59PM (#36935299)
The end goal is to minimize software TCO. In addition to being semantically less clear, repeated plumbing code tends to diverge over time, which makes it difficult to refactor and more bug prone if people assume behavior is homogenous.

The best way to handle cases that will be almost the same but may diverge over time is to create a functional mini DSL that describes the domain behavior, and create a template implementation that can be used if desired. Then everything is using a common language, and a non-template implementation indicates the presence of non-standard logic.

Re:Repeating Yourself Thrice Doesn’t Turn You into a 3x Deve... by disgruntledphd2 on Monday July 31, 2023 @05:46AM (#36940436)
> The best way to handle cases that will be almost the same but may diverge over time is to create a functional mini DSL that describes the domain behavior, and create a template implementation that can be used if desired. Then everything is using a common language, and a non-template implementation indicates the presence of non-standard logic.

I mean yeah, I'm a big fan of DSLs. The problem occurs when someone writes the DSL, doesn't document it and leaves. Then it becomes super, super painful to maintain and extend.

Basically I'm coming round to the conclusion that (assuming reasonably competent colleagues), the least experienced person should be able to maintain and extend the code if it's to have any hope of remaining useful over time.

And good tests, for gods sake test the crap out of anything complicated with well-chosen names so that people can read the tests and understand how the code should be used.

Re:Repeating Yourself Thrice Doesn’t Turn You into a 3x Deve... by CuriouslyC on Monday July 31, 2023 @08:14AM (#36941464)
If the functions are all clearly named and reasonably small-ish DSLs can be mostly self-documenting. Plus you can always ctrl click in your IDE of choice to view function source. I'm talking something like this:

f(base data) .validate() .domainAction1() .domainAction2() .log() .describe() .sendHtmlResponse()

Re:Repeating Yourself Thrice Doesn’t Turn You into a 3x Deve... by dkarl on Sunday July 30, 2023 @02:03PM (#36934099)
Dogma leads to shitty code no matter which way a person leans. One of the worst pieces of code I ever worked on was a query generator. Somebody noticed that there were recurring patterns in some BI-ish queries that were used to generate a dashboard for customers that wanted to see their usage, and they decided to factor out the redundant parts and eliminate the boilerplate.

What did they end up with? The few hundred lines of code expressing the BI queries shrank in half, but behind the simplicity was close to a thousand lines of dense, inscrutable magic. It was a net increase in LOC, but the value of the magic was supposed to compound as they added more queries. What happened was, the original programmer moved on, and every attempt to add more queries failed, until I joined and it was my turn to be sacrificed to the monster. (I did manage to figure it out. The key was realizing that the whole thing was stupid, from conception to execution — the other engineers had put the original programmer on a pedestal, and they were trying to make the code make sense, which it didn't.)

After making the query generator work for a few queries, I had established the credibility to say that we shouldn't use it anymore, and we should just write out all the boilerplate instead. Suddenly adding and modifying queries became something that anybody could do.

It isn't just custom code that ends up this way. I'm currently working on a project that uses SQLAlchemy, and as the glutton for punishment I am, I'm the person who cleans up all our SQLAlchemy difficulties. I virtually always have the documentation open in a tab, and I have the source code checked out to the version we use. If we just wrote raw SQL and wrote our own row mappers, we'd have twice as much database code, but we'd understand it, and anybody could write and debug it. Instead, half the team treats it as witchcraft, and I feel like I've invested more time learning SQLAlchemy in the last year than I ever spent learning SQL.

This is not to say I'm against abstraction, just that it can be done so poorly that it's counterproductive. You always have to compare -- are we better off with this, or without it? Saying that something reduces boilerplate or reduces repetition isn't the end of the conversation, even if it's true. You have to ask what the cost is.

Re:Repeating Yourself Thrice Doesn’t Turn You into a 3x Deve... by lovasoa on Sunday July 30, 2023 @02:43PM (#36934540)
This is very true. And this is what the blog post was advocating too ! It was not about using some smart custom ORM, but about writing dead simple raw SQL queries in SQLPage instead of hundreds of lines of python and typescript.
Re:Repeating Yourself Thrice Doesn’t Turn You into a 3x Deve... by dgb23 on Sunday July 30, 2023 @04:05PM (#36935371)
I like to go in the other direction:

Write the raw SQL and then generate the boilerplate from that.

This has very few surprises because it’s a bottom up approach. And even better: you can do the exact same thing by hand.

There’s tools/libs that help with that like hugsql (Clojure) or sqlc (Go and other languages).

Doing it top down (ORM etc.) is what can cause so many problems outside of the happy path and trivial cases. These tools basically need to reinvent SQL and map it into a procedural language.

Just use SQL!

Re:Repeating Yourself Thrice Doesn’t Turn You into a 3x Deve... by actionfromafar on Sunday July 30, 2023 @06:07PM (#36936493)
Query builders are a good golden path for me. Just SQL but extra hand holding in type coercion and syntax.
Re:Repeating Yourself Thrice Doesn’t Turn You into a 3x Deve... by dkarl on Monday July 31, 2023 @11:23AM (#36943990)
I think query builders can be very helpful in a language with a good type system. The only times I haven't used raw SQL and didn't feel like it was a massive mistake were when using Scala, via Slick and Quill.
Re:Repeating Yourself Thrice Doesn’t Turn You into a 3x Deve... by dkarl on Monday July 31, 2023 @11:27AM (#36944074)
There's a tool called PugSQL that looks promising for Python, but it seems that async isn't directly supported yet[0]. If I ever find time, I'd love to jump on this and make it work, but nobody should hold their breath for that.

[0] https://github.com/mcfunley/pugsql/issues/44

Re:Repeating Yourself Thrice Doesn’t Turn You into a 3x Deve... by edandersen on Sunday July 30, 2023 @08:09PM (#36937430)
Ah, the "homemade ORM" anti-pattern. My condolences.