The language is PHP. I have several functions that relate to each other (and sometimes use each other) that I've decided to couple under the same class.
Here are my reasonings, do keep in mind that I'm limited by what I don't yet know in terms of how my code will be used:
These functions have no clear need for a class on their own.
These functions I only need as small bits for random events throughout my other codebase.
These functions I see as a "package of methods that relate to a certain scope", e.g functions that deal with file functionalities (write / read / append, etc.)
These functions help me avoid over-Dependency Injecting; while the classes that use these methods cannot work without these methods, as such, they're depdendent on these methods, it makes no sense to DI the class that contains these methods because it over-complicates code that should be simple.
These functions are a core part that should never be touched, they're provided to you "as is". Static methods cannot be overwritten and this is a plus in my case. Under no circumstance do I want developers to meddle with them.
It is more elegant to write
use Package;
thanrequire_once
for every file, using Composer will leave the functions out in the global scope which I don't want.
And here's my actual code, just stripped out of functionality:
class FilesHelpers
{
public static function saveFileToDisk(){}
public static function saveExportFile(){}
public static function getFilePath(){}
}
As you can see, these functions all deal with files, but as stated previously, they rarely touch each other and are generally able to stand on their feet alone, without needing other functions.
I simply say use FilesHelpers
and call whatever method I need.
I want these functions to be readily accessible, but I thought that's not a great idea since I don't really need them except for the places where I need them, while making them static doesn't help me with this, in my mind, a set of static functions that don't care about each other in a class are more "decouples" than non-static functions that require object instantiation.
If I were to put it in other words: I don't want to have the classes that use these functions have dependencies because they can easily be swapped, if you use my class, you use these methods, no discussion, if you want to do the process in some other way, fine, you have the main interface that you can write to and you don't need to use my static functions.
Is this the right approach?
use FilesHelpers;
?File
was a crap decision as we then end up with static methods that have side effects, making testing harder and less robust.IFile
, with an implementation of a non-staticFile
class would have been a much better design choice. Using other people's mistakes as justification to make the same mistake yourself is deeply twisted logic.