Python Forum
too many methods in class - redesign idea?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
too many methods in class - redesign idea?
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
Gribouillis likes this post
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.

Possibly Related Threads…
Thread Author Replies Views Last Post
  access share attributed among several class methods drSlump 0 260 Nov-18-2021, 03:02 PM
Last Post: drSlump
  a function common to methods of a class Skaperen 7 717 Oct-04-2021, 07:07 PM
Last Post: Skaperen
  Listing All Methods Of Associated With A Class JoeDainton123 3 1,053 May-10-2021, 01:46 AM
Last Post: deanhystad
  Special Methods in Class Nikhil 3 1,068 Mar-04-2021, 06:25 PM
Last Post: Nikhil
  cant able to make methods interact with each other in the class jagasrik 2 802 Sep-16-2020, 06:52 PM
Last Post: deanhystad
  Question about naming variables in class methods sShadowSerpent 1 970 Mar-25-2020, 04:51 PM
Last Post: ndc85430
  a class calling one of its own methods? Skaperen 1 939 Jul-21-2019, 04:43 AM
Last Post: Gribouillis
  Do objects get their own copy of the class methods? Charles1 1 1,221 Feb-02-2019, 04:40 PM
Last Post: ichabod801
  Attibute Error: Two methods in a class, one I can reach, the other not (phantom file? GaryKuipers 4 1,884 Aug-28-2018, 02:32 PM
Last Post: GaryKuipers
  How are these methods working within this class workerbee 3 1,889 Nov-29-2017, 04:15 PM
Last Post: hshivaraj

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020