[Tutor] Dependencies among objects

Yigal Duppen yduppen@xs4all.nl
Fri, 26 Apr 2002 12:03:26 +0200


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

> while working on my pet project, I find it increasingly difficult to manage
> dependencies across objects.

As others have already pointed out, your solution might not be the best one. 
I can really advise the following books if you want to maintain a grasp of 
your structure:

M. Fowler, Refactoring
Gamma et al., Design Patterns

The latter explains the "best" way to design certain kinds of behaviour; it's 
a good book, and it caused a minor revolution in thinking about programs. 
However, I find it hard to always appreciate which pattern is needed for my 
choice.

The Refactoring book is in my opinion a must-have; it describes specific 
problems in designs; for every problem, it describes how this problem can be 
circumvented in a structural way, AND a step by step approach for refactoring 
your code to fit this new structre.

For example (book not at hand, so it's from the top of my hand), he describes 
the problem of "long argument lists": methods taking too many arguments.

He then reasons: if that is the case, it might very well be that many of 
these parameters are related -- put those parameters in a new class.

And he then explains how to transform the following code:

def printEmployee(firstname, surname, street, zipcode, city, country, age, 
gender):
	print firstname, surname
	print street
	print zipcode, city


into the following code


class Employee:
	def firstname():
		...

	def surname():
		...

def printEmployee(employee):
	print employee.firstname(), employee.surname()
	print street()
	print zipcode(), city()



The best part is that the resulting code often makes it easier to spot 
_other_ flaws (why have the name-combining logic in de print method?)

In short: get that book. Your programs will definitely improve.

YDD
- -- 
.sigmentation Fault
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE8ySXvLsKMuCf5EdwRAng+AJ0SxPcZO37ThPrbSdSYqH+30i4cCQCfYe/5
s1VFZWijxIzzGAvLM50+ahg=
=XyIb
-----END PGP SIGNATURE-----