[SUMMARY] Clean Code Chapter 6: Objects and Data Structures (Robert C. Martin)
In this Chapter Uncle Bob talks about Objects and Data Structures :
Objects and Object Oriented code
Data Structures and Procedural code
Active Record
Special form of DTOs created by your db querying tool. They have some logic like find and save.
Do not put business rules in them, you should treat the Active Record as a data structure and create separate objects with business logic and hide the internal data.
Hybrids
If you are not careful you will create hybrids.
Hybrids are classes that acts both as Data Structures (having public accessors and mutators) and Objects (functions that do significant things)
They are the worst of both worlds
The Law of Demeter
Recommended by LinkedIn
A module should not know about the innards of the objects it manipulates.
A method f of a class C should only call
Not following the Law of Demeter => train wrecks (long chain of methods called one after the other)
Tell don't ask
When you ask an object for its implementation, you'll risk to violate the Low of Demeter and create train wrecks.
Objects are here to hide the implementation details, to do that you have to tell them what to do, not ask them for the tools to do it yourself. That's the beauty of abstraction.
Conclusion
You should use both Data Structures (direct access to data, no logic) and Objects (logic, abstraction to access data) !
Be sure to tell your Objects what to do without asking for the internals
Be sure to ask the internals of your Data Structures, they should not do anything
The decision to use one or the other has to be made on a case by case basis by considering how the system might evolve!
For more details and examples, I highly recommend reading the book!
The summaries of the previous chapters are available in my previous posts :)
why is it hard to add new functionality and add new data structure in hybrids?