7 ms·
Donkey code
- karmakaze 3y agoBesides the naming, the code examples because it uses an ask-then-do-different-things anti-pattern rather than Tell-Dont-Ask which implies defining distinct classes (or property values that define behaviors in the thing itself) rather than strewn across the codebase by interpretation, ie. there was no factoring only use-site decisions. The problem wasn't lack of naming, it was incomplete defining of the thing. If it had a definition that everyone agreed upon, the name is arbitrary like naming of celestial bodies.
- sleet_spotter 3y agoThis is a helpful way to think about it. Though, is the problem of having an incomplete definition the same as lacking a unique name? Maybe the solutions are different, and the author points out that made-up names don't cut it. Is it instead more of a catch-22 situation? The other dimension implied here is the timing of when variety is introduced. If all the options were known when the system was originally designed, the issue may have been more clear. So then the other problem is that, when the need to distinguish between cases first came up, the solution was designed for only 2 cases. And if there were only ever those two cases, the solution presented would probably be the most efficient. The solution to this incremental scope problem is a lot less clear to me and something I struggle with personally in scientific computing projects. When variety #2 pops up, how does one decide whether to restructure everything in case of a variety #3, or take the quicker (potentially more efficient) path and avoid unnecessary complexity? Assuming I've done as much as I can to anticipate and constrain my needed functionality. It somehow feels like I end up losing either way.
- 6510 3y agoFirst you have to make things worse. You just proudly {horse breed: zebra} then {horses breed: sofa} and so on.
- karmakaze 3y agoI didn't know (or maybe forgot) that zorce and hebra are a-things. Certainly don't know of the sofa kind--google shows me mini-hinny laying cozily.
- karmakaze 3y agoThink about how physicists talk about particles, they can pick any name, rename them but everyone knows what's what because the names are defined by the set of properties not the other way around. If they find distinct sets of properties they want to talk about then they know they need another name. I ran into this same scenario talking about names for different states of inventory. Every person/company I talked to had different names or meanings of special 'reserved'/set-aside states. I kept telling everyone to define what they mean by that name, list the expected behaviors of it. Then it doesn't matter what anyone calls anything, I can match them up by properties/behaviors or find out they're distinct things. Things became clear you can't trust names when I saw the formula: on-hand = available + unavailable + committed. Shouldn't available + unavailable form some complete total?
- karmakaze 3y ago> The solution to this incremental scope problem is a lot less clear to me and something I struggle with personally in scientific computing projects. When variety #2 pops up, how does one decide whether to restructure everything in case of a variety #3, or take the quicker (potentially more efficient) path and avoid unnecessary complexity? Short-answer rule-of-three (but really it depends). I get what you mean, but this is more about implementation choices than identification of variety. Varieties can be grouped different ways that makes sense for implementation challenges or development timelines and later refactored to be more effective. The identification of the varieties themselves is distinct from the choice of grouping/implementation. Things that have little effect on behaviors (eg. color only affects one aspect of rendering) can be a property of a thing. More complex/varied characteristics may be better served by separating into distinct things. Sometimes a 'bag of properties' can serve for all things (similar to JS Object). That's how I believe reddit's data model evolved at one time. I think HN isn't much different as it seems that id's of posts, comments, etc share numbering.
- knallfrosch 3y ago> If it had a definition that everyone agreed upon And there's your problem.
- karmakaze 3y ago...and the proposed solution is to go about it by listing properties/behaviors/etc rather than agreeing on a set of names and hoping for the best.
- devdp430 3y agoThe writing style is very beautiful.
- gnulinux 3y agoYeah, it's gorgeous. Makes me jealous. Reminds me of some author I read recently but I can't put my name on them...
- knallfrosch 3y agoThe sentences are concise with little interjections. Long sentences are reserved for enumerations, donkeys and horses, which are easy to read too.
- zubairq 3y agoInteresting read, I try to give a lot of thought to naming things but still can’t get it right first time. Often I need the courage to refactor, which also involves changes to a lot of documentation. Also, retaining backwards compatibility is tough when renaming things so proper names can’t always be used
- gnulinux 3y agoThis is why I'm a big fan of "name it Steve" philosophy. When we create a new service many people are inclined to call it something like "rhombus keyboard fibriculator" or "octatonic runner". Usually it's of the form "{verb}er" or "{noun} {verb}er" or "{adj} {noun} {verb}er" or whatever. It's never accurate, and it's super confusing because chances are there is some other "{x} {y} {verb}er" with the same "verb". Not to mention it's super long. Just name your things as things. Name it Pluto, Calvin, Steve, Jon, Ted, Ann, Mobius, Orpheus, Martha, Alex... I don't care. Maybe even Tomato, Artichoke, Celery, Broccoli, Pumpkin, Rooibus etc... It just needs to be as neutral as possible. This is the power of mathematics really. When I say "monoid" do you have any idea what it might be? No, well that's great, because here is a very formal definition of this thing called "monoid". Old school names like "group", "category" etc are worse. Names like "Kolmogorov complexity", "P-Complete", "Galois Field", "Ultrafilter" are much better.
- mcbishop 3y agoAgreed. And might as well use one-syllable words that are pleasant to read / write (e.g. bob, tom, box, rack, sun).
- kbenson 3y agoThe next thing you know your using joe[1] to write joe[2] in joe[3]. At work we have an app called "directly", and an app called "indirectly". Ask me if I ever have to clarify by asking "you mean in an indirect way, in the app indirectly or in the app directly?" (the answer is ALL THE TIME) 1: the editor 2: a service you created 3: Joe-E the language
- gnulinux 3y agoJust use less common names like Brice or Werner or Rubin?
- InitialLastName 3y agoMy favorite thing is when names that I often have to communicate verbally for written use have ambiguous alternative spellings.
- deleted 3y ago[deleted]
- abdellah123 3y agoThis one's epic. I believe this is a fundamental aspect of OOP. Name different "things" differently!
- seb1204 3y agoI thought it is more about recognising that they are talking about a different thing they have to name instead of adding additional qualities to it.
- DeathArrow 3y agoThe problem is not with naming, the problem is with OOP, specifically inheritance and encapsulation. You could have separate entities for horses, donkeys, mules, zebras and functions that work on them. Maybe less code reuse but state is kept separately, code is very easy to understand, modify and implement. And also is faster to implement. If you have too many ifs in some functions you have to ask yourself if you architected the code well.
- dsego 3y agoI didn't read this post as relating to OOP, more to have with defining specific terms and modelling the domain as it relates to business needs. In the example given, the business didn't recognize that they are dealing with donkeys. In a real project you could have eg invitation vs user account. Is the invitation its own model associated with a future user account or just a placeholder user account. Things change when you have other admin accounts that have to assign those users to other entities before the user is signed up in the system for example. (maybe it's a contrived example, couldn't thing of something better)
- Rayhem 3y agoI'm no fan of inheritance either, but I'm not sure it's that cut-and-dry. As a rust example, it's not uncommon to see struct Horse {...} enum Quadruped { Horse(Horse), ... }; possibly alongside a Horse or Horse-like trait (perhaps `HorseImpl`, in which case you'd see `impl HorseImpl for Horse` which, at best, parses...strangely to human beings. "The implementation of the HorseImpl(ementation) for Horses" -- what??). Then, to what `Horse` do you refer when you say the word without a ton of heavy formality?
- RHSeeger 3y agoThe problem is definitely with naming. Regardless of whether you have function neigh() { ... } or class horse { method neigh() { ... } You still wind up with "add functionality for this _slightly different case_" and the code gets modified in exactly the same way. It's not until you clear up that you're dealing with different things, with different names, that you can start to make the code clearer. Because you need to be able to talk about the things in the code.
- librasteve 3y agoA beautifully written piece with lovely images. At some stage in my (ongoing) OO enlightenment, I got to appreciate that in a healthy OO codebase, there should be constant refactoring and code movement in order to adapt to small and large changes (and that there is a lot of judgement in when to opt for a big change when requirements are only incrementally arriving.) While I agree with much of the sentiment about clear naming, I think that many languages with OO capabilities fail to provide easy to use role based composition at their peril. It is a big challenge to do wrestle real code and we need all the tools we can get. Here's how it could look with roles at a coarse grain level: --- role Breedable { ... } role Feedable { method eat-carrot { ... } } role Trainable { ... } class Horse does Breedable does Feedable does Trainable { has $.sound = 'neigh'; ... } class Donkey does Breedable does Feedable does Trainable { has $.sound = 'heehaw'; ... } my $h = Horse.new(); $h.eat-carrot(); --- The benefit of roles is that they let you apply your human language skills (English, French, etc) to combine words as in natural languages - so roles are akin to Adjectives and classes to Nouns, and method names to Verbs. With roles, you can avoid the temptation to hardwire inheritance: --- class Donkey is Horse { ... } --- And it leaves open the option for layers of specialization of roles like this: --- role DonkeyTrainable does Trainable { ... } class Donkey does Breedable does Feedable does DonkeyTrainable { ... } --- And you can use inheritance and role composition side by side: --- class Beast Breedable does Feedable does Trainable { ... } class Horse is Beast { ... } class Donkey is Beast { ... } --- Here's what it says in the docs: _Roles are a collection of attributes and methods; however, unlike classes, roles are meant for describing only parts of an object's behavior; this is why, in general, roles are intended to be mixed in classes and objects. In general, classes are meant for managing objects and roles are meant for managing behavior and code reuse within objects._ btw these examples are in raku ... https://docs.raku.org/language/objects#Roles https://docs.raku.org/language/objects#Roles
- lizmat 3y agoPlease note that in Raku, you can also use a role as a class without having to create a class, through a process called auto-punning. https://docs.raku.org/language/typesystem#Auto-punning https://docs.raku.org/language/typesystem#Auto-punning
- h2odragon 3y ago> “This last one is always sterile,” .. there are fertile mules. Rare, but they exist.
- HideousKojima 3y agoAs I understand it, the offspring they are capable of producing are either horses or donkeys, not more mules.
- d--b 3y ago> The point of this example is that it takes very little for software development to get crippled by complexity without precise language Ah I was wondering where this was headed. It’s a bit disappointing to be honest. Such a build up for such an obvious conclusion. Otherwise it feels like the real world. Start with ifs and refactor when you have too many of them…
- deleted 3y ago[deleted]
- koliber 3y agoSometimes you want to identify things by type, and sometimes by capability. There is a time and a place for each. The article illustrates the case where capabilities were used instead of types to create complexity. It can go the other way too — web browser identification used to be based on naming the creature, but then shifted to capabilities. Know both approaches and use the one that results in a simpler system.
- TehShrike 3y agoThere's nothing inherently wrong with `is_stubborn` or `is_short` flags in your schema, if that's the thing that actually matters. But the `if` blocks given as examples in that post make it clear that there is business logic that cares about something that is higher than just those booleans – logic that is based on some other identity or flag. An `is_donkey` or `is_mule` flag isn't necessarily any better – if you're determining e.g. whether or not the animal can be used in dressage competitions, a `can_compete_in_dressage` boolean would be better than looking for `is_horse`, or God forbid `!is_short && sound === 'neigh' && !is_stubborn`
- SPBS 3y agoIt's not enough to have separate names, implicitly you must also treat them like separate entities and duplicate the code for horses into a separate donkey entity. While the code may be similar at first, changing business requirements means that donkey code and horse code will eventually have to be evolved separately and your future self will be thanking you for duplicating code rather than tangling everything into giant mess. Write! That! Code! Twice! Then once you have more experience in the domain, you can refactor out the parts that are actually semantically identical. Code reuse should be proven, not a given.
- trealira 3y agoAren't there other ways to solve this that are easy to refactor? For example, writing a standalone function for just the common parts of the horse and donkey neigh implementations, and if suddenly some things stop being common to both, move it into the horse or donkey implementations specifically. Or, maybe this idiom (it's C++ but should apply to other languages with inheritance and virtual method equivalents, e.g. Java, C#). Why not something like this? https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Non-Virtual_Interface https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Non-Virtua...
- Fudgel 3y agoThis kinda sound a little bit like trying to figure out your domain modelling.