Python Forum

Full Version: too many methods in class - redesign idea?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have one xml file (famtree.xml) that I read in, that holds many parameters about family tree data. It has many levels, root level is me, then parents, grandparents....

Now I am currently creating one class instance of that data: class myFamTree

Then I used to create (too many) class methods to return some data. get_own_parents(), get_parents_mom(), get_parents_dad(), get_parents_parents_mom(), get_parents_parents_dad(), etc.....

I feel this is getting too many methods. Is there any good practice how to clean code such scenario and avoid a superclass like here?
show your code so we can help
i can't - it's protected
Then paraphrase. Post enough to provide insight into the issues you are having.

I suspect your data has too little structure, you are ignoring the structure it has.

I would expect a family tree to be a tree of people. In addition to information like birth date and place, a People has 2 parents, X siblings and Y children. To find your grandparents you ask for your parents, and then ask for their parents. To find your uncles you ask for your parents, ask for their siblings, and filter out those that are not male. To find first cousins you get your parents, get their siblings, get their children.

To do these kinds of queries you need very few methods. Mostly you need a way to take the result of a query, filter it, and us it in another query. This is the kind of thing that relational databases are really good at. If you put your family tree data into a database you might get all the functionality you need for free. Almost no programming required.