Live data from Hacker News

(Some) ORM haters do get it

revision-zero.org

31–40 of 108 posts

Re: (Some) ORM haters do get it

#31
He's wrong.

SQL is great for what it does, but I use ORMs for reasons other than writing queries in a different way. I inherited an utter mess of a schema that wasn't even remotely close to 1NF. Imagine fields containing comma-joined sets of values, and with column names not even remotely related to what they actually held. For legacy and business purposes, updating the schema was a non-starter.

So I used SQLAlchemy to remap the schema into something usable. I wrote getters that split out those comma-joined fields and returned the desired value against tables that required indexes like:

    UPPER(SUBSTR(name_delpt,1,STRPOS(name_delpt,',')))
I wrote (and therefore more importantly _documented_) the bizarre and complex way some of the tables joined together. I wrote something that was unit-testable and that could be used as a foundation for other work so that I wouldn't have to memorize the insane corner cases and reproduce them from scratch each time I needed to access the data in some little-used table.

I _didn't_ write a more convenient way to say `SELECT * FROM blog`. In general, that doesn't interest me and I wouldn't have bothered with it. ORMs are great - if used well! - for encapsulating all of the little bits of business cruft in one central, easy-to-manage place. They're handy for roughly the same reasons that subroutines are handy.

Re: (Some) ORM haters do get it

#32
post #6

There is a need to manipulate relational data from object oriented code. ORMs are tools that facilitate that. The Object/Relational impedance problem doesn't go away if you hand-carve the code, it just makes you work hard on all the points of contact instead of just the problematic ones. The real "problem" with ORM is when people use such tools as a way of avoiding having to understand databases (and specifically SQL…

I think you've hit on something here. I came to start using ORMs after 10+ years of writing / optimising databases and SQL. When using an ORM (and most of my work is done with a probably not-well-known one, XPO from DevExpress) I'm aware of what is (or should!) be happening under the hood; my prior experience with 'bare metal' is extremely useful, nay essential to creating a performant system. On occasion, it's necessary to do a direct SQL query, but not often.

Sure, the apps I'm writing are for small-medium business, but XPO 'just works'. Context is important; if I was working on something with more users / tighter speed requirements, an ORM may or may not be the best choice. Still, this falls into the 'right tool for the job' category that good developers are already aware of.

Re: (Some) ORM haters do get it

#33

Well now I'm reminded of my initial though when a colleague first introduced me to an ORM: "It feels wrong to use this tool to just map every object to a table." Of course I went on to write many, at best, moderately complicated web apps very fast using NHibernate and didn't miss writing vender-specific SQL or column to property mapping boilerplate. But this article is a breath of fresh air. I may just try Dapper for…

I agree, microORMs are the right solution if you are trapped in the mainstream. I'm a very happy user of Dapper.

Re: (Some) ORM haters do get it

#34
post #19

