"Instance" is just a big word for "one of a thing". Just like a string, integer, or float, a class is a custom type that has some special properties.Īn object is just an instance of a class type. Classes Allow for Even More ReusabilityĪ class is a special type of value in an object-oriented programming language like Python. We call that DRY (don't repeat yourself) code. However, imagine if we had done this hundreds of times in ten or twenty different code files! All of sudden, it makes a lot sense to stop repeating yourself and write more reusable functions. It's not a big deal when two lines are the same and exist right next to each other. In the second example, we only need to make the change in one place. In the first example, we would need to update two lines of code. In our example, let's pretend the soldier dictionary changed, and now the key that stores the "damage" value is called dmg. When code is duplicated, it leads to many potential problems. We don't want too much of our code doing the exact same thing. Soldier_two_dps = get_soldier_dps(soldier_two) Soldier_one_dps = get_soldier_dps(soldier_one) We can use a function to refactor the code a bit: def get_soldier_dps(soldier): Soldier_two_dps = soldier_two * soldier_two Let's pretend we have some code that looks like this: soldier_one_dps = soldier_one * soldier_one The best way to stay sane as a software engineerĪ couple of examples of "clean code" practices include writing good comments, using DRY code, and naming variables well, just to name a few.A way to make new feature development faster.A way to make finding and fixing bugs easier. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |