0

I have a large set of objects, and I need to be able to do some complex stuff with each object, so I have some fairly long functions.

In general, is it better to put the long functions in the class that they'll actually be used in (GreatObject, below) for proper encapsulation, or is it better for efficiency to put one function in the collection class (GreatSet, which will only ever have one instance)?

class GreatSet(object):
    def __init__(self):
        self.great_set = [] # Will contain a lot of GreatObjects.

    def long_method(self, great_object): # Is this function better here?
        [Many lines of code]

class GreatObject(object):
    def __init__(self, params):
        self.params = params

    def.long_method(self): # Or here?
        [Many lines of code]

I'm using Python 2.7.

4
  • In general, it belongs with the object. How may objects will be instantiated at a given time? How long is long? Long method is a code smell. Perhaps you should consider refactoring. Then, if still you are concerned about cost of instantiation do some testing of different implementations. Commented Jan 12, 2017 at 13:09
  • There could be up to 400 objects. There are a few methods—let's say altogether they are 300 lines of code. I'm not sure how Python handles this sort of thing, but that could be the equivalent of 120000 LOC, right? Commented Jan 12, 2017 at 13:36
  • 1
    @PProteus totally wrong - the code (the executable bytecode compiled from your sources) is of course shared between objects, you don't have one instance of the "long_method" function's code for each instance xD Commented Jan 12, 2017 at 13:46
  • Thank you, Bruno. That seems like the better way to do it, but I have practically no knowledge of that level of the language. Commented Jan 12, 2017 at 13:47

1 Answer 1

3

in both cases, long_method will belong to it's class (there will be a single long_method function per class, shared by all instances), and in both cases looking up obj.long_method will create a new Method instance for each lookup, so wrt/ "efficiency" (whatever it's supposed to mean) it won't make any difference. Also, unless you need maximum time and space performances - in which case a lower level language might be a best choice - you should really feel more concerned with proper design and maintainability than with maximum raw performances.

So, if long_method is supposed to work on GreatObject it might belong to GreatObject, but it depends on the respective responsabilities of those classes, what long_method really do, and which application layers long_method, GreatObject and GreatSet belong to. If for example GreatObject and GreatSet both belong to the domain model and long_method do presentation-related stuff then obviously long_method belongs neither in GreatObject nor GreatSet.

Finally, as PartialOrder mentions in his comment, "long" functions are most often a code / design smell. Sometimes a function has to be "long enough" because it has do something complex - and even then you can usually refactor it into smaller functions (eventually into methods of a distinct class if those functions need to share state) -, but quite often a long function means it's just doing too many things.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.