The idea of translating the declarative way of doing things to an imperative approach (that's basically what ORMs are doing) is imho a huge failure. It just never worked decently. These days, we have languages which integrate rather nicely into the declarative mindset, so no need anymore for such bizarre "paradigm translators".

What are those languages and how do they integrate nicely into the declarative mindset?

Re: (Some) ORM haters do get it

#35
post #3

Earlier quoted context omitted.

We could be more subtle, and go with "it depends". For getting and putting data to be used on an edit form, using an object/class which actually has (non relational constraint) "business logic" in it, ORM good. For batch mass update, ORM bad. (one wonders if every table needs a custom class, though)

Batch updates aren't where it ends. As a Django developer with a strong SQL background I find my hands are tied far more often than I would like. Sometimes this is caused by bugs like the current group by bug[1], other times it's caused by the design of the ORM. I do agree that the ORM is handy for things like "Get me all the things in this table", and "update this single record using a form", but this author is dead…

I don't know about the django ORM, however for some other modern ORM's batch operations and statement reordering are one of the top ORM benefits. If you are developing a set of API's that you have no idea how they'll be consumed, the combination of declarative transactions and heuristically optimized ORM based reordered statements + batch updates gives you easy to understand code, proper transaction semantics and really good performance. Without the ORM + declarative transactions you may end up writing ugly apis that pass transactions or connections around and you'll end up having a major task of optimizing the database interactions. Learn your tool, use it for the right use cases and you'll be happy. Putting your left shoe on the right foot will always feel wrong.

Re: (Some) ORM haters do get it

#36
post #7

Can anyone point out the coup de grâce the author seems to think he has arrived at? He points out some (well known) ways that ORM's can be used inneficiently, and acknowledges the techniques that have been developed to work around these, but then seems to conclude that he has proven once and for all that ORM's are bad. I totally missed the connection on that part. Is it that SQL is better in dealing with sets than an…

The connection (according to the author as far as I've understood him) is: "All the techniques that have been developed to work around the inefficiencies of ORMs are just reinventing the wheel. The solutions have been there for 40 years and your workarounds are only needed because you think about individuals (object-orientied) instead of sets (relational)."

Which is creating a false dichotomy. Assuming your are using some OO language then you will have to, at some point, turn the relational data into objects. At that point you are always creating an ORM.

There is a really interesting post about how writing that code is literally stealing from your clients. I tend to agree. http://ayende.com/blog/3712/stealing-from-your-client

Re: (Some) ORM haters do get it

#37
post #26
post #12

I don't understand where this ORM 'divide' is coming from. ORMs are powerful, because they let you say less and do more. For 90% of the queries out there, an ORM is fine. SQL is powerful, because you can control and fine-tune your statements. For the remaining 10%, use SQL. Are ORMs bad? No. Can you them for everything? No. The same thing can be said for almost every technology in existence.

> ORMs are powerful, because they let you say less and do more To me this is a bonus. The real deal is that ORMs allow you to write queries as "first-class" components of the language, hence benefiting from language features such as type checks, duck-typing, factoring, static analysis even, and more. Compare this to stitching strings (however parametrized they are) and manually coercing your object values to strings.…

Don't forget the whole other side of it.

http://en.wikipedia.org/wiki/Object-relational_impedance_mis...

Re: (Some) ORM haters do get it

#38
Yet another software religious war, which seems to boil down to "if you don't think my technology is correct 100% of the time, you are insulting my honor".

Do other fields get into constant pissing matches like this? Languages, libraries, process, licenses, editors, Operating systems, you name it, software engineers are fighting about how much better theirs is and how you are an idiot for not seeing the true light their vast intelligence is trying to bequeath unto you.

What is it about our brains that makes the subtlety of "use the right tool; every problem isn't a nail" so difficult? Or is it just hard wired into our need to be identified with a community?

Re: (Some) ORM haters do get it

#39
post #12

I don't understand where this ORM 'divide' is coming from. ORMs are powerful, because they let you say less and do more. For 90% of the queries out there, an ORM is fine. SQL is powerful, because you can control and fine-tune your statements. For the remaining 10%, use SQL. Are ORMs bad? No. Can you them for everything? No. The same thing can be said for almost every technology in existence.

"ORM is fine" 90% or even 100% of the time don't make them right. Thee problem is that OOP is not well defined, it belongs to "soft" science, and relational databases are "hard" science. I have read the article about ORM and Vietnam, in we just have all of the symptoms of impedance mismatch.

I have to add that this issue is much deeper than a simple efficiency issue. A perfect db with 100% availability would still not be well matched to an object system.

I listened to a podcast from 1990, the guy said "the problem with objects is that we don't know what they are". I just finished SICP, and it seems it is the same conclusion: objects with their state, their messages, their multiple parents, their instanciation, are not a proper model for many or most cases in software development.

Example of proper models include types, text files, streams (pipes), modules, maybe services.

Re: (Some) ORM haters do get it

#40
post #12

I don't understand where this ORM 'divide' is coming from. ORMs are powerful, because they let you say less and do more. For 90% of the queries out there, an ORM is fine. SQL is powerful, because you can control and fine-tune your statements. For the remaining 10%, use SQL. Are ORMs bad? No. Can you them for everything? No. The same thing can be said for almost every technology in existence.

The divide is fundamentally about choosing what's in charge of your system, the system being composed of your databases, your applications, and your supporting infrastructure (your scripts, your migrations, etc.) To relational database folk such as myself, the central authority is the database, and our principal interests are what ACID exists to provide: concurrent, isolated, atomic transactions that cannot be lost, on top of a well-defined schema with strong data validity guarantees. To us, what's most important is the data, so everything else must serve that end: the data must always be valid and meaningful and flexible to query.

The side that argues for ORM has chosen the application, the codebase, to be in charge. The central authority is the code because all the data must ultimately enter or exit through the code, and the code has more flexible abstractions and better reuse characteristics.

The reason for the disagreement comes down to disagreement about what a database is about. To the OO programmer, strong validation is part of the behavior of the objects in a system: the objects are data and behavior, so they should know what makes them valid. So the OO perspective is that the objects are reality and the database is just the persistence mechanism. It doesn't matter much to the programmer how the data is stored, it's that the data is stored, and it just happens that nowadays we use relational databases. This is the perspective that sees SQL is this annoying middle layer between the storage and the objects.

To the relational database person, the database is what is real, and the objects are mostly irrelevant. We want the database to enforce validity because there will always wind up being tools outside the OO library that need to access the database and we don't want those tools to screw up the data. To us, screwing up the data is far worse than making development a little less convenient. We see SQL not as primarily a transport between the reality of the code and some kind of storage mechanism, but rather as a general purpose data restructuring tool. Most any page on most websites can be generated with just a small handful of queries if you know how to write them to properly filter, summarize and restructure the data. We see SQL as a tremendously powerful tool for everyday tasks, not as a burdensome way of inserting and retrieving records, and not as some kind of vehicle for performance optimization.

At the end of the day, we need both perspectives. If the code is tedious and unpleasant to write, it won't be written correctly. The code must be written--the database is not the appropriate thing to be running a web server and servicing clients directly. OOP is still the dominant programming methodology, and for good reasons, but encapsulation stands at odds with proper database design. But people who ignore data validity are eventually bitten by consistency problems. OODBs have failed to take off for a variety of reasons, but one that can't be easily discounted is that they are almost always tied to one or two languages, which makes it very hard to do the kind of scripting and reporting that invariably crop up with long-lived data. What starts out as application-specific data almost invariably becomes central to the organization with many clients written in many different languages and frameworks.

We're sort of destined to hate ORM, because the people who love databases aren't going to love ORM no matter what, and people who hate databases will resent how much effort they require to use properly.

Post reply on HN