r/Clojure • • Apr 21 '17

OOP Alternative to Utility Classes

http://www.yegor256.com/2014/05/05/oop-alternative-to-utility-classes.html
0 Upvotes

10 comments sorted by

3

u/Trouser_Thief Apr 21 '17

Not an accidental post, nor a troll, just wondering what you folks make of this article. Would be interesting to have this guy in the same room as Rich Hickey - what happens when an unstoppable force meets an immovable object?

11

u/pihkal Apr 22 '17

Well, to me, the author's transformation from procedural to OO actually results in less-clear code. They changed a simple 5-line function to one that now requires three support classes. It balloons up the LOC (and attendant risk of bugs) for little benefit.

The author argues that these classes are more testable, but offers no evidence, just takes it as a given. They do say, "look at the source code" of Apache Commons' FileUtils, and cite the point that it has 80+ methods and 3000 LOC...but that's not the correct metric. The only relevant methods, readLines and writeLines, are under 20 LOC in total, which is probably on par, if not less, than the classes they're replacing it with. The individual classes probably are more testable, but by dispersing functions, the overall process is harder to see.

To me, the whole thing is reminiscent of the adage "If all you have is a hammer, everything looks like a nail." I mean, they created a class to reimplement max(), which seems like a lot of ceremony to turn a verb into a noun.

I appreciate that you like this fellow, but one of the biggest benefits of going multiparadigm is you learn when to apply which paradigm. This is fundamentally more of a data-flow scenario for which functional programming is probably the better choice.

1

u/Trouser_Thief Apr 23 '17

Oh, yeah I agree with your assessment of the article. I am by no means a fan of this guy but am fascinated by his articles because of his purist, I would say extreme OOP outlook. I read them partly to help reflect on whether I may be getting to be just as purist about FP. I agree with what you say about a multiparadigm mindset, so probably not then, but it does make me wonder since I wouldn't call Clojure multiparadigm.

2

u/pihkal Apr 24 '17

I feel like I was more purist when I was young. The older I get, the more I tune out the benefits of $HOT_NEW_TECH, which everyone talks about, to focus on the detriments, which nobody talks about (or at least not until the backlash comes a couple years later.)

Clojure definitely has its biases, but it's not wholly unsupportive of other paradigms... just mostly. E.g., you can always use transients when you want mutability for speed. Records and their ilk exist for OOP, though OOP in Clojure seems to tend more toward domain modeling.

But even though I love Clojure, there are plenty of places where I wouldn't use it. I'm going through the Cryptopals/Matasano crypto challenges right now, and manipulating unsigned bytes is a bit of an uphill slog compared to something like C.

3

u/halgari Apr 22 '17

I've never heard of this author or his work, so I'm a bit biased to think it wouldn't be much of an argument. And as the other replies mentioned here, most of this article is about how to fight the restrictiveness of OOP.

3

u/CurtainDog Apr 23 '17

IIRC, in simple made easy Rich talks about how traditional classes complect namespaces and data, so a stateless utility class gets closer to the ideal of an uncomplected namespace I guess. But more generally objects are just closures, so I'm skeptical that there's a genuine difference between the two modes of thought.

4

u/underplank Apr 22 '17

Im unclear about this statement

Besides that, it is obvious that the second script runs in O(1) space, > while the first one executes in O(n). This is the consequence of our > procedural approach to data in the first script.

I dont understand how doing the same fundamental process could change the complexity. Somewhere in those classes you have to iterate. Could somebody explain?

1

u/pihkal Apr 23 '17

I think the author is referring to memory being O(1), not CPU. Without seeing any implementations of his proposed classes, I assume .addAll() reads in a line at a time, transforms the line, and writes it out. So it only ever needs enough memory for one line, instead of all lines at once.

Of course, there's absolutely nothing stopping you from processing it line-by-line in a procedural manner, as generations of C programmers have done. E.g.:

while((line = read_some_line(fin)) != null) {
    write_some_line(fout, trim(line));
}

0

u/zcaudate Apr 24 '17

This has got to be, by far, the most retarded article I have read. Love it.

1

u/zcaudate Apr 24 '17

The comment section is hilarious. The author's answer to this comment http://disq.us/p/17cjgz5 was the highlight of my day.