• @[email protected]
    link
    fedilink
    English
    71 year ago

    You shouldn’t reach through an object to invoke a method. That tightly couples the classes which getJ and getG (for instance) return.

    • Anomandaris
      link
      fedilink
      21 year ago

      That is an interesting point, but it’s not Java specific, you could do this exact thing in most other languages and it would look pretty much the same.

      Considering the fact that in a lot of enterprise projects the data structures are not necessarily open to change, how would you prevent reaching through objects like this?

      • @[email protected]
        link
        fedilink
        21 year ago

        This is why I wasn’t too critical of Java. Java is verbose by convention and other languages are more terse by convention. You could just as easily write some nasty ‘snake_cased_object_abstract_factory_adapter_facade_broker_manager’ in python or any other language. There are a few things syntax wise working against it but you can still write (overly) terse Java and it’s just as annoying to read as in any other language. IMO it’s convention and style not the language itself. You can also say some mean things about languages with less verbosity but more operators and keywords like C++/rust. It’s a funny meme tho lol anyone who has worked in Java knows there’s at least a bit of truth to it

      • @[email protected]
        link
        fedilink
        English
        11 year ago

        I’ll shrink your example. Suppose you have an object A which has a B which has a C, which is what you need.

        By writing a.getB().getC(), you are implicitly coupling A and C together without A noticing. All A knows is that it is coupled to B. Should B ever decide to use a different C’, which would make more sense for B, it may break your code without noticing it.

        The solution is to make the coupling explicit. A should define a getC function that observes the needed contract. For the time being, it may get its C from B (which is fine, because C is under B’s immediate control), but if B changes, and wants to use C’, you know to look into A (which is already explicitly coupled to B) and see if it can still function. You’d notice that it relied on B’s returning C and can find a solution to this.

        An example with fewer variables: You have a shopping cart, which manages items. Implicit coupling translates to knowing and relying on the fact that the items are stored in an array. Adding an item the bad way would be shoppingCart.getItems()[shoppingCart.getItems().getLength] = item;*

        The proposed solution adds the function ShoppingCart::addItem. Should ShoppingCart switch to a linked list, it can change the implementation of addItem accordingly. Instead of reaching through the cart into the items, you make dealing with the items the problem of ShoppingCart.

        I don’t have copy at hand, so I can’t check. I think this advice stems from “The Pragmatic Programmer” by David Thomas and Andrew Hunt.

        * I don’t actually know Java, so please forgive if this example wouldn’t really work.

    • @Aceticon
      link
      English
      21 year ago

      It’s sad that it took getting so far down the thread before somebody pointed out the obvious program design flaw.

      If you’re digging many levels down into your datamodel from high up for lots of datapieces, it’s probably the case that both the data is unnecessarily framented and the code design itself doesn’t have proper OO isolation of responsabilities.

      If you’re designing for performance (a well know raeson to screw OO design), then the datamodel itself would be a lot flatter (because you get more performance on the DB by trading it for space), whilst if you’re not then you break the thing into parts as you have functions were you fetch intermediate objects into memory and handle the data in them (a Visitor Pattern would probably make this a lot cleaner).

      I bet whomever designed the datamodel isn’t the same as those doing the coding.

      • @[email protected]
        link
        fedilink
        English
        11 year ago

        To be fair, probably hardly anyone actually looked at the code. And now that I did, I think this is compiled, not source code.