12 ms·
ValueObject (2016)
- gepardi 4y agoI really wanna understand this but I feel like it’ll take me a while.
- seibelj 4y agoWhy make a string when you can create a new StringValue class, StringValueFactory, StringValueFactoryIterator, StringValueFactoryIteratorFactory, StringValueTemplateDecorator...
- xupybd 4y agoLearning a functional language could help. You get to explore immutable data structures and how to work with them. Much of what is being discussed here are concepts that are first class citizens in a functional language.
- stickfigure 4y agoIf your takeaway is "I should strive to make objects immutable" then you are on the right path.
- jmts 4y agoProgramming is all about abstractions and the ways they interact. Specifically this article talks about what it means for two instances of an object to be "equal", which depends very much on what kind of equal you care about. Consider the possibility that you order two laptops from your favourite computer store. When you order them, you customise one, then in the shopping cart you hit "+1" to order two of them, so for the purposes of this example they are functionally equivalent. When they arrive though, they have different serial numbers ABC, and XYZ. So then we start asking the question, are they equal? One way of asking this question is "Do these two things perform the same function?" to which we can answer yes, ABC and XYZ perform the same function - this line of questioning treats the laptops as ValueObjects as described in the article - laptop ABC and laptop XYZ can be swapped for each other without concern about whether they will behave differently. Another way of asking this question is what amounts to something closer to "Is this thing the one that I care about?". This line of questioning treats the laptops as ReferenceObjects. You've just received both laptops, taken them both out of the box, they're functionally identical except that you've decided that you want to use ABC for work stuff, and XYZ for personal stuff, and now you have some work stuff to do. You pick up one laptop and ask "Is this laptop ABC?" - not because the function of the laptops matter at this point, but now because of what they represent. Both have their pros and cons (as described, ValueObjects are better when they are immutable, ReferenceObjects might tend to be more complex), and different languages make different choices about whether certain language primitives and standard objects/classes are implemented as ValueObjects or ReferenceObjects, but understanding them both conceptually allows you to write code that forces one kind of behaviour because that's what matters most for your chosen solution, independently of what the language you are writing it in has as its standard behaviour.
- deleted 4y ago[deleted]
- celdon25 4y ago(2016)
- quickthrower2 4y ago"==" considered harmful? :) Maybe we should explicitly say O.referenceEquals(O2) or O.fullDepthEquals(O2) or O.itemsReferencesEqualInAnyOrder(O2). etc.
- marcosdumay 4y agoWhy would one care about reference equality of anything in Javascript? It's not like memory addresses are a concept on that language.
- UK-Al05 4y agoReference equals doesn't make sense for a value object anyway.
- quickthrower2 4y agoUnless you are using immutable objects. Then efficient equality check is referenceEquals() || deepEquals()
- nine_k 4y ago1 is an immutable object, and in languages such as Java or C++ it's not a reference-type object. You meant heap-allocated immutable objects, like some kind of a Haskell list, or maybe small stack-allocated immutable objects, like a tuple.
- int_19h 4y agoThat should be in the guts of the implementation of == for value types, not something exposed outside of it.
- quickthrower2 4y agoYes I agree, in Haskell it is derived, for example.
- lmkg 4y ago
- DecoPerson 4y agoThe way is see ValueObjects is as the most convenient tool when working with a very well-defined space. For example, integers on the number line. There’s no uncertainty— 2 is 2 is 2. Same goes for a 2D coordinate within a 2D space, or 3D coordinate… etc. The “date space” is quite well-defined these days, as long as you’re working with a Gregorian calendar and typical timezones. It’s at the limit of “coordinate within a space” but as programmers and with training we can handle it. Once you start getting away from simple, well-defined spaces, things start to get complex. What is a “phone number space”? Or a “given name & surname” space? They exist, but they are insurmountably complex — both for us and for code — and are ever-changing as the world changes. So we bring order by converting these problem to a well-defined space that we know how to work with: strings. Or rather, strings that conform to certain patterns (numbers for a phone number). This is a grey-area. ValueObjects may be useful, or may just complicate matters. But not all problems can be mapped to strings. Customers have N sales orders. Sales orders have X lines. Lines refer to inventory items with Y modifiers. You could probably define the “space of all customers, sales orders, inventory items & modifiers” but it would have so many dimensions, and be mostly enormous regions of non-sense. This is where ValueObjects have no place.
- duped 4y ago> not all problems can be mapped to strings I think that statement is at odds with the Turing-Church hypothesis, but I understand that is pedantic
- nextaccountic 4y ago> What is a “phone number space”? Do you need to call the phone number? Then you probably need to type it. If two phone numbers when typed result in the same number, they are the same. So you probably want to normalize your numbers: remove ( or - or spaces or anything that isn't actually meant to be typed, then you can compare two phone numbers. Of course it doesn't end there. There's another, looser equivalence between phone numbers that are much more useful: if we call both numbers, will they call the same phone? Things like presence vs absence of country codes, area codes and other things could be part of your normalization. But then, you would need to keep the context in which each phone number was collected. For example, if someone in a given city tells they have a phone number, and someone else in another far away city informs the same phone number, they probably refer to different phones (because the area code is likely different) What's better, of course, is to ask for country code and area code in the same form. If all your phone numbers are "fully qualified", then you can just compare them for equality. But this is not always possible So this kind of thing is fuzzy and has edge cases, but getting it right is essential if you want to search for two people with the same phone number (for example). Since there are edge cases, it's important that phone numbers are stored exactly as they were input, and then normalize as a separate column or something.
- iamcrazyyounus 4y agoA valuable object might make references to other things. For instance, a route generated by an application that explains how to get from one location to another would be a value object. It would represent a snapshot of the locations along a particular path, but this suggested route would not be identifiable, despite possible internal references to things like cities, roads, etc.
- Pxtl 4y agoNice little introductory essay on the concept. One wrinkle I've often pondered -- once you get copy by value semantics, do you really need immutability? I mean immutability is more pure but it's also often a nuisance implement if an object has a lot of members and you need to do a lot of "copy but change this one member" operations.
- sparkie 4y agoOCaml has a nice solution: Functional Objects[1]. Instead of a member mutating something in an object, it just returns a copy of the object with the relevant changes. [1]:https://v2.ocaml.org/manual/objectexamples.html#s%3Afunctional-objects https://v2.ocaml.org/manual/objectexamples.html#s%3Afunction...
- int_19h 4y agoIt's a double-edged sword - by having your data immutable, you can also avoid copies. Or, better yet, the compiler can avoid them for you.
- mrkeen 4y agoI use Haskell, where the syntax to "copy but change one member" is: let newObj = oldObj { member = newMember } In Java, you use the builder pattern, which is not built in. So you use Lombok to generate it for you, and then you get in a fight with your team lead about not using Lombok because he heard about this-one-guy-who-had-a-problem-with-it-just-do-your-own-research-I-can't-be-bothered-explaining-it-to-you, so then you roll (and maintain) your own Builder object for every value class.
- duped 4y agoThis feels like a roundabout discovery of data oriented programming.
- revskill 4y agoWasted 10 years programming because of OOP. Better late than never. Just use function and what you need is really just Data structure + Algorithm. DDD, Design patterns,.. or complicated things just born to fix the OOP methodology.
- bbzealot 4y agoI don’t see your point. DDD is not inherently tied to OOP: the whole strategical part isn’t about code at all, and the tactical part is applicable to FP as much as to OOP. WRT design patterns, they exist in functional programming as well, they’re just different.
- vasergen 4y agoDDD is not hightly coupled to OOP, the problem is that most popular books (red and blue one linked in Fowler's blog for example) were written in time when OOP was very popular and authors also used languages that propagates OOP. If the authors would used purely functional languages then people wouldn't assume it is about OOP.
- bbzealot 4y agoFair point, even though IMHO it's not that hard to apply the concepts from these books to a FP context. Nowadays there are even resources about DDD specific to the FP world, such as Domain Modeling Made Functional by Scott Wlaschin [0]. [0] https://pragprog.com/titles/swdddf/domain-modeling-made-functional/ https://pragprog.com/titles/swdddf/domain-modeling-made-func...
- aryehof 4y ago> what you need is really just Data structure + Algorithm Lets hope your not in charge of building that new complex air traffic control system, bank lending system, or large inventory management system. Did you learn nothing of over a decade of large projects using data-flow modeling (structured analysis)?
- 4y ago
- moonchild 4y agoIsn't this just henry baker, equal rights for functional objects (1993)? - https://plover.com/~mjd/misc/hbaker-archive/ObjectIdentity.html https://plover.com/~mjd/misc/hbaker-archive/ObjectIdentity.h...
- wiso 4y agoNow C# has records which is exactly that.
- xavdid 4y agoPython has two great ways to write immutable "ValueObjects": - Dataclasses w/ the `frozen=true` kwarg - namedtuple They add some headroom to programs, but I find they help _immensely_ for readability and fewer bugs. Bonus, each works out of the box for equality testing (two objects are "==" if they're of the same type and the values of each of their fields are equal)
- davidkunz 4y agoThere's a JavaScript stage-2 proposal for records and tuples: https://github.com/tc39/proposal-record-tuple https://github.com/tc39/proposal-record-tuple
- dang 4y agoRelated: ValueObject - https://news.ycombinator.com/item?id=12953266 https://news.ycombinator.com/item?id=12953266 - Nov 2016 (104 comments)
- captainmuon 4y agoBasically this is a very limited case of operator overloading. Redefine `==` so that it means value equality and not reference equality. But it would be so much nicer if JavaScript had proper operator overloading. You could actually define it nicely on top of TypeScript since you usually know the types of the operands, and could rewrite the call at compile time. Unfortunately, the TS developers are philosophically opposed to adding such a feature. And I'm sceptical JS proper will add operator overloading soon. It would be incredible for e.g. graphics programming to have proper matrix and vector types and to be able to write formulas in a natural way!
- wruza 4y agoI wanted that too until I realized they are usually used only in a couple of files in a project, only a handful of times in each. Operator overloading looks nice but it isn’t a life changer. What javascript lacks is a culture of [in]formal protocols which could provide additional semantics. It has some: toString, toJSON, @@iterator, etc. Maybe this is a good thing.
- fbn79 4y agoYou can ovveride valueOf and do something like +p1 === +p2 Check this generic implementation https://jsfiddle.net/tas2n59L/ https://jsfiddle.net/tas2n59L/
- TazeTSchnitzel 4y agoIn languages like C, C++, Rust, Swift, and C#, either not all types have to be heap-allocated reference types, or the runtime will handle copy-on-write for you, so it's convenient to create and work with small value types. It's unfortunate that so many popular languages don't do this. Having everything on the heap is not good for performance and creates an unnecessary choice between mutability and aliasing.
- cbrogrammer 4y agothere is no copy-on-write in the C language
- skitter 4y agoBut in C, the first half of the 'or'-dtatement applies.