I have been reading Robert C. Martin s (aka Uncle Bob) books very intensely as of late. I have found a lot of the things he talks about to be real life savers for me (small function size, very descriptive names for things, etc).
The one problem I haven t gotten past yet is code coupling. One problem I keep having over and over is that I will create a object, like something that wraps an array for instance. I do will some work on it in one class, but then have to call another class to do work on it in a different class. To the point where I m passing the same data 3-4 levels deep, and that just doesn t seem to make sense because it s hard to keep tract of all the places this object is getting passed, so when it s time to change it I have a ton of dependencies. This seems like anything but a good practice.
I was wondering if anyone knew of a better way to deal with this, it seems that Bob s advice (though it s probably me misunderstanding it) seems to make it worse because it has me creating lots more classes. Thanks ahead of time.
EDIT: By Request a real world example (yes I totally agree that it is hard to make sense of otherwise):
class ChartProgram () {
int lastItems [];
void main () {
lastItems = getLast10ItemsSold();
Chart myChart = new Chart(lastItems);
}
}
class Chart () {
int lastItems[];
Chart(int lastItems[]) {
this.lastItems = lastItems;
ChartCalulations cc = new ChartCalculations(this.lastItems);
this.lastItems = cc.getAverage();
}
class ChartCalculations {
int lastItems[];
ChartCalculations (int lastItems[]){
this.lastItems = lastItems;
// Okay so at this point I have had to forward this value 3 times and this is
// a simple example. It just seems to make the code very brittle
}
getAverage() {
// do stuff here
}
}