Skip to main content
Tweeted twitter.com/#!/StackProgrammer/status/645076988034699264

This question is somewhat language-agnostic, but not completely, since OOPObject Oriented Programming (OOP) is different in, for example, Java, which doesn't have first-class functions, than it is in Python.

In other words, I feel less guilty for creating unnecessary classes in a language like Java, but I feel like there might be a better way in the less boilerplateyboilerplate-y languages like Python.

Is there a fourth, better, approach? If not, which one of these apporachesapproaches would be better, and why? Is there something I'm missing?

This question is somewhat language-agnostic, but not completely, since OOP is different in, for example, Java, which doesn't have first-class functions, than it is in Python.

In other words, I feel less guilty for creating unnecessary classes in a language like Java, but I feel like there might be a better way in the less boilerplatey languages like Python.

Is there a fourth, better, approach? If not, which one of these apporaches would be better, and why? Is there something I'm missing?

This question is somewhat language-agnostic, but not completely, since Object Oriented Programming (OOP) is different in, for example, Java, which doesn't have first-class functions, than it is in Python.

In other words, I feel less guilty for creating unnecessary classes in a language like Java, but I feel like there might be a better way in the less boilerplate-y languages like Python.

Is there a fourth, better, approach? If not, which one of these approaches would be better, and why? Is there something I'm missing?

Question Protected by gnat

This question is somewhat language agnostic-agnostic, but not completely, since OOP is different in, for example, JavaJava, which doesn't have first-class functions, than it is in PythonPython.
In

In other words, I feel less guilty for creating unnecessary classes in a language like Java, but I feel like there might be a better way in the less boilerplatey languages like Python.

My program needs to do a relatively complex operation a number of times. That operation requires a lot of "bookkeeping", has to create and delete some temporary files, etc.
That's

That's why it also needs to call a lot of other "suboperations" - putting everything into one huge method isn't very nice/modular/readable, modular, readable, etc.

class Thing:
    
    def __init__(self, var1, var2):
        self.var1 = var1
        self.var2 = var2
        self.var3 = []

    def the_public_method(self, param1, param2):
        self.var4 = param1
        self.var5 = param2 
        self.var6 = param1 + param2 * self.var1
        self.__suboperation1()
        self.__suboperation2()
        self.__suboperation3()


    def __suboperation1(self):
        # doDo something with self.var1, self.var2, self.var6
        # doDo something with the result and self.var3
        # self.var7 = something
        # ...
        self.__suboperation4()
        self.__suboperation5()
        # ...

    def suboperation2(self):
        # usesUses self.var1 and self.var3

#    ...
#    etc.

The problem I see with this approach is that the state of this class makes sense only internally, and it can't do anything with its instances except call their only public method.

# makeMake a thing object
thing = Thing(1,2) 

# callCall the only method you can call
thing.the_public_method(3,4) 

# youYou don't need thing anymore
 

The problem I see with this is that I have to pass a lot of variables between functions.
Also Also, the functions would be closely related to each other, but they wouldn't be grouped together.

This question is somewhat language agnostic, but not completely, since OOP is different in, for example, Java, which doesn't have first-class functions, than it is in Python.
In other words, I feel less guilty for creating unnecessary classes in a language like Java, but feel like there might be a better way in the less boilerplatey languages like Python.

My program needs to do a relatively complex operation a number of times. That operation requires a lot of "bookkeeping", has to create and delete some temporary files etc.
That's why it also needs to call a lot of other "suboperations" - putting everything into one huge method isn't very nice/modular/readable etc.

class Thing:
    
    def __init__(self, var1, var2):
        self.var1 = var1
        self.var2 = var2
        self.var3 = []

    def the_public_method(self, param1, param2):
        self.var4 = param1
        self.var5 = param2 
        self.var6 = param1 + param2 * self.var1
        self.__suboperation1()
        self.__suboperation2()
        self.__suboperation3()


    def __suboperation1(self):
        # do something with self.var1, self.var2, self.var6
        # do something with the result and self.var3
        # self.var7 = something
        # ...
        self.__suboperation4()
        self.__suboperation5()
        # ...

    def suboperation2(self):
        # uses self.var1 and self.var3

#    ...
#    etc.

The problem I see with this approach is that the state of this class makes sense only internally, and can't do anything with its instances except call their only public method.

# make a thing object
thing = Thing(1,2) 

# call the only method you can call
thing.the_public_method(3,4) 

# you don't need thing anymore
 

The problem I see with this is that I have to pass a lot of variables between functions.
Also, the functions would be closely related to each other, but wouldn't be grouped together.

This question is somewhat language-agnostic, but not completely, since OOP is different in, for example, Java, which doesn't have first-class functions, than it is in Python.

In other words, I feel less guilty for creating unnecessary classes in a language like Java, but I feel like there might be a better way in the less boilerplatey languages like Python.

My program needs to do a relatively complex operation a number of times. That operation requires a lot of "bookkeeping", has to create and delete some temporary files, etc.

That's why it also needs to call a lot of other "suboperations" - putting everything into one huge method isn't very nice, modular, readable, etc.

class Thing:

    def __init__(self, var1, var2):
        self.var1 = var1
        self.var2 = var2
        self.var3 = []

    def the_public_method(self, param1, param2):
        self.var4 = param1
        self.var5 = param2
        self.var6 = param1 + param2 * self.var1
        self.__suboperation1()
        self.__suboperation2()
        self.__suboperation3()


    def __suboperation1(self):
        # Do something with self.var1, self.var2, self.var6
        # Do something with the result and self.var3
        # self.var7 = something
        # ...
        self.__suboperation4()
        self.__suboperation5()
        # ...

    def suboperation2(self):
        # Uses self.var1 and self.var3

#    ...
#    etc.

The problem I see with this approach is that the state of this class makes sense only internally, and it can't do anything with its instances except call their only public method.

# Make a thing object
thing = Thing(1,2)

# Call the only method you can call
thing.the_public_method(3,4)

# You don't need thing anymore

The problem I see with this is that I have to pass a lot of variables between functions. Also, the functions would be closely related to each other, but they wouldn't be grouped together.

added 14 characters in body
Source Link
iCanLearn
  • 1.3k
  • 3
  • 13
  • 17
class Thing:
    
    def __init__(self, var1, var2):
        self.var1 = var1
        self.var2 = var2
        self.var3 = []

    def the_public_method(self, param1, param2):
        self.var4 = param1
        self.var5 = param2 
        self.var6 = param1 + param2 * self.var1
        self.__suboperation1()
        self.__suboperation2()
        self.__suboperation3()


    def __suboperation1(self):
        # do something with self.var1, self.var2, self.var6
        # do something with the result and self.var3
        # self.var7 = something
        # ...
        self.__suboperation4()
        self.__suboperation5()
        # ...

    def suboperation2(self):
        # uses self.var1 and self.var3

#    ...
#    etc.
class Thing:
    
    def __init__(self, var1, var2):
        self.var1 = var1
        self.var2 = var2
        self.var3 = []

    def the_public_method(param1, param2):
        self.var4 = param1
        self.var5 = param2 
        self.var6 = param1 + param2 * self.var1
        self.__suboperation1()
        self.__suboperation2()
        self.__suboperation3()


    def __suboperation1():
        # do something with self.var1, self.var2, self.var6
        # do something with the result and self.var3
        # self.var7 = something
        # ...
        self.__suboperation4()
        self.__suboperation5()
        # ...

    def suboperation2():
        # uses self.var1 and self.var3

#    ...
#    etc.
class Thing:
    
    def __init__(self, var1, var2):
        self.var1 = var1
        self.var2 = var2
        self.var3 = []

    def the_public_method(self, param1, param2):
        self.var4 = param1
        self.var5 = param2 
        self.var6 = param1 + param2 * self.var1
        self.__suboperation1()
        self.__suboperation2()
        self.__suboperation3()


    def __suboperation1(self):
        # do something with self.var1, self.var2, self.var6
        # do something with the result and self.var3
        # self.var7 = something
        # ...
        self.__suboperation4()
        self.__suboperation5()
        # ...

    def suboperation2(self):
        # uses self.var1 and self.var3

#    ...
#    etc.
added 40 characters in body
Source Link
iCanLearn
  • 1.3k
  • 3
  • 13
  • 17
Loading
Source Link
iCanLearn
  • 1.3k
  • 3
  • 13
  • 17
Loading