One of the things that has confused me when studying best software engineering practices is how to handle methods in classes that don't alter or otherwise interact with the state.
In this article I like how the author discusses the separation of data and logic. But in trying to apply this in practice, I have cases where I have an odd-out function outside the class that doesn't change the state of the object but isn't really relevant outside the object.
For example:
def inspect_color(color):
if color == 'brown':
return 'Similar to chocolate'
class Dog(object):
def __init__(self, color):
self.color = color
def play_in_mud(self):
self.color = 'brown'
results = inspect_color(self.color)
vs.
class Dog(object):
def __init__(self, color):
self.color = color
def play_in_mud(self):
self.color = 'brown'
results = self.inspect_color()
def inspect_color(self):
if self.color == 'brown':
return 'Similar to chocolate'
Is there a general software engineering principle or best practice (from any reasonable paradigm) that suggests which of these approaches should be followed? For multiple reasons my intuition tells me that the top one is better, except doing this: inspect_color(self.color)
bothers me, so I am unsure. I did some reading but didn't find a clear answer on best practices for cases like these.