In Clean Code, we're advised to reduce, wherever possible, the number of arguments in a method's signature. This can often be achieved by using class attributes in lieu of parameters.
Now in a code review I pointed out this exact thing, because a particular method had a parameter but really this parameter was only used to pass always the same class attribute into it. So why not drop the parameter and reference the class attribute instead?
The author of the code argued that, by keeping the parameter explicit, it was easier to write unit tests for the method, because the test data could be directly passed in.
I feel like that's a bit of a code smell or design smell. I'd concede that it's easier to do testing if you can pass stuff directly to the method. But would that be so much harder than creating the object, then setting the internal fields to what you need them to be? (Or maybe there's even better techniques?)
I appreciate that there is some amount of subjectivity to this question, but there still should be an answer to the question whether or not it would be considered "best practice".
EDIT: Of course I mixed up terminology. I don't mean a class attribute. I mean a member attribute. The example would look like this
class C:
def __init__(self, some_field):
self.some_field = some_field
def do_work(self):
_helper_function(self.some_field)
def _helper_function(self, param_field):
#doing work using `param_field`
The method _helper_function
isn't part of the public interface, and from within C
only ever gets called with self.some_field
. I suggested changing that to:
class C:
def __init__(self, some_field):
self.some_field = some_field
def do_work(self):
_helper_function()
def _helper_function(self):
#do work using self.some_field