3 ms·
I have a version of this that I've used for years. Objects make good nouns. Closures make good verbs. Software organized around nouns looks different than so
by btilly 13y ago
I have a version of this that I've used for years. Objects make good nouns. Closures make good verbs.
Software organized around nouns looks different than software organized around verbs. Not better. Not worse. Different. Use what is appropriate.
- deleted 13y ago[deleted]
- guelo 13y agoA concept from a classic Steve Yegge rant, http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom...
- btilly 13y agoHe said it better, but I said it before he did: http://www.perlmonks.org/?node_id=318563 http://www.perlmonks.org/?node_id=318563. The root of that discussion was http://www.perlmonks.org/?node_id=318257 http://www.perlmonks.org/?node_id=318257 which I think is interesting in its own right.
- dustingetz 13y ago"Objects make good nouns" most 'objects' as they exist in today's OOP have methods, which makes them both nouns and verbs. Idiomatic code in most of today's mainstream OO languages doesn't make heavy use of closures, mostly only Javascript. Anyway, the closures vs methods debate has been going on for decades is not a closed topic[1], and the answer certainly depends on the language you're using. [1] http://www.c2.com/cgi/wiki?ClosuresAndObjectsAreEquivalent http://www.c2.com/cgi/wiki?ClosuresAndObjectsAreEquivalent
- samatman 13y agoI don't believe this is a nitpick, but fully encapsulated OO provides only nouns and verb phrases that have a pre-specified noun. This is different from data structures (nouns) with collections of functions (verbs) that can act on many data structures in a consistent way.
- btilly 13y agoWhat you say works well if you already have your data nicely divided into different kinds of objects. However a useful technique with closures is a dictionary of closures allowing you to map things that may be of the same type to different behavior. Wrapping those in objects so that you can use method dispatch can be a lot of useless work. As a concrete example I point you at http://www.perlmonks.org/?node_id=34786 http://www.perlmonks.org/?node_id=34786 where I demonstrated how to take transform a markup language into HTML through the technique of grabbing a token, then passing to a closure that is associated with that token to consume a bit of the document. Porting that closure-based code to an OO style would be a nightmare.
- wellpast 13y agoActually there is better and worse. Verbs/functions are easier to compose and recompose; they're easier to reason about (there is no "state" in verbs; verbs are declarative.) Nouns (assume: with behavior; not just data -- otherwise you are in FP-land again) are harder to compose and recompose, harder to reason about (state transitions). Consider calculating age. Assume general FP and general OOP ways of solving the problem. Then: ageOf(p)... ...ageOf() can potentially be applied (or modified to apply) to different value types. ...self-evidently does not mutate p or cause a state transition (assuming FP). p.ageOf() ...can only be applied to value types/classes that support that operation. ...may or may not mutate p/cause a state transition of p. One is much easier to reason about and reapply when the world changes.
- btilly 13y agoFor a start when we're talking about closures versus objects, we're not necessarily talking about true functional programming. Closures can have and manipulate internal state. Pretending that they don't ignores real world usage where they do. However let's limit ourselves to functional techniques. Whenever someone talks about something in the very abstract and says "easier to reason about" I mentally insert "for you". Certainly it isn't true for everyone or every situation. A basic principle in programming is that starting at the hardware level we build abstraction layers that let us think about things in higher level ways, hopefully ultimately in a way that matches the terms that the end application uses. End applications have concepts like account balances, user status, and other such stateful pieces of information. If you're going to have the state, workaround that you pretend you don't, you will cause confusion. Pick your favorite 5 Monad tutorials for proof. Consider as an example the use case, "Provide a form to let us mark spammers, and block them from our site." For all the theoretical advantages of avoiding state, this is going to be easier to model in a system where users have a piece of state known as is_spammer.
- _random_ 13y agoOr you could pick something like C#/F# and use both approaches harmonically.