220: One Of These Things Is Not Like The Others.
Take Up Code - A podcast by Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes
Categorie:
Finding differences will help you make sense of your code. We’re fairly good at spotting things that are different. But how good are you at thinking of different ways that something can be similar or different? That’s an important skill you need to develop in order to become a better programmer. Listen to the full episode or read the full transcript below for examples. And if you’d like to participate in programming sessions where I work through issues like what I described in this episode, you can become a patron of the podcast and select your reward level. Transcript Differences will creep into your code as you think of new things it needs to do. It happens to all of us. And if you don’t recognize them, the differences will turn your code into a jumbled mess. There can be different ways to think about things so you have to examine your code from different viewpoints to find things that are similar and things that are different. There’s a popular children’s show called Sesame Street that sometimes shows a game called “One of these things is not like the others.” At least that what I think it’s called. It’s been a while since I watched it. You have four pictures and need to figure out which one is not like the others. Let’s say there’s a picture of a baseball, a man, a bird, and a bat. Which one is not like the others? Most kids will choose the baseball because all the others are pictures of living things while the baseball is not alive. But this is where you can develop your programming skills by thinking of other ways that things are alike. Maybe you said the man is not like the others because the others start with the letter b. This is accurate but not the best programming reason because names don’t usually define behavior. Look for differences in behavior more than other things. Maybe man could be different than the others because all the others fly. But does a baseball really fly? Not really, it gets thrown instead. And while it’s not that easy to toss a man, it can be done. You could think about the baseball being unlike the others because the others can move on their own. That’s similar to being alive. It would make more sense though if the bird was replaced with a picture of a motor. The point is that you can sometimes find multiple similarities and differences. These will change how you think about your code and will affect your design. Getting to the right level of similarities will allow you to solve basic problems once and then reuse that solution in other parts of your code. Then you can focus on just the new differences and not worry about things you’ve already figured out. Think for a moment about our bodies. We’re made up of various organs and systems. Is our skin similar or different than our arm muscles? They seem very different but actually contain a lot in common. If you were programming something like this, you might start out with different classes for skin and muscle since they seem so different. But eventually, you’ll realize that both need food and oxygen and the ability to grow. This is where you can start going down the wrong path if you don’t recognize the similarities. All of our organs are made of cells that are specialized but actually very similar. Even plants are made of plant cells. The cells all perform the same basic functions but can be adapted slightly for skin, muscle, bone, etc. They all have the ability to absorb nutrients through the cell wall and divide and grow. This is where you can benefit by writing code that does the common functionality once and reusing it in all the different places that need it. As an another example, consider the game that I’m working on. I wanted a configuration file to hold some of the data that defines the basic behavior and settings of the game. The configuration file uses properties that each have a name and a value. That’s alrea