48: Chaining: Operators, Classes, Calls.
Take Up Code - En podcast af Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes
Kategorier:
Chaining is a common term that has several different meanings. This episode explains how you can use chaining in your programs and how it works. This episode describes four kinds of chaining: Object instance chaining as in a linked list or even from parent to child nodes in a binary tree. Method call chaining as when methodA calls methodB which then calls methodC. Class inheritance chaining which results in a parent-child relationship among your class hierarchy. And finally operator chaining when you have multiple operators in a compound statement. The last type of chaining was the chaining that I had in mind when I started preparing this episode. But then I added the other chaining types too. They seem to fit well and should give you some extra relationships in your own mind to help relate programming topics. Listen to the full episode or read the full transcript below. Transcript I’ve actually already explained most of the concepts but haven’t tied them all together into a single term. Our brains work really well with associations and the more something relates to something else you already know, then the easier it is to learn it. That’s the real reason for this episode. It should help solidify several concepts we already talked about and give you another way to relate them. Anytime you have an instance of a class that points to another instance which points to a third, etc., then you can imagine each of these items forming a link in a chain. I explained this as a linked list earlier in episode 40. A single path through a binary tree from parent node to child node is also a chain. An interesting property of chains is that you can split them anywhere and have two perfectly good chains. And you can join them back together again. But here’s the interesting part. You don’t need to join a portion of a chain back to where it was originally connected. Now this won’t always work very well with a binary tree but for a linked list if you want to move several consecutive items from one list to another, then you might as well move the piece of chained items and leave it intact during the move. This is exactly what you’d do in real life if you wanted to remove several feet of chain from one metal chain and attach it to the end of another chain. Just break one link, open the broken link, and detach the chain portion to be moved. Then you might be able to reuse the broken link or maybe you need a new link but you can then use a joining link to attach the section onto the end of another chain. In programming, anytime you can move a bunch of items by just changing a single item, you have a good solution. Chains don’t always have to be instance of object though. When you have a methodA that calls methodB which call methodC, etc., this is also a chain. This type of chain will be super important to understand when I explain exception handling. Don’t worry, that’s on my list for a future episode. I want to prepare you a bit more though before we get to exception handling. For right now, just understand that each method called had to be called from some other method somewhere. A method also cannot call two other methods simultaneously on the same thread. See what I mean, now I’ve brought up the topic of threads. Let’s not go there for right now. Just know that when a method calls another method, then the method doing the calling stops and has to wait for the called method to complete and return before it can continue. The called method could in turn call other methods of its own and then it also has to stop and wait for each of those methods to complete. When a method completes and returns to the calling method, you can think of this as removing the current last link from a chain of methods. As your program executes, it will call various methods which could call into a deep nesting of methods before backing out again. You program probably won’t back out all the way b