10 ms·
My definition of an object is "an ADT that gets to 'decide', using private runtime state, what to do in response to any/all attempts to interact with it." Whic
by derefr 10y ago
My definition of an object is "an ADT that gets to 'decide', using private runtime state, what to do in response to any/all attempts to interact with it."
Which makes for a very simple test: if something is an object, you should be able to send it a message that makes it change its mind about what messages it accepts after that.
There's no obvious way to implement a behavior like this using the native "objects" in nominally-object-oriented languages like C++ or Java. (But you can do so with the closure types from these languages. Or you can look higher on the stack: threads with IPC queues, or POSIX processes, are both objects.)
But implementing this behavior is obvious in Ruby; this is how basic things like Object#extend work.
And implementing this behavior is also obvious, oddly enough, in Erlang: http://joearms.github.io/2013/11/21/My-favorite-erlang-program.html http://joearms.github.io/2013/11/21/My-favorite-erlang-progr...
In conclusion, Erlang is more object-oriented than Java. ;)
---
And yeah, I know, this definition of OOP-ness sort of flies in the face of decades of things people have said about OOP. Objects, under this definition of OOP, don't obey any of the SOLID principles. They're slippery; unpredictable; "alive."
But this was the original definition! It's the one Smalltalk fits; it's the one LambdaMOO fits; it's what is meant by referring to Javascript as object-oriented.
It's a shame we have this collision between meanings; having two separate words for "OOP like Smalltalk" and "OOP like Java" would have saved language-designers all a world of arguments from people who try to propose extensions to a language in one category, assuming it's in the other.
- vonkow 10y agoErlang is an amazing language for learning how to effectively use OOP principles. I feel like anyone who works primarily with class-based languages owes it to themselves to write a few server-like modules in Erlang even if they never touch the language again.
- yellowapple 10y agoPerl is also another interesting language for learning the low-level details of OOP, since its default set of OOP features (as of Perl 5; this all changes in Perl 6) involves manually "blessing" some data structure into an object. Objects are thus just "blessed" data structures (and not just hash tables, either, though those are the most common), and methods are just subroutines that accept their object as their first argument. Between Perl and Erlang, I think I've ended up learning way more about real object orientation than I did from my dabbling in C++ and Java.
- Tarean 10y agoI have been thinking for a while that oop in the message passing sense is basically coroutines. I guess that is encoded in Erlang? Probably should learn it at some point.
- runeks 10y agoYour definition makes a lot of sense to me. In pure programming languages, an object can appear when you run the code, but never before that. The pure description of the program cannot contain objects -- since objects carry state -- but when this description/code is executed/evaluated by the runtime system, a stateful object appears that you can interact with (e.g. a web server). So, for pure programming languages, this "private state" is contained within the runtime system (out of reach for the programmer), rather than in the code itself, and the RTS applies pure functions to the state it carries, in order to reach a new program state.
- noblethrasher 10y agoYour definition of an object is similar to one that I’ve used[1][2][3], which is unsurprising since we got it from the same place. But, now I define an object as any component with which you interact strictly by way of negotiation (as opposed to command and control), and I finally think I understand why Alan Kay says that the most important thing in OOP is the message passing. Thus, statically-typed languages like C#/Java/C++/etc. are also capable of being object-oriented as long as you avoid setters and realize that some of the negotiation happens at compile-time. [1] https://news.ycombinator.com/item?id=8679224 https://news.ycombinator.com/item?id=8679224 [2] https://news.ycombinator.com/item?id=11967631 https://news.ycombinator.com/item?id=11967631 [3] https://news.ycombinator.com/item?id=11809477 https://news.ycombinator.com/item?id=11809477
- betenoire 10y agoAvoid setters?
- lacampbell 10y agoNot parent, but I think setters destroy what's objecty about objects - that you send them high level messages and don't care what's inside of them. As Alan Kay puts it (paraphrasing): "setters turn an object back into a data structure"
- eropple 10y agoThis is one of the few things about Ruby idioms that drive me a bit nuts--improper use of `attr_accessor` when `attr_reader` creates better and cleaner code is one of the leading code smells in the library ecosystem.
- cagmz 10y agoIs this to say that `attr_accessor` is too "heavy handed" when just using `attr_reader` could have sufficed?
- naasking 10y agoA definition of objects that rules out immutable objects doesn't seem viable to me. Furthermore, your definition merely requires mutability, which is a feature of Java and C++ objects, so I'm not sure what you think is lacking. Your definition reduces to "objects undergo state transitions".
- seanmcdirmid 10y agoObjects require at least an identity. Immutable data structures necessarily don't have identity (well, to say identity isn't meaningful because aliases can simply be seen as copies). There is more to it than identity, and you can always use a mutable variable or rebound parameter to recreate an identity for an immutable object (I.e. the world as an input and output).
- lacampbell 10y agoI don't follow why objects need an identity, as you define it. How does not having an identity make something un-objectlike?
- seanmcdirmid 10y agoI would say identity is core to objectness. Without identity, something is purely structural, only its value is relevant because it will not transition to another state later. Objects must be things you can manipulate. They have a presence beyond their values.
- lacampbell 10y agoI use objects that can't transition from one state to another practically every working day. It's a strange view point to me that because something is immutable, it's not really an object. But a view worth thinking about. To me the important thing is hiding internal state and sending high level messages instead of something like "change internal field x to y". Whether that internal state you're hiding is immutable or not seems irrelevant to me.
- unscaled 10y agoTo be entirely accurate, you're wrong when you say your definition of OOP is the original one. SIMULA-67 predates Smalltalk and its object system is much closer to Java than it is to Smalltalk, although it helped inspire both. To be more fair, there are two very different schools which interpreted and continued to developed Simula in two radically divergent ways. One of the schools is focusing on clear division between rigid, statically defined classes that never change (but may extend other classes) and runtime instances, which can contain only state, not behavior. This school does everything exhaustively codify the interfaces exposed by the classes: ensure that a predefined set of messages with a predefined set of parameters for each message (a.k.a methods) is permitted, control access to these methods and in the most extreme languages (e.g. Eiffel) even verify that object state and parameters conform to a predefined contract. This school is obviously not opposed to modifying behavior patterns based on runtime state, but it believes that the object system should not directly support that, and encourages programmers to implement their own mechanism on top of rigid-class object systems: this is what design patterns are usually meant to achieve. The other school believes that an object system's first priority is giving objects absolute autonomy in parsing the messages they receive and as a result usually ends up with object systems that focus on powerful runtime extension and arbitrary message parsing functionality instead of compile-time strictness. We could trace them back to their origins and call them the C++ school and the Smalltalk school. We could pin them to their modern champions and call them the Java school and the Ruby school. We could follow their poster boys and call them the class school and the message school. We could go on with type of languages they tend to thrive in and call them the dynamic school and the static school. I would call either school "more object-oriented" than the other - they just have entirely orthogonal definition of what objects are. I can't even call either of them "better". I much prefer the Smalltalk school to the rigid version of the Java/C++ school (as was practiced in these two languages through most of the 90s and 2000s), but modern languages in this school have started incorporating some of the ML/Haskell tradition to give you much more powerful (and safer!) abstractions to define the way your objects may change their behavior in runtime. Design patterns are no longer encouraged, and if I'm using a language like Scala, Kotlin, Rust, Swift or even C#, I rarely find it necessary to employ a design pattern anymore.
- dtech 10y agoYour definition sounds almost exactly like how an actor can handle messages and state, so it's no wonder that it's easy to implement in Erlang.
- coldtea 10y ago>My definition of an object is "an ADT that gets to 'decide', using private runtime state, what to do in response to any/all attempts to interact with it." Which makes for a very simple test: if something is an object, you should be able to send it a message that makes it change its mind about what messages it accepts after that. Your criterion doesn't match your definition. Specifically "you should be able to send it a message that makes it change its mind about what messages it accepts after that" doesn't logically derive from "an object is an ADT that gets to 'decide', using private runtime state, what to do in response to any/all attempts to interact with it". The second not only needs: a) an object is an ADT that gets to 'decide', using private runtime state, what to do in response to any/all attempts to interact with it but also implies b: b) you can interact with the object and alter its private decision making process (and not just partially, e.g. only alter the data part of its runtime state).
- throwawayjava 10y ago> My definition of an object is "an ADT that gets to 'decide', using private runtime state, what to do in response to any/all attempts to interact with it." Which makes for a very simple test: if something is an object, you should be able to send it a message that makes it change its mind about what messages it accepts after that. I'm not sure these are equivalent criterion. In Java and C++, the "dispatch matrix" is defined at compile time, and it is stored at runtime, and the object itself does determine the contents of that matrix. So these languages do clearly meet your first criterion. But also, clearly, not the second. I guess the objection you might raise is "any/all messages", with "modify your dispatch matrix" being a reasonable message. But there will always be some message that cannot be implemented by the object; we can certainly code up some Goedelian thing inside of a core OO calculus. But for any concrete OO language, something more reasonable than that probable exists. So then the question is where -- not if -- we draw the line on "any/all messages". And I'm not sure that drawing that line south of Ruby but north of Java makes a lot of sense.
- moron4hire 10y agoMeh, that's a bit like arguing over "I could care less" vs "I couldn't care less", except far more pedantic because this time it's about an abstract concept that doesn't mean anything at its face value. Object Oriented Programming is whatever the majority of people agree to it being, so as to facilitate communication. "Original definitions" don't matter. Alan Kay has no more right to define "Object Oriented Programming" than anyone else, you or me included, anymore than long-dead Thomas Jefferson has a greater right than the rest us to define the course of our country. If you go on a job interview and they ask you "do you know object oriented programming?" and you say "oh yeah, I know all about that," then you're both going to be very disappointed on your first day of work.
- dllthomas 10y ago> Meh, that's a bit like arguing over "I could care less" vs "I couldn't care less" Obviously an aside, but... While the former quite possibly arose as a corruption of the latter, I like to interpret it as "I could care less [but only if I really tried]".
- protomyth 10y agoYour definition of an object sound more like and agent http://robotics.stanford.edu/~shoham/www%20papers/AgentOrientedProgrammingAIJ.pdf http://robotics.stanford.edu/~shoham/www%20papers/AgentOrien... [PDF]