Chapter 6: Objects and Data structures [Slide]
- We keep our variables private to prevent unintended dependency on them, so that we can change implementation.
- If you expose your implementation detail to user, the chance of mistake and bug will raise
Procedural code is easy to add new functions without changing existing data structures Object Oriented code is easy to add new classes without changing existing functions
- Procedural code is hard to add new data structures OO code is hard to add new functions (because all classes must change)
- Procedural code is complementory of OO code
a module should not know about the innards of the objects it manipulates
Below code is violating the law of demeter:
final String outputDir = ctxt.getOptions().getScratchDir().getAbsolutePath();
Calling multiple functions after returning object, one after another is called Train Wrecks.
This would violate the law of demeter.
If above code was returning only data structure instead of object, it wouldn't violate the law of demeter.
final String outputDir = ctxt.options.scratchDir.absolutePath;
having functions, mutators, accessors for public and private variables makes a Hybrid code.
It will makes it hard to add new functions and data structures.
result of law of demeter is:
BufferedOutputStream bos = ctxt.createScratchFileStream(classFileName);
a data structure with public variables and no functions.
private variables manipulated by getters and setters.
Special form of DTO. data structure with public (or bean-accessed) variables with extra navigational methods like save and find. (example: Model class in Laravel)
- Putting business rules into these classes makes it a Hybrid structure and hard to change.