89: Design Patterns: Dirty Flag.

Take Up Code - Un pódcast de Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes

Categorías:

What is the dirty flag design pattern? The dirty flag behavioral pattern avoids work that would need to be done again anyway. This simple pattern explains how to add a bool value to your class that can be set anytime a property changes. This means results need to be re-calculated when they’re requested. The bool value can then be cleared. Even though this is a simple design pattern, there are some things you’ll need to consider: Do you need it? This design pattern works well when the results to be calculated are difficult or resource intensive to compute. You want to save them. You also don’t want to be calculating them several times in a row when only the last one counts. When do you set the dirty flag? Make sure that you set the dirty flag within the class itself whenever an important property changes. This property should affect the result of the calculated result and by changing the property, that makes the last result invalid. When do you clear the dirty flag? It might seem obvious that the dirty flag should be cleared whenever the result is calculated with up-to-date information. But there are other times when you might want to clear the flag. Listen to the full audio episode or you can also read the transcript below. If you’d like to read the book that describes this pattern along with diagrams and sample code, then you can find Game Programming Patterns at the Resources page. You can find all my favorite books and resources at this page to help you create better software designs. Transcript The basic description says that this pattern avoids unnecessary work by putting it off until it’s actually needed. Let the procrastinators rejoice because this design pattern is a perfect fit. We’ve all been taught to get our work done as soon as possible and avoid putting off what can be done today. But would you wash a coffee cup after every sip? Would you save a document that you’re writing on your computer after every letter typed? Would you prepare your income taxes after every paycheck? All of these are crazy, right? Nobody would do such thing. But a computer will if you’re not careful. Of course there’s got to be a balance here. While you don’t want to save an important document after every tiny change, you also don’t want to wait until the end either. If you lose power or the document editor crashes, then you just lost all the work since the last time you saved your work. Maybe then you’ll wish you had been saving more often. There are also times where it is better to perform a task right away. Let’s say you have to add up a bunch of numbers and they just keep coming. You don’t even know how many there will be. You could save them somewhere until you have all of them and then add them. But that would be taking this pattern the wrong way. Whenever you can maintain a running total, then you’ll usually be better to just do the work as it comes along. This pattern helps when the work that you could do right away will become outdated and useless a moment later. And if it takes a lot of resources, either in terms of memory used, or time required, or battery power consumed, to do work that just needs to be redone anyway, then you really should avoid this. First of all, what is a dirty flag and how can it help? A flag in programming terms is just a value usually stored as a member variable of a class that is either true or false. Sometimes you’ll hear the terms set and cleared. Or on or off. These all mean the same thing. A flag is nothing more than a boolean value. If your programming language doesn’t support bool data types directly, then you can use a numeric value and store 1 for true and 0 for false. Sometimes this is called a bit because a binary bit also can only be either 0 or 1. A flag helps anytime you need to remember if something is on of off, if something’s been done or not, or in this case if something’

Visit the podcast's native language site