Python Forum
too many methods in class - redesign idea? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: too many methods in class - redesign idea? (/thread-32772.html)



too many methods in class - redesign idea? - Phaze90 - Mar-04-2021

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?


RE: too many methods in class - redesign idea? - Larz60+ - Mar-04-2021

show your code so we can help


RE: too many methods in class - redesign idea? - Phaze90 - Mar-05-2021

i can't - it's protected


RE: too many methods in class - redesign idea? - deanhystad - Mar-05-2021

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.