OBJECTS AND DATA STRUCTURES
CLEAN CODE
DATA ABSTRACTION
Private variables
Why getters and setters?
Both represent the data of a point on the Cartesian plane:
One exposes its implementation
One hides it
Abstraction: Allow users to manipulate the essence of the data, without having to know its implementation
DATA ABSTRACTION
The first uses concrete terms to communicate the fuel level of a vehicle
The second does so with the abstraction of percentage.
We don’t want to expose details of our data.
DATA/OBJECT ANTI-SYMMETRY
Objects hide their data behind abstractions and expose functions that operate on
that data.
Data structure expose their data and have no meaningful functions.
Geometry class operates on the three shape classes.
The shape classes are simple data structures without any behavior.
If a perimeter() function were added to Geomtry:
The shape classes would be unaffected.
Any other classes that depended upon the shapes would also be unaffected.
If I add a new shape, I must change all the functions in Geometry to deal with it.
DATA/OBJECT ANTI-SYMMETRY
If I add a new shape, none of the existing functions are affected.
If I add a new function all of the shapes must be changed.
Procedural code (code using data structures) makes it easy to add new functions
without changing the existing data structures. OO code, on the other hand, makes it
easy to add new classes without changing existing functions.
The complement is also true:
Procedural code makes it hard to add new data structures because all the
functions must change. OO code makes it hard to add new functions because
all the classes must change.
THE LAW OF DEMETER
A module should not know about the innards of the objects it manipulates.
A method f of a class C should only call the methods of these:
C
An object created by f
An object passed as an argument to f
An object held in an instance variable of C
final String outputDir = [Link]().getScratchDir().getAbsolutePath();
TRAIN WRECKS
Considered to be sloppy and should be avoided.
Options opts = [Link]();
File scratchDir = [Link]();
final String outputDir = [Link]();
final String outputDir = [Link];
HYBRIDS
Half object and half data structure.
Hard to add new functions.
Hard to add new data structures.
Avoid creating them
HIDING STRUCTURE
[Link]()
Or
[Link]().getAbsolutePath()
String outFile = outputDir + "/" + [Link]('.', '/') + ".class";
FileOutputStream fout = new FileOutputStream(outFile);
BufferedOutputStream bos = new BufferedOutputStream(fout);
BufferedOutputStream bos = [Link](classFileName);
DATA TRANSFER OBJECTS
Data structure is a class with public variables and no
functions.
DTO.
Very useful structures, especially when communicating with
databases.
More common is the “bean” form:
Have private variables manipulated by getters and setters.
ACTIVE RECORD
Data structures with public variables.
Typically have navigational methods like save and find.
Direct translations from database tables.
Treat it as though they were objects by putting business rule methods in them.
Treat it as a data structure and to create separate objects that contain the business rules and that hide their internal
data.
CONCLUSION
Objects:
Expose behavior and hide data.
Easy to add new kinds of objects without changing existing behaviors.
Hard to add new behaviors to existing objects.
Data structures
Expose data and have no significant behavior.
Easy to add new behaviors to existing data structures.
Hard to add new data structures to existing functions.