Last week I was doing some pairing programming with a colleague of mine, and we were designing an API client class and at one point inside a method I had this piece of code:
array('deliveryAddress' => $this->getContainer()->getParameter('client.subscribe.deliveryAddress'))
And I suggested we should modify these lines like this:
return $soapClient->Subscribe(array('deliveryAddress' => $this->getCompanyDeliveryAddress()));
“Why would you that? It would be only a wrapper method for the container getParameterMethod()” my colleague asked me.
So the question is: does it worth to create a wrapper method for only one line of code?
I think it depends on how you choose to structure your code, but I usually do for these reasons:
getCompanyAddress() it’s way more readable than getContainer()->getParameter(‘client.subscribe.deliveryAddress’). Yes, for you it might be the same, but think about future programmers who will work on your code. When we are reading code lines, we are doing this with the sole purpose of understanding what it does. A getCompanyAddress will give the exact answer without reading and trying to understand what a bunch of lines do.
2. Avoiding comments
I think if you need comments above a line of code, then your code is not self-explanatory, and it’s often a hint that you should create a new method with a good naming. I like to think my wrapper methods like “working comments”, because they are like a comment which is executing and does stuff.
So as a rule of thumb when I feel the need to comment code I ask myself if method with a descriptive name should be added instead.
3. Better debugging and fixing code
I think for a new programmer it will be easier for him to find where the problem is when a bug is occurring. And if he needs to fix it then he will be sure that if he will fix that method the code is working.
What do you think? Are wrapper methods a waste of time and space or do you make use of them?