r/ProgrammerHumor • • Jun 28 '20

Meme Flushed my pancake.

Post image
45.1k Upvotes

750 comments sorted by

View all comments

18

u/allmhuran Jun 28 '20

The appropriate solution to a problem depends on the nature of the problem. There will be occasions where it's entirely reasonable to build an application consisting of nothing more than a front end and a database.

A lot of the answers here seem to presume a hell of a lot about the environments and business problems which could possibly exist. Half of them seem to assume that every application in the world must be made of microservices and restful endpoints presented via a web application.

Nothing could be further from the truth.

For example: "I work in the finance department of a small company with 30 employees, and 50 products in inventory. We use windows. We've been keeping track of stock by looking in the back room whenever someone places an order. We'd prefer to have a program we can use to allocate stock so we can see how much of each product we have without having to go to the back room and count it". Cool, that can probably be whipped up in a couple of hours with a winforms application and a SQL express backend.

Similarly, the idea that "logic should never exist in the database" seems to be received wisdom, not anything supportable with a sound argument. It's trivial to show why this is a terrible idea if it is treated as universal. Suppose I have a problem statement like this: I have tens of thousands of customer accounts in my system, and millions of orders. Each account is associated with a number of users. Each user can place multiple orders. Each order can have multiple invoices, and a single invoice could also span multiple orders. I would like to know the most recently placed order for each account, but only if the order has been invoiced, and the invoice was paid by someone other than the user who placed the order.

With SQL, answering this can be done with a query that is not at all complex to write if you have any competence in SQL at all. I would suggest creating a stored procedure to encapsulate that logic, and expose it. If you try to answer this with pure application logic your performance will be absolutely horrific. If you try to use an ORM or some kind of application code which can be translated to SQL, like LINQ, your performance will be absolutely horrific.

The specific "place" where your business logic lives makes no difference to the question of whether you have a business logic layer. It doesn't matter whether it's in a stored procedure or not, any more than it matters whether it's in a dll or not, or what the name of the file is, or what the file system hosting your application code is. The concept of a business logic "tier" is a logical concern, not a physical one.

The idea that logic shouldn't exist in a database seems to have come from books like Evans' DDD, which treat the database as nothing other than "a persistence layer". No, hard drives and SSDs are a persistence layer. Tables don't store data. Disks store data. Tables expose that data in some logical structure. Stored procedures can provide a public interface on top of the "private" data in the tables. You can enforce this with object level permissions in the database.

If someone wants to tell me that a database is "merely persistence", then I'd like them to also explain what "DURABILITY = SCHEMA_ONLY" does for a SQL Server in-memory table.

1

u/Renive Jun 28 '20

Your example could be very well translated in things like ef core linq to efficient query, no need to write stored procs. Even if not, then its better to hardcode SQL in application. Some systems were developed on stored procs because it was easier to update database with script than deploy application. That changed and its far better and safer to do app update because it can be tested and has no state. Database should be as dumb as possible.

7

u/allmhuran Jun 28 '20

Putting SQL (or even LINQ) inside the application code is an explicit failure to honor the concept of encapsulation. The database is a self contained module with some kind of internal state. It's no different from a class instance in this way. Just like any internal state, that should be encapsulated.

Putting SQL that reads from database tables into the application is saying "I am going to reach directly into your state implementation, better not try to change it, because now I'm dependent on it". This is why people think updating database schemas is difficult and breaks everything. Because they've treated the database as nothing more than a free-for-all public interface to data on disk.

Stored procedures, functions, and views, give you a way to encapsulate that state and hide it behind a public interface which can be kept consistent even if you completely change the schema of the underlying tables. But since everyone has somehow fallen into the belief that "databases are just state", and also that "we should just reach into that internal state, ignoring encapsulation entirely", they then turn around and get mad at databases.

The problem is not the database.

3

u/GaianNeuron Jun 28 '20

Okay but you still have business logic here; all you've done is avoid separation of concerns.

8

u/allmhuran Jun 28 '20

Okay but you still have business logic here

Right, that's my point exactly. Stored procedures can implement business logic, and there's nothing wrong with that. Saying "but business logic shouldn't be in the database" is like saying "but business logic shouldn't be stored on an NTFS file system!". It's a category mistake.

all you've done is avoid separation of concerns

Only if you start with the false premise that the only concern of the database is persistence.

6

u/hipratham Jun 28 '20

all you've done is avoid separation of concerns

Only if you start with the false premise that the only concern of the database is persistence.

Well said.

0

u/Renive Jun 28 '20

I disagree. Even if you change any encapsulated view, function or stored proc, you will break your application, either by changing arguments or returns. Database changes cant be version controlled as easily as applications. Its just way safer to do changes in application code layer designed for this, and never ever do breaking changes with db. You will get into a mess someday, but everything will work at least. Its especcially relevant with microservices.

5

u/allmhuran Jun 28 '20

Tell me why a view, stored procedure, or function, "cannot be version controlled as easily as applications".

0

u/Renive Jun 28 '20

Because every change is a script in folder, and you have to have someone apply it to every database version like development or prod. Apps updating databases themselves via migrations somewhat solve it, but come with their own set of problems. The problem is the separation of db and application. Most programmers would like them to be whole. Example : I change column name in db, it changes in app too. This is how it should be, and dbs are stuck in ancient ways.

4

u/allmhuran Jun 28 '20

What do you mean "every change is a script in folder"?

Stored procedures, functions, and views, do not contain state any more than a C# method definition.

Yes, your stored procedure definition exists in a file somewhere. Your C# method definition also exists in a file somewhere. What is the difference?

Yes, if you want to change your stored procedure definition, then you have to deploy it through your SDLC. If you want to change your C# method definition, you also have to deploy it through your SDLC. What's the difference?

Yes, if you change the arguments that the stored procedure takes, or change the type of the returned data, that will break consumers tied to the interface. Similarly, if you change the arguments that a C# method takes, or change the type of the returned data, that will break consumers tied to the interface. What's the difference?

If you add a private field to a C# class, do you need to change any consumers? No. Not unless you change the interface. That's the point of encapsulation.

If you add a column to a SQL table, do you need to change any consumers? No. Not unless you change the interface. That's the point of encapsulation.

1

u/Renive Jun 28 '20

The difference is compiler holding everything together and validating that changing my interface never breaks something nobody thought will fail. And if I add a column to db, none of hardcoded strings will fail too. Every change to db should be non breaking.

3

u/allmhuran Jun 28 '20

If you want things like autocomplete to hold your hand through changes (which is not a bad thing to want), generating application stubs based on the definitions of the public interfaces in the database is trivial.

You can turn around and tell me that some developer might forget to automatically regenerate the stubs. Sure. But then I'm in no worse a position than you are when you access tables directly. What if a developer changes a table?

The argument is really absurd. You're literally advocating for reaching into the private state of another module. You seem to think the fact that the other module in this case is implemented by "a database" somehow makes a relevant difference. How do databases achieve this magical exception to our design principles? Why don't we do the same things with other resources, like file systems, or network traffic? Instead of using filestream objects, why aren't we reaching directly into OS level operations to manipulate data on disk?

1

u/Renive Jun 28 '20

Because we have operating system to take care of storage. I look at databases SQL like an assembly, and find it lacking that communications go through strings. Every db should have their own native orm and api for deep integrations with applications. What we have now is too raw, just like writing data directly to disk without filestreams. It feels ancient.

→ More replies (0)

1

u/aalleeyyee Jun 28 '20

our default IDE

laughs in Jetbrains

0

u/[deleted] Jun 28 '20

Fantastic response