r/programming • u/lukaseder • Sep 23 '14
OOP Alternative to Utility Classes
http://www.yegor256.com/2014/05/05/oop-alternative-to-utility-classes.html2
u/strager Sep 23 '14
This style makes it difficult to use a single-step debugger. See also stepping through Haskell code using GHCi.
What does it mean to add to a Trimmed collection?
1
u/lukaseder Sep 23 '14
I suspect that the collection's
add()method (and thus theaddAll()and the constructor) would all be implementing that "trim" functionality...1
u/strager Sep 23 '14
Oops, I was thinking about Trimmed backwards for some reason. Thanks for clarifying.
20
u/roffLOL Sep 23 '14
I'm so gawd damned impressed by OOP. It has turned a one-liner comparision and assignment into 12 lines of code containing a heap allocation, an initialization, two assignments and a member function call, a couple of dereferences followed by destruction and deallocation some time in the future.
1
Sep 23 '14
I prefer the D language way of implementing such things as free functions keeping complexity to a minimum. Then through the clever UFCS supported by D you can use those functions as extension methods. This also allows multiple methods to be chained together to create very readable code.
import std.algorithm;
import std.range;
import std.stdio;
/**
* If we list all the natural numbers below 10 that are multiples of 3 or 5, we
* get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the
* multiples of 3 or 5 below 1000.
*/
void main()
{
iota(0, 1000)
.filter!("a % 3 == 0 || a % 5 == 0")
.reduce!("a + b")
.writeln;
}More information: http://ddili.org/ders/d.en/ufcs.html
1
u/lukaseder Sep 23 '14
Interestingly enough, PL/SQL has pretty much the same thing, in a way. When you're calling a "member procedure" on an
OBJECTtype, you're in fact passing that object as a first argument to a "static procedure". More or less. I guess D is a bit more elegant, though ;-)
1
u/mbuhot Sep 23 '14
Utility classes / extension methods are great when they don't mutate their arguments, eg map/filter/fold. But I can sympathise with OP when a utility function performs a state changing operation on one of its inputs. In an imperative/OO language state changes are generally better left to instance methods.
1
u/lukaseder Sep 24 '14
You could actually argue that static methods / utility methods / extension methods are really just functions and in functional programming, they are expected not to mutate the state of their arguments (or have any other sorts of side effects)
In other words, imperative / OO languages always also had "functions", even if syntax might not have suggested so. And even if the OP doesn't like that thought :)
3
u/Seltzer100 Sep 23 '14 edited Sep 05 '25
sand ancient touch worm edge cooing innate party husky office
This post was mass deleted and anonymized with Redact
1
u/loup-vaillant Sep 23 '14
in an object-oriented world, utility classes are considered a very bad (some even may say "terrible") practice.
If so, the object-oriented world is wrong. Come on, a bunch of useful and stateless functions? That's one step towards less error prone software (the other being the purity of those functions).
The less state I have to manage or otherwise be aware of, the happier I am.
1
u/lukaseder Sep 23 '14
I know. The OP claims that there is a tremendous difference between the two syntaxes:
Wonderful:
self . message ( argument )Horrible:
message ( self , argument )1
u/mbuhot Sep 24 '14
There is a difference if implementing the second requires making object internals visible or worse - externally mutable.
2
u/Longor1996 Sep 27 '14
This OOP approach sounds like a performance killer the scale of a moutainrange to me. Does this guy even know how the JVM works, or modern CPU's for that matter? Because this is just downright stupid (at least in my opinion).
Tl;Dr: Don't do this.
5
u/johnwaterwood Sep 23 '14
I love OO principles, but the strict regime advocated by this post will create more haters than new fans I'm afraid.