A lot of times in code on the internet or code from my co-workers I see them creating an Object with just one method which only gets used once in the whole application. Like this:
class iOnlyHaveOneMethod{ public function theOneMethod(){ //loads and loads of code, say 100's of lines // but it only gets used once in the whole application } } if($foo){ $bar = new iOnlyHaveOneMEthod; $bar->theOneMethod(); }
Is that really better then:
if($foo){ //loads and loads of code which only gets used here and nowhere else }
?
For readability it makes sense to move the loads and loads of code away, but shouldn’t it just be in a function?
function loadsAndLoadsOfCode(){ //Loads and loads of code } if($foo){ loadsAndLoadsOfCode(); }
Is moving the code to a new object really better then just creating a function or putting the code in there directly?
To me the function part makes more sense and seems more readible then creating an object which hardly is of any use since it just holds one method.
The problem is not whether it’s in a function or an object.
The problem is that you have hundreds of lines in one blob. Whether that mass of code is in a method of an object or just a class seems more or less irrelevant to me, just being minor syntatic sugar.
What are those hundreds of lines doing? That’s the place to look to implement object oriented best practice.
If your other developers really think using an object instead of a function makes it significantly more ‘object oriented’ but having a several-hundred line function/method isn’t seen as a code smell, then I think organisationally you have some education to do.