3 ms·
OO doesn't prevent the use of pure function class. You can group all your common util functions together into the util classes. OO really isn't dogmatic in fo
by ww520 14y ago
OO doesn't prevent the use of pure function class. You can group all your common util functions together into the util classes.
OO really isn't dogmatic in forcing one style or the other.
- MatthewPhillips 14y agoUtility classes are code smell (paradigm smell, even). They exist where the OO model breaks down and you want to get-shit-done rather than spend an hour thinking about where you can stick function X so that it's useable by class Y and Z. Same goes for verb classes; you know, those does-the-thing classes that you create which are just objects wrapped around a single function called execute(), run(), create(), etc. etc.
- ww520 14y agoUtility classes serve merely as organization purpose. It has nothing to do with OO. It's like putting related files in a directory, or putting related Python functions in a module. I don't get how "stick function X so that it's useable by class Y and Z" is relevant for consideration. As the name suggested, utility classes are function libraries that can be used by all.
- wonderzombie 14y agoThe problem is that what you "really" want when you're doing this is a plain old namespace. In some languages, objects necessarily come with a bunch of baggage and implications you don't want without some way to opt out. Yeah it works fine most of the time, but it's a bit like the opposite of primitive obsession. You wouldn't use an Integer class for an index in a for loop. In Java you have little choice but to dump it all in one place. Contrariwise, Python offers you more options; you can just make a module and put a bunch of top-level methods in there if you want. Go has some notion of "objects" but you can have methods or top-level functions in a given package. And so on.
- ww520 14y agoNo, the problem is not to use a namespace. The problem is I want to organize related functions as a group. Namespace is just one way to do it. There are other ways. Package, class, namespace, or module serve the same function in term of organizing functions. What are the bunch of baggage and implication to use class as a way to organize functions? I don't see the difference in usage you mentioned with Python module. Putting a bunch of "top-level" methods in a module is the same as putting a bunch of static methods in a class.
- pjmlp 14y ago> You wouldn't use an Integer class for an index in a for loop Why is this bad in Java, while in Python where all types are objects it is ok?
- artsrc 14y agoIt is possible to do separate functions form data in OO languages. It is less convenient than it is on other languages, where the wrapper class is not needed. Separating functions from data is not the natural initial solution in an OO language, and is done as a refinement. It is not promoted as a default solution. At some point the language is getting in the way and it better to go with something that supports what you are doing.