What's the cleanest and most coherent way to assign a new value to an object field when the calculation of that value is outsourced to another method?
The most obvious way would just be to update the field value when it's calculated in the method, as so: [examples in Python]
class MyClass:
def __init__(self, arg):
...
self.calc_values(arg)
...
def calc_values(self, arg):
...
self.var1 = value1
self.var2 = value2
...but I feel like this doesn't give very traceable code, as I don't like the idea of updating values 'in the background'. My preference is to do something like this:
class MyClass:
def __init__(self, arg):
...
self.var1, self.var2 = self.calc_values(arg)
...
def calc_values(self, arg):
...
return value1, value2
Although this feels wrong, since the fields could just be set directly in the object method, hence I sometimes do this:
class MyClass:
def __init__(self, arg):
...
self.var1, self.var2 = self.calc_values(arg)
...
@staticmethod
def calc_values(arg):
...
return value1, value2
Which one of these is generally preferred, or is there a better way entirely?