# Clean Code - 6 Objects and Data Structures ## Data Abstraction > Hiding implementation is about abstractions! To not expose the details of data, use: - interface - getters and setters ## Data/Object Anti-Symmetry > Objects hide their data behind abstractions and expose functions that operate on that data. #### Objects and data structures are different - Objects - Data is hidden behind abstractions, and use getters and setters (expose functions) to operate data - Data structure - Data is exposed to the outside, and there are no meaningful functions #### OOP solution Polymorphism #### Procedural code (code using data structures) vs OO code | | Procedural code | OO code | |:--------------- |:----------------------------------------------------------------- |:---------------------------------------------------------- | | **Easy when..** | Add new functions (no need to change the existing data structure) | Add new classes (no need to change the existing functions) | | **Hard when..** | Add new data structure (need to change all existing functions) | Add new functions (need to change all existing classes ) | | **When to use** | Want to add new functions instead of new types | Want to add new types than new functions | > the idea that everything is an object is a myth. Sometimes you do want simple data structures with procedures operating on them. ## The Law of Demeter > a module should not know about the innards of the objects it manipulates. Another good resource to read: https://betterprogramming.pub/demeters-law-don-t-talk-to-strangers-87bb4af11694 ### Train Wrecks > Chains of calls like this are generally considered to be sloppy style and should be avoided. When `ctxt`, `Options`, and `ScratchDir` are merely data structures without behaviors, the following codes do not violate the Law of Demeter: ``` const outputDir: string = ctxt.options.scratchDir.absolutePath; ``` ### Hybrids > half object and half data structure. > > hybrids make it hard to add new functions but also make it hard to add new data structures. So should avoid using it. ### Hiding Structure When `ctxt`, `options`, and `scratchDir` are objects with real behavior: - We should ask it to do something instead of knowing how it works inside ## Data Transfer Objects Quintessential form of a data structure (data transfer object, or DTO): - A class that has public variables but does not have functions - Useful for databases communication, or parsing messages from sockets ### Active Record - Special forms of Data Transfer Objects - Are data structures with public variables - Usually have navigational methods such as `save` and `find` - The good practice is to treat Active Record as data structures ## Conclusion > Objects expose behavior and hide data; Data structures expose data and have no significant behavior. More flexible when: | | Objects | Data types and procedures | |:---------------------- |:-------:|:-------------------------:| | **Add new data types** | V | | | **Add new behaviors** | | V | ###### tags: `learn` `clean code` `test automation`