4 ms·
I like to use helper classes to organize things, it's just syntactic sugar for me, and a little easier to manage long term. I think of them like little toolkits
by ehnto 4y ago
I like to use helper classes to organize things, it's just syntactic sugar for me, and a little easier to manage long term. I think of them like little toolkits. Standard libraries in many languages are essentially delivered as helper classes, I don't think it's a nefarious anti-pattern.
The difference between `setup_environment_variables` and `Environment.setupVariables` is just ease of use, auto-complete and organization. Not some scary over-engineering creeping in. Tracking down related helper functions is far easier like this too, if I want to see what environment related helper functions I have, I just type Environment. and it shows me everything available to autocomplete, or I click/press into Environment to go see all the related code.
Anyone who's ever opened the "functions" file of a long running codebase to find 50+ free-form functions knows it's not a great way to be organized.
- eptcyka 4y agoUse modules and namespaces.
- selfmodruntime 4y agoThey are for this usecase equivalent to a static class
- robonerd 4y agoThen use them.
- marcellus23 4y agoNot every language has those.
- eptcyka 4y agoThe context here is C++. Yes, if you are bound to write Java/C/php, do whatever.
- Supermancho 4y agoMany languages actively fight against having static free functions. PHP, easy. Java, sacrifice testability and even the ability to access the functions under multiple contexts without adding MORE code, in a self-defeating exercise.
- zenexer 4y agoWhy does a language need to offer both if they’re functionally identical? Does it really matter what it’s called?
- ajuc 4y agoNamespaces and modules were invented for this purpose. The only feature that classes have over functions + modules is internal state. And it's evil. I am especially frustrated by Uncle Bob suggested refactoring of long pure function into a class with private fields. You had a function you could not call wrongly and now you have internal state, race conditions, lifecycles. Good job.
- tedmiston 4y agoEasy solution: static class.
- ehnto 4y agoFor sure, satic classes and functions are what I would use if the language has them.
- ajuc 4y agoCan still have internal state.
- ehnto 4y agoI suppose, but that's if you build it that way. A 'pure' function can also have state if you pull it in through global vars or other functions. Writing stateless code has to be deliberate no matter which method you use.
- tedmiston 4y agoHow can a purely static class have internal state if there's no instantiation of the class or instance variables? Are you referring to variables declared on the class directly? If so, that feels analogous to a function potentially having module-level state variables to me, in Python for instance.
- ehnto 4y agoWe would really have to be talking about a specific language to discuss properly I think, but there is no need for internal state. There is more advantages as well, depending on language, in that one is extensible and one isn't. Some probably see that as a con, but I have done enough work backed into tight corners to know it is better to have it than to hack around something non-extensible.