Cleaner code: Tuskfish refactored
22 July 2018 | 135 views
I came across an excellent article that convinced me that using static methods is a bad idea. The first iteration of Tuskfish used static methods for anything that didn't seem to need an instance. It made for fast and convenient development, but as I discovered while trying to remove all the static stuff, it makes changing things really difficult. In particular, if you have static methods that have several static dependencies of their own unravelling it gets pretty painful. But the static methods are all gone now and Tuskfish uses non-static methods for everything, except session management.
Dependencies are now injected instead of reaching for them statically. Once sorted out, this made further changes easier, but dependency injection also highlighted areas where the structure of the application was not as clean as it could be, forcing me to rethink and rearrange the class design to separate the responsibilities better. So it turned out to be both more useful and far more work than I expected.
The obnoxious side-effect of dependency injection is that your constructors have more arguments, which can get pretty annoying for classes that you instantiate frequently. However, this problem goes away if you use factory classes to instantiate them.
The last major change was to modularise the structure. A 'modules' directory was added to the trust_path, and the base content management functionality has been largely partitioned off into /modules/content. The structure of this module can be replicated to create other discrete modules, allowing them to be installed or updated without disturbing the rest of the file structure. An 'example module' has been developed to give guidance on the required file structure, to faciliate rapid development of others.
The net effect is that the internal structure of Tuskfish is now much cleaner and hackable, although absolutely no new functionality has been added. I need to revise the code documentation, API reference and guides, and test everything. When it's done the result will be released as Tuskfish V1.1.