4 ms·
I disagree with the anemic object argument. If an object is just there to store data and no behaviour, then that's fine - don't add behaviour if it doesn't need
by Cthulhu_ 10y ago
I disagree with the anemic object argument. If an object is just there to store data and no behaviour, then that's fine - don't add behaviour if it doesn't need it. A large portion of back-end services are CRUD and data wrangling operations anyway - as in, convert data format A to data format B (which I guess could be a constructor or factory method if you're comfortable with having the conversion logic in a data class).
- tantalor 10y agoEspecially true if your business objects are generated code, e.g., protocol buffers. Combining business logic with business objects is a mistake. That's a textbook example of tight coupling.
- jbattle 10y ago> Combining business logic with business objects is a mistake. That's a textbook example of tight coupling. Isn't that a textbook example of object oriented programming? Whether OOP is a mistake in and of itself is another question ...
- tantalor 10y agoTextbook examples of object oriented programming are notoriously bad. Stuff like, Dog dog = new Dog("Spot"); dog.bark(); These are toy examples meant to quickly introduce the mechanics of objects, not teach good software engineering patterns.
- mattmanser 10y agoWhere else are the business rules of an object supposed to go? Textbook? It's what you're supposed to do! And if you say in a bloody "service", which just has the object passed as the first variable into every bloody method, I swear I'll come down there and strangle you with your own keyboard cord. That's the actual textbook definition of tight coupling. I'm working on a project which has 5 layers, business objects with no methods, a dto layer, a dal layer, a service layer and finally the website. All incredibly tightly coupled and utterly pointless. It's an utter nightmare. Every time I touch the code half of it disappears, right now I've still deleted more lines than I've added while adding a ton of functionality. That idea is so broken because it violates KISS, YAGNI and DRY all in one in a futile attempt to "decouple" things which are obviously coupled because the data is needed to perform the business rules. There's a malaise in modern programming and it's the dogmatic pursuit of decoupling over simple and clear code.
- clifanatic 10y ago> If an object is just there to store data and no behavior Then why do you have it at all?
- Bognar 10y agoTo define a valid shape for related data.
- sidlls 10y agoSometimes many different values are related to each other and it is useful to keep them close together for explicitness (and other reasons).
- st3v3r 10y ago... to store data?
- wtetzner 10y agoI suppose because the language doesn't have typed records. You have to simulate them with objects.
- afroisalreadyin 10y agoVery good point. I would say that if your object is doing e.g. validation, or if/then/else'ing on field values to normalize them somehow, it's already far from anemic. But the key point is that you should not put data in objects, and then put the business logic, as in the small code sample, into some routine that simply accesses fields. That's the anti-pattern.
- caconym_ 10y agoI agree, but the problem is that usually the representation of the data comes before the logic you need around it, which can accumulate over a period of months or years. Depending on the application, depending on the programmer(s), that logic can turn into a real mess since there's no obvious place for it to live. This reduces code reuse, which leads to bugs. It's not always appropriate, but building some language-idiomatic encapsulation around data from the very start makes it much less likely that the inevitable addition of hundreds or thousands of lines of logic will descend into incomprehensible spaghetti hell. This doesn't have to be OOP; it could just as easily be e.g. a module in a purely functional language.