News for hackers. Stuff that matters.
Re:Why OO Sucks by Joe Armstrong (2000) by kabdib on Thursday March 25, 2021 @08:03PM (#26587281)
Years ago, I maintained (tongue in cheek) that OOP was a great paradigm not because it was inherently better, but because it was so bad that you had to rewrite your code several times to make it work. And once you've written something three or four times you begin to figure out your mistakes.

Then that Gang-of-Four "Design Patterns" book became popular and really screwed things up. I swear I didn't see factories-making-factories-making-factories until that thing was published, then I couldn't go a day without encountering yet another SingletonFactoryWorkerVisitor or whatever was cool that week, ugh.

I'm joking, of course. Except about Gang-of-four. And rewriting your code.

Re:Why OO Sucks by Joe Armstrong (2000) by pwdisswordfish6 on Thursday March 25, 2021 @08:53PM (#26587659)
> I swear I didn't see factories-making-factories-making-factories until that thing was published

No? That's a pretty good description of the sort of metaprogramming that Lisp gives you, and Lispers are really enthusiastic about that kind of thing. People will dispute the parallels, but the real difference comes down to:

- non-Lisp languages being significantly less powerful

- Lisp's veneer of respectability

... which shouldn't be all that odd, considering the person who gave us "design patterns" was Dick Gabriel, a Lisper.

At least the "SingletonFactoryWorkerVisitor" programmers use a nomenclature that reflects the ontology the object is supposed to fit into.

Re:Why OO Sucks by Joe Armstrong (2000) by kabdib on Thursday March 25, 2021 @10:27PM (#26588191)
My guess is that LISP programmers are largely a self-selected group of pretty decent hackers. I'm basing this on some exposure to Lisp Machines in the 1980s, and some Common LISP stuff in the 90s. Generally adults. LISP is my favorite language I'll never ship a product in (well, okay, after SmallTalk :-) ).

The Gang-of-Four-driven stuff I saw in C++ and Visual Basic (late 90s and early 00s) still gives me the shivers. Like, "Hey, factories are cool, let's make a bunch of them for no reason because we might need the flexibility someday." Sigh.

Re:Why OO Sucks by Joe Armstrong (2000) by marcosdumay on Friday March 26, 2021 @10:47AM (#26592777)
There is an effect where some things work well on some languages, but fail completely when translated to others because of superficially unrelated features.

Compile and run time code generation seem to be one of those things. Having a mostly pure language (even when there aren't guarantees) makes them much saner.

1 - Pre-compile time generation, by its turn seems to never work very well. And if there is developer interaction between the code generation and compiling, than it's always a disaster.

Re:Why OO Sucks by Joe Armstrong (2000) by fsckboy on Friday March 26, 2021 @02:07PM (#26595095)
but lisp has homoiconicity so lisp factories create 1st class objects at runtime, including new 1st class lisp factory factories.
Re:Why OO Sucks by Joe Armstrong (2000) by inopinatus on Thursday March 25, 2021 @09:44PM (#26587946)
I think Design Patterns is often painfully misunderstood and misconstrued. My idea of using the factory pattern, for example, is not to write a MarriageFactory class, but to write a Celebrant class, and we may recognise that it embodies the factory pattern if that is helpful to the comprehension or authoring of the code.

And so it goes with the others, many of which I use extensively in my own work, but at most there's a comment at the top saying, for example "this business rules structure is in the chain-of-responsibility pattern".

That is the material use of patterns, they are not abstract frameworks. Naming things makes a difference: encouraging a taxonomy of purpose, rather than of form, goes a long way to re-orienting a programmer towards problem-solving with concretions rather than abstractions. As a consequence, Design Patterns, along with Refactoring and PoEAA, continues to serve as one my most-thumbed indexes of solution ideas.

Re:Why OO Sucks by Joe Armstrong (2000) by kragen on Thursday March 25, 2021 @09:11PM (#26587768)
Well goodness, I wrote a factory factory factory in OCaml only eight days ago:

    (* A goal that succeeds if v is one of `terms` *)
    let rec amb (terms : term list) (v : term) = match terms with 
      | [] -> null
      | term::ts -> disj (match_goal v term) (amb ts v)
    and null (s : state) = Mzero            (* a goal that always fails *)
    and ...
For those who don't speak OCaml (whose syntax is really quite unintuitive), that's a function named `amb` which takes a parameter called `terms` of type `term list` (in C++ syntax, that would be list<term>). It returns an anonymous function that takes a parameter called `v` of type `term`. This function may call `disj`, or it may return `null`, which is not a keyword, but actually another function I defined; it takes a parameter named `s`, which is of type `state`, and returns Mzero, which is at long last an honest-to-God singleton constant. (Of the parametric type `α stream`, as it happens, but let's not get distracted with that.)

So `amb` is, in object-oriented parlance, a state stream factory factory factory. Upon being passed a `term list`, it returns a factory, which, upon being passed a `term`, returns a factory, which, upon being passed a `state`, returns a `stream`. (I only traced the `null` path in this comment, but the `disj` path also returns a stream, specifically a `state stream`—OCaml's static type checking guarantees that both return paths return compatible types.)

You might think that isn't the way it's actually used, but my example code uses it in precisely that way:

    dump_stream (call_fresh (amb [Const 17; Pair(Const 20, Const 20); Const 11]) empty_state) ;;
Here `amb` is being invoked with just a `term list`. (OCaml separates list items with semicolons, so that commas always make tuples.)

So I think factory factory factories are very useful, but also I think they're really hard to keep under mental control without a functional language with a strong static type system. How on earth would you translate those four lines of code above into Java? With four inner classes, maybe, nested eight levels deep with {{}}?

If you're interested, I was translating μKanren, a Prolog-like logic-programming language originally written in 39 lines of Scheme, into OCaml: http://canonical.org/~kragen/sw/dev3/mukanren.ml

Re:Why OO Sucks by Joe Armstrong (2000) by lmm on Friday March 26, 2021 @02:00AM (#26589156)
A factory factory factory would be a bag of hidden mutable state that could construct another bag of hidden mutable state that could construct another bag of hidden mutable state that could construct another bag of hidden mutable state, making it virtually impossible to track down where anything in the final result had come from - things could come from any mutation to the final result, or some but not all mutations to any of the intermediate results.

Without the mutable state it's not a factory, it's just a function, and there's no confusion and no problem.

Re:Why OO Sucks by Joe Armstrong (2000) by kragen on Friday March 26, 2021 @01:46PM (#26594890)
I don't agree with either of the claims in your comment.

Although the GoF Abstract Factory pattern doesn't rule out having mutable state in your factory object, that is neither necessary for its purpose nor common practice when applying it in OO programming languages. The example in GoF is MotifWidgetFactory (for shitty Unix GUIs) vs. PMWidgetFactory (for shitty OS/2 GUIs), and in fact GoF says, "An application typically needs only one instance of a ConcreteFactory per product family," suggesting that they were thinking of cases where the factories don't even have immutable state such as the `terms` argument to `amb`.

They do explore a "prototype-based" factory that is stateful—but there the statefulness is just used as a way to configure the factory at startup, by building up a "product catalog" in it. Creating products doesn't modify the factory's state, and you could configure the "product catalog" just as easily by passing it in as an argument when the factory is instantiated; it's just that, in Smalltalk, that kind of thing is normally represented by segments of executable code that invoke a long sequence of side-effecting methods, rather than by data structures.

(You can also represent that kind of thing invoking a long sequence of pure functions, as in the "fluent interface" pattern that became popular with JQuery, but that isn't the Smalltalk way to do things, especially in 01994.)

The GoF examples other than the prototype factory are WidgetFactory and MazeFactory; in InterViews, WidgetKit, DialogKit, and LayoutKit; and in ET++, WindowSystem. Kerievsky's examples in Refactoring to Patterns (where he points out that people constantly conflate the various "factory" patterns, and that the lines between them are somewhat vague) are HTML parser AST node creation, ORM attribute descriptor creation, java.util.SynchronizedCollection and UnmodifiableCollection, and an OutputBuilder interface that allowed him to create either DOM nodes or XML output from the same unit test code. In all of these cases except the OutputBuilder, the factory objects are stateless.

So, by your definition that a factory is what you get if you take a function and add mutability, we find that 6 out of 7 of the GoF examples of "factories", and 3 out of 4 of Kerievsky's examples of "factories", are actually "functions" rather than "factories". I think this clearly shows that the categorization you propose does not coincide with the scriptural explanation of what "factory" means.

⁂

It's true that, in the case where there's no state, you can do this with just a pointer to a function. But pointers to functions don't exist at all in languages like Java, and aren't first-class values in languages like Pascal, where they are subject to various restrictions that keep you from using them like this.

Even in languages like C, which have first-class function pointers but not closures (except as a GCC extension subject to Pascal-like restrictions), you can't create new function pointers at run-time. So in C, or C++, you can replace a singleton factory like the ones GoF considers usual with a function pointer, or maybe a record of function pointers. But you can't write a factory factory that way, much less a factory factory factory.

So, while you're right that in a sense an immutable factory "is just" an indirectly-invoked function—that's the main point of my comment—if you want to do that in a language like Smalltalk or C++ (at least in 01994, when they wrote the book) or Java (at least in 02000), the Abstract Factory pattern in GoF tells you how to get it working.

Re:Why OO Sucks by Joe Armstrong (2000) by lmm on Sunday March 28, 2021 @10:21PM (#26617064)
> Although the GoF Abstract Factory pattern doesn't rule out having mutable state in your factory object, that is neither necessary for its purpose nor common practice when applying it in OO programming languages.

An object by definition encapsulates mutable state. Even if a particular object happens to not contain any mutable state, there's no way to observe that from outside, by design.

If your language forces you to use an object to represent an (immutable) function value, all I can say is don't use such a shitty language.

Re:Why OO Sucks by Joe Armstrong (2000) by kragen on Tuesday March 30, 2021 @11:10AM (#26635495)
> An object by definition encapsulates mutable state. Even if a particular object happens to not contain any mutable state, there's no way to observe that from outside, by design.

If I read you correctly, your definition of "object" excludes each of the different concepts that C++, D, Python, and Abadí and Cardelli's object-calculus call "objects", but includes what Scheme, OCaml, and Kotlin call "functions"? You are of course welcome to use that definition, but it might improve communication to clarify that you're using the same word that other people use but with a completely different meaning.

> don't use such a shitty language.

There's a plausible reading of the GoF book and the whole "OO patterns" movement that it's largely about how to make do with shitty languages, though I'd nominate SICP as being better at that. Often languages that are shitty on one axis have compensating advantages on other axes—for example, although you can script Cocoa with cffi in Python, which is less shitty than Objective-C for scripting in general, you waste a lot more time debugging segfaults; despite the admitted aesthetic advantages of 68000 assembly with respect to amd64 assembly, the latter performs noticeably better on this laptop; and, while I think I've convincingly shown that a factory factory factory is dramatically more readable in OCaml than in Java, you're probably going to have a much easier time getting your remote dictionary server performance to scale to 16 cores in Java than in OCaml. If you have enough RAM, anyway.

Re:Why OO Sucks by Joe Armstrong (2000) by lmm on Wednesday March 31, 2021 @05:32AM (#26645178)
> If I read you correctly, your definition of "object" excludes each of the different concepts that C++, D, Python, and Abadí and Cardelli's object-calculus call "objects", but includes what Scheme, OCaml, and Kotlin call "functions"?

That's not my intention; I'm trying to match the definition of an "object" in common programming languages. As far as I can see an object (instance) is essentially a bundle of method handles associated with an opaque (possibly empty) bundle of (possibly) mutable state. (In some languages the bundle might also include some visible state, but that doesn't change the essence of the thing, since that can be emulated with method handles - indeed Kotlin does exactly this). In Python's case the opacity of that state is a convention rather than a strict rule, but I think the general consensus would be that it should generally treated that way, and also that Python's objects are somewhat less object-ey than those of other languages. Certainly I'd say this definition is a close fit for C++ and Kotlin; what are you seeing as different there?

Re:Why OO Sucks by Joe Armstrong (2000) by kragen on Wednesday March 31, 2021 @03:45PM (#26652147)
Hmm, I had thought that Python didn't allow you to add data attributes to objects derived from immutable classes like `tuple` or `str`, but evidently I was wrong about that!

    >>> class S(str): p = lambda me: len(me) + 3
    ... 
    >>> S("hi").p()
    5
    >>> S("uhoh").z = 3
    >>> 
You can't even detect this by enumerating the object's attributes with `dir()` or `.__dict__` at some moment — a method, or external code, can add new attributes at any later moment.

(Hypothetically in CPython maybe you could disassemble the bytecode of a method to see if it tries to mutate the object or some global variable, but even if this is possible, you can also use cffi to change the value of 3 in CPython. I regard both of these as breaking the semantics of Python per se.)

So, I was wrong about Python. And, on further reflection, I was wrong about C++ as well, because even if you provide a `const`-argument override for a function, which is a thing you can do to observe whether an object is `const` or not from the perspective of your caller:

    int x(int* foo) { return *foo + 3; }
    int x(const int* foo) { return *foo + 2; }
    // declaring as `const int i` changes program behavior:
    int main(int argc, char **argv) { int i = 7; return x(&i); }
there's not even a compiler warning if your caller fails to do so as well:

    int x(int* foo) { return *foo + 3; }
    int x(const int* foo) { return *foo + 2; }
    // int y(int* foo) { return x(foo); }
    int y(const int* foo) { return x(foo); }
    int main(int argc, char **argv) { int i = 7; return y(&i); }
D's `immutable` is externally observable in the same way as `const` in D or in C++, but rigorously so in the sense we want here: invoking a function whose parameter is `immutable` with a mutable object is a compile-time error.

Also, D's `immutable` is transitive, unlike C++'s `const`, but like, for example, E's DeepFrozen.

In the simple object calculus, everything is an object and there is no mutability at all. You could reasonably object (heh) that, although it's intended to capture object-oriented programming, and Abadí and Cardelli's book is called A Theory of Objects, it's not widely used, so opinions may differ as to whether they succeeded. https://news.ycombinator.com/item?id=26588642 goes into more detail there.

So your definition of "object" only excludes each of the different concepts that D and Abadí and Cardelli's object-calculus call "objects" (and I guess I should add E), not Python's or C++'s as well.

As for Kotlin, my claim about Kotlin was not about Kotlin "objects" but about Kotlin "functions", which are closures that can include access to mutable state, just as in Scheme and OCaml. I don't know Kotlin well enough to know whether your definition of "object" fits its definition of "object". It's an interesting question. Maybe it does! Kotlin's data classes, for example, can contain mutable attributes, and there really is no (non-reflective) way to observe them from another class — for example, for a method to refuse to accept an argument if it happens to be mutable in this way. Kotlin/Native evidently has a form of transitive and externally observable immutability, but I don't know much about it.

Your revision of the definition to include a bundle of method handles does seem to neatly exclude what are called "functions" in Scheme, OCaml, and Kotlin. I think it might also exclude objects in E, but since E is dynamically typed, that's sort of an implementation detail, and anyway E doesn't figure prominently in the consensus definition of the term "object".

Thank you for a very educational and stimulating discussion!

Re:Why OO Sucks by Joe Armstrong (2000) by marcosdumay on Friday March 26, 2021 @10:41AM (#26592714)
The funny thing is that rewriting your code a lot is much easier on FP with flexible types.

You just need to not pay any attention to design and start coding right away. Then you'll rewrite your FP code even more than you did for your OOP code, and still have an easier time overall.

In all seriousness, I've found that a very nice way to develop FP code. It always looks good at the end anyway, and there's no risk of getting into astronaut mode.

Re:Why OO Sucks by Joe Armstrong (2000) by watwut on Friday March 26, 2021 @04:04AM (#26589759)
The only thing that changed after that book is that people started to put "Factory" into the name of factories and "Decorator" into the names of decorators.

Both existed for a long time before, but did not had names that would clearly instantly made them recognizable as such.