9 ms·
Language constraints aside, the real world is not something that can be cleanly modeled without the notion of circular dependencies between things. Not very man
by bob1029 6y ago
Language constraints aside, the real world is not something that can be cleanly modeled without the notion of circular dependencies between things. Not very many real, practical activities can be truly isolated from other closely-related activities and wrapped up in some leak-proof contract.
Consider briefly the domain model of a bank. Customers rely on Accounts (I.e. have one or many). Accounts rely on Customers (i.e. have one or many). This is a simple kind of test you can apply to your language and architecture to see if you have what it takes to attack the problem domain. Most approaches lauded on HN would be a messy clusterfuck when attempting to deal with this. Now, if I can simply call CustomerService from AccountService and vice versa, there is no frustration anymore. This is the power of reflection. It certainly has other caveats, but there are advantages when it is used responsibly.
If you want to understand why functional-only business applications are not taking the world by storm, this is the reason. If it weren't for a few "messy" concepts like reflection, we would never get anything done. Having 1 rigid graph of functions and a global ordering of how we have to compile these things... My co workers would laugh me off the conference call if I proposed these constraints be imposed upon us today.
- titzer 6y agoI don't understand how reflection is involved here? Don't you just have a data model (schema) that can be interacted with?
- Smaug123 6y agoIn F#, you would naturally approach this by defining a Customer independent of the Account (e.g. just containing a name and address), and an Account independent of the Customer (e.g. just containing an ID), and then a Bank which is a mapping of Account to Set<Customer>. What you see as a cyclic dependency, I see as a data type that you haven't reified.
- JackFr 6y agoThis came up in an application I had modeling college football. A Team is a member of a Conference and a Conference is made up of Teams. But during realignment a few years ago when teams were switching all over the place it became apparent that Teams and Conferences were truly independent of each other and that their association itself was a first class object which also needed to capture the Season/Year. Not really rocket science but some times your model is telling you something if you’re willing to listen.
- kd5bjo 6y agoThis is almost exactly the motivational example that Codd used when originally describing relational algebra. He described 5 different data organization schemes for a single problem and designed relational algebra to work with all of them. This ability for the same program logic to work with many different data storage layouts should make changes like you describe less painful to implement. (see page 2) E. F. Codd. 1970. A relational model of data for large shared data banks. Commun. ACM 13, 6 (June 1970), 377–387. DOI:https://doi.org/10.1145/362384.362685 https://doi.org/10.1145/362384.362685 PDF: https://www.seas.upenn.edu/~zives/03f/cis550/codd.pdf https://www.seas.upenn.edu/~zives/03f/cis550/codd.pdf
- gpderetta 6y agoMy dream, when I'll have a few years of free time, is to design a language were the relational table is the primary data structure abstraction.
- kd5bjo 6y agoI’m working on a Rust library for this as my MS project. It calculates the queries inside the type system, so there’s minimal runtime cost.
- swirepe 6y agoThis is a cool idea. What would you need to be able to try this in 2 weeks, instead of a few years? What's the lean slice?
- kd5bjo 6y agoRelational algebra isn’t that hard to implement if you’re willing to sequential-scan all of the time. The massive complexity comes in the query planner: The point of adding an index is to change the space-performance tradeoff of certain operations. The system needs to be able to take advantage of these data layout changes, or there’s little benefit over storing everything in flat arrays.
- 6y ago
- im3w1l 6y agoThis resembles sql a lot. Btw I'm surprised that few languages seem to have in their standard library a many-to-many container which supports efficient lookups in either direction.
- iso8859-1 6y agoWhich languages support this at all? Are you talking about ORM?
- im3w1l 6y agoI'm talking about something like this std::relation<Foo, Bar> foobars; ... auto& bars = foobars.forward(foo); ... auto& foos = foobars.backward(bar); Edit: Boost apparently has it, see e.g. https://stackoverflow.com/questions/1128144/c-maps-for-many-to-many https://stackoverflow.com/questions/1128144/c-maps-for-many-...
- gpderetta 6y agoBoost.Bimap is a special case (and built on top) of Boost.Multiindex that allows indexing a logical table with an arbitrary subset of fields.
- resonantjacket5 6y agoActually yeah that is kinda interesting. The couple times I've had to do it, always ended up hand-rolling a 'class' with hashmaps in both directions. I wonder why few standard libraries have a both way look up structure.
- piaste 6y ago> and then a Bank which is a mapping of Account to Set<Customer> The problem is that this only lets you get customers from accounts, and not vice-versa. And if you add a Map<Customer, Set<Account>> to the Bank, you now have to ensure the mappings are synchronized at all times. Which, to be fair, is very straightforward as long as Bank is immutable, but still kind of annoying. But considering how incredibly common many-to-many relationships are in business logic and especially in SQL, I'm constantly surprised that there isn't a de facto standard, efficient data structure to represent them. That speaks to how entrenched cyclic relationships are in software engineering, I suppose.
- Smaug123 6y agoSure, but you wrap that up if you want. A Bank can contain both maps, if you want, and you hide the methods that would update individual maps, only exposing your wrappers that update everything together.
- piaste 6y agoYeah, I'm not saying it's difficult, I'm just saying that it's weird that there isn't a standard ManyToMany<T1, T2> class in most programming languages, even though a functional (if perhaps not performance-optimal) implementation is straightforward and should be very useful at least for DB mappings.
- Smaug123 6y agoTrue, I have implemented it myself multiple times, getting a bit annoyed at its absence without stopping to ask why.
- valand 6y agoIn the in-storage form (database), the mappings is synchronized at all times. Many-to-many relationships in the purest form is described as records of ItemAId/ItemBId pairs. `List<{ account: Account, customer: Customer}>`. There isn't any cyclical relationship in this form. From `List<{ account: Account, customer: Customer}>` one can derive `Map<Customer, Set<Account>>` and `Map<Account, Set<Customer>>`. Bank can have copy of both mappings and use them as double index, synchronizing both mappings as operations goes by, while at the same synchronizing both mappings to the in-storage form. Or not! We're talking mostly about data-modelling in a programming language, which applies to the in-memory form. `Customer { accounts: Set<Account> }` and A `Account { customers: Set<Customer> }` is its in-memory form, a layer of indirection from its actual form, the in-storage `List<{ account: Account, customer: Customer}>`. If informations of Accounts, Customers, and the many-to-many relationship of both are laid flat in its purest, source-of-truth form, the in-storage database entries, why are people suggesting that there is/should be a cyclical relationship in its representative form, the in-memory objects?
- jonhohle 6y agoWhich makes it easy to answer the question “what customers are on this account” and hard to answer the question “what accounts does this customer have”. As a customer, I usually want the latter.
- cratermoon 6y ago> a data type that you haven't reified Insightful. Once you learn to see dependencies as implicitly revealing abstract data types you haven't yet teased out into existence, you start seeing all kinds of places where behavior that ought to be explicit and handled by an abstraction is smeared across multiple other concepts.
- Smaug123 6y agoI've got a half-cooked blog post about this in the oven: "reify the state machine". I've seen plenty of code with lots of communicating parts, which is really a distributed implementation of a state machine. Rewriting to make the states explicit as data, and perhaps creating a central "state machine" type for that machine, can really help clarify the domain model and let you see what you're missing.
- sbelskie 6y agoI’m having a hard time understanding why you think F# is unable to handle the domain model presented.
- mattgreenrocks 6y agoIt reads as an extremist take on Chesterton’s Fence: “the fence exists, therefore it is probably the only thing that could’ve worked.” There is nothing in this contrived example that F# or Haskell (gasp, pure functional) wouldn’t handle with ease. It just would be modeled differently from traditional langs. I’d argue the alternative modeling forced by FP langs would be superior.
- hutzlibu 6y ago" I’d argue the alternative modeling forced by FP langs would be superior." Then I'd like to see that. The other example of the functional someone gave more above, was not convincing to me. Much more complicated then the straight-forward simple, but evil cycle approach.
- zaphar 6y agoSometimes I read conversations about software engineering concepts by career developers and I get this sense that there is this entire hidden world of other engineers that I have somehow managed never to encounter. Every engineer I've ever met who has been doing it for more than a couple years universally decries circular dependencies. Every one of them has come to that opinion via hard won experience dealing with real world problems they encountered. And yet comments like yours reveal there are other engineers working in places where apparently the opposite is true. Whenever I see that I wonder how this sort of self sorting manages to occur.
- ironmagma 6y agoSome problem domains have unique characteristics. I think the closer a problem resembles nature, the more it breaks our structures we want to impose on it.
- kayodelycaon 6y agoPossible reason: selection bias. If everyone else is fine with circular dependencies or at least sees them as a necessary evil... are they going to extremely vocal about the current status quo when there they have no need to defend it?
- Aeolun 6y agoIn my experience, it’s generally less experienced engineers that will happily make a clusterfuck of circular dependendies. If a more experienced engineer does, it’s generally considered a necessary evil, but not a choice taken lightly.
- TeMPOraL 6y ago> Every engineer I've ever met who has been doing it for more than a couple years universally decries circular dependencies. Every one of them has come to that opinion via hard won experience dealing with real world problems they encountered. They may not realize they have them - at runtime, in the object graph, possibly indirectly. There's little difference in principle between cycles in "static" code vs. runtime state, but we often can't express them in the former, because our languages don't specify the concept of dependency at enough granularity (and some rely on a linear compilation pass).
- Ma8ee 6y agoIn a relational database you would have a CustomerAccount table keeping track of which accounts that belongs to which customers. Customer would not know about Account, and vice versa. And what has reflection to do with this?
- gorky1 6y agoReflection allows you to put the interfaces to Account and Customer into a shared module and the business logic implementations into their own mutually independent modules, thus removing the cyclic compile time dependency. You "wire up" the implementations at runtime, using reflection.
- Ma8ee 6y agoI still don't get it. I put the interface into a shared module and the implementations in independent modules without reflection. Why exactly would we need reflection to do that?
- Smaug123 6y agoIf I may, that sounds horrifying. Please, for the sake of my sanity and the IDE, always do things at compile time rather than runtime if you can!
- ldlework 6y agoIf the code is implementing against interfaces, why would late binding mess up your IDE experience at all?
- kohlerm 6y agocircular dependencies at runtime are not really a problem and often necessary. circular dependencies at build time/between modules are evil.
- jonhohle 6y agoYour build time is someone else’s runtime.
- dgb23 6y agoI don’t quite understand what you’re trying to convey with your example in regards to FP. First of all, both data and functions are first class concepts in the functional paradigm and are not glued together as classes. So you’re not modeling your domain as „account service“ and „customer service“, but rather have data representation of these concepts and functions that query/reduce etc on those. In your example that would be a relational model, described as sets of tuples/maps, and relational functions do derive new data. There is no need for circular dependence, because your operations are generic over relational data. I agree with the notion that the purely functional approach doesn’t dominate, for good reason. But more and more languages are incorporating FP concepts since about a decade or longer, at least the basic building blocks like closures, immutability and function composition have gained massive traction and eliminate the need for many of the complex patterns in traditional, class based OO.
- monocasa 6y agoYou would forget everything about a customer if they closed their last account?
- Smaug123 6y agoNo, of course you would maintain a Set<Customer> as well (possibly even a Map<Customer, Account Set> for efficiency).
- monocasa 6y agoTheir whole premise is that > Customers rely on Accounts (I.e. have one or many). I'm giving a domain relevant counter example to that premise. And interestingly enough, that leads you to a clean way of modeling it without the cyclic dependency as you've just done.
- ImprobableTruth 6y ago... if you model it that way you're not using cyclic dependencies at all and this is in fact exactly how you would model it in a functional way.
- IggleSniggle 6y agoBut functional-only business applications already did take the world by storm! Relational DBs is the defacto way data is organized, and a Junction table is the way you model the data so that a functional query (generally in SQL) can be made against it.
- username90 6y agoSQL is not functional, it mutates global state.
- perl4ever 6y agoSELECT?
- IggleSniggle 6y agoYes, there is a single transactional global state. There is always state somewhere, the question is where it should be. Global state is the input to the program, and a new global state is the output. Given identical inputs, it will always produce the same output. This is the entire principle behind ACID, and mutation of global state is a common approach for functional programmers regardless of whether they they are using a DB or not. If a DB were not fully transactional, or if your queries of the DB are not deterministic themselves, then we’ve moved out of functional territory.
- brundolf 6y agoI think the OP is overly dogmatic and I agree with you that sometimes you just need circular dependencies That said, I don't see what circular dependencies have to do with reflection, much less a functional style? For a trivial example, Rust lacks reflection but the following (functional-ish) code works just fine: mod ModA { use crate::ModB::B; pub struct A { ref_to_b: Option<Box<B>> } pub fn into_b(a: A) -> Option<Box<B>> { a.ref_to_b } } mod ModB { use crate::ModA::A; pub struct B { ref_to_a: Option<Box<A>> } pub fn into_a(b: B) -> Option<Box<A>> { b.ref_to_a } } Am I missing something?
- Smaug123 6y agoCould you give an example of a domain that naturally requires circular dependencies? I have been trying lately to practice seeing outside the functional-programming-tinted lenses, but I need a bit of help to do so :)
- brundolf 6y agoI think the GP's example is pretty reasonable. And like I said, I still don't really see what this has to do with FP. Clearly F# has trouble with it, but so does C++
- Smaug123 6y agoI don't think the GP's example is reasonable, and nor would an SQL-writer. Could you give something I will find it harder to wriggle out of?
- valand 6y agoIn the functional paradigm, language constraints aside, to model this "story" would need these: - The definition of Account - The definition of Customer - A ledger consisting of: List of Account, List of Customer, List of Account-Customer relations A clerk works on the records on the ledger with one hand and one pen, a metaphor for the service process working on the data in the database. An act, such as money transfer, would be described as a function. A customer creating a new account for himself is written as `fn createAccount(Customer, NewAccountData)` because from the perspective of the clerk/bank manager/service the customer, newAccountData, and the existing data in the ledger as objects which the clerk/bank manager/service must move around in a precise way. The module which has 'fn createAccount` depends on the types `Account` and `Customer`. In english it roughly sounds like, "the success of writing the rule of creating an account for a customer depends on knowing the definition of Account and Customer." The function is not written as `customer.accountService.createAccount(newAccountData)`, because the clerk doesn't schizophrenically pretend to be the customer and create an account for himself. The clerk just receive request from the actual customer, writes new account entry and customer-account-relation entry into the ledger, that's it. There's simply no need to call CustomerService from AccountService and vice versa. There's no need for reflections because data types are all available at compile time.
- piva00 6y ago> `customer.accountService.createAccount(newAccountData)` I believe that most OO implementations would read accountService.createAccount(customer, newAccountData) Care to elaborate if that was the main point of the clerk's schizophrenia criticism? Or if I'm misinterpreting just call me dumb, haha.
- valand 6y agoOh, I meant to emphasis on that "customerService depends on accountService and vice versa" thingy. While in my writings "accountService is a member of customer" is a form of customer depending on accountService. But my main point is that it should not be written that way at all. Semantically, if we must include the subject, it should be `theService.createAccount(Customer, NewAccountData)` Or replace theService with CustomerAccountService or whatever. Writing that way, with functions depending on types, avoid getting tangled from the so-called account-customer cyclical dependencies. Because account and customer don't need to know each other until a function needs to know both of them. There's no such thing as `customer.getAccounts()` because in the end the query would roughly look like `getAccountsWhereCustomerIs(CustomerId)`. You're not dumb. It's just me mis-writing due to the fact that I'm writing this at 4 in the morning lol. Or maybe I'm misinterpreted parent comment and that confuses you. In that case, I'm the dumb.
- deleted 6y ago[deleted]
- danielovichdk 6y agoCustomers and Accounts are not cyclic. A customer has an account. The customer is the domain root. Hence there would be no accounts if there were no customers. A customer does need to to hve an account. An account must have a customer. It's not cyclic in any way
- sk5t 6y agoBanks think the other way around - the account is important, the customer less so. Multiple entities, or no entities, might be authorized to access or control the account at various times.
- Someone 6y ago“Customers rely on Accounts (I.e. have one or many)” Alternative model: the Customer relation couples a Person to an Account. I think that’s a better model, as it allows Person P to be a Customer at bank B but not at Bank C, Person Q to be a Customer at both B and C, etc. It also allows you to model Persons before you model Accounts, breaking the circularity, allows Companies to become Customers, etc.
- dataflow 6y agoI actually don't follow your example; I don't see why this is cyclic. In your example what exactly would CustomerService be responsible for that prevents it from functioning unless it has a reference to AccountService? AccountService is pretty obvious (add/remove money, link other accounts, close account, etc.) but I'm struggling to see what CustomerService would do on a per-customer basis that it would need a reference to AccountService for. The only thing I can imagine is a get/add/remove accounts operation, but (a) you need nothing but an account ID for that (not the actual AccountService class) and the caller can look up the actual accounts using the IDs if they want, and (b) you can (and I think perhaps should?) model this as a database table managed by the Account class where you're associating customer IDs with account IDs. Another way to think about this is, how would you do this in C++? You have header files, and they shouldn't be cyclic. Who #includes who? Well in this case it seems to me AccountService.hpp should #include CustomerService.hpp, but not the reverse. Maybe CustomerService.hpp would #include AccountID.hpp? Whatever I imagine I just don't see why you'd need cyclic #includes.
- sillysaurusx 6y agoFunny that we both think in C++. It was what popped into my head too. AccountMgr has many Account(s). Ditto for CustomerMgr. Interact with both to taste.
- cjfd 6y agoOne can have Customer in one header file, possibly forward declaring Account. Account in another header file possibly forward declaring Customer. Definitions in the two cpps which can include both header files. When writing such a thing one should ask oneself whether one really needs to do it that way, though. Not that it is too much of a mess but asking such question is always good.
- deleted 6y ago[deleted]
- deleted 6y ago[deleted]
- rowanG077 6y agoFunctional only actually can model this kind of real world circulair dependencies easily without having circular dependencies in the language level. Not sure what it even has to do with functional style in general though.