1 00:00:00,580 --> 00:00:03,490 Let's take a second look at what we just wrote here. 2 00:00:03,490 --> 00:00:06,110 See how I'm using the number one here? 3 00:00:06,110 --> 00:00:09,410 Right now it's obvious why we're passing one here. 4 00:00:09,410 --> 00:00:12,630 That's the range we want the tower to have but 5 00:00:12,630 --> 00:00:16,530 down here, one is the amount to decrease the invaders health by. 6 00:00:17,790 --> 00:00:22,740 It may be obvious now but if we were to show this code to other programmers, 7 00:00:22,740 --> 00:00:28,970 it might not be entirely obvious why we choose to use one both here and here. 8 00:00:28,970 --> 00:00:32,470 Some might think that I decided to have the health decreased by one 9 00:00:32,470 --> 00:00:34,590 because the range is one. 10 00:00:34,590 --> 00:00:38,170 That wasn't my reasoning but that isn't very obvious to the reader. 11 00:00:39,330 --> 00:00:42,060 Numbers like these are called magic numbers 12 00:00:42,060 --> 00:00:46,030 because often time when we see numbers appear in code like this, 13 00:00:46,030 --> 00:00:50,260 it isn't very obvious why that particular number is being used. 14 00:00:50,260 --> 00:00:52,585 We can make this code more descriptive and 15 00:00:52,585 --> 00:00:55,510 self-documenting by using constant variables. 16 00:00:56,520 --> 00:00:59,080 Constant variables are variables whose values 17 00:00:59,080 --> 00:01:00,690 can't change after they've been created. 18 00:01:01,770 --> 00:01:06,370 Any variable can be declared constant by using the const keyword. 19 00:01:06,370 --> 00:01:08,713 We can declare them as method variables like so. 20 00:01:13,448 --> 00:01:18,320 If declared in a method, they can only be used within that method. 21 00:01:18,320 --> 00:01:22,570 Declaring constant variables inside a method is fairly rare. 22 00:01:22,570 --> 00:01:25,410 In some cases it makes sense to do this but 23 00:01:25,410 --> 00:01:28,920 what's more common is declaring them inside classes like so. 24 00:01:30,870 --> 00:01:33,080 This way they can be used in more places. 25 00:01:34,410 --> 00:01:37,070 If we were to make it public, any class could access it. 26 00:01:38,090 --> 00:01:40,570 We don't need that, so we'll make it private so 27 00:01:40,570 --> 00:01:43,570 that it can only be used inside the tower class. 28 00:01:43,570 --> 00:01:46,920 Remember, a common convention in C# is to prefix 29 00:01:46,920 --> 00:01:49,500 private variables with an underscore. 30 00:01:49,500 --> 00:01:51,610 We'll do that with the constant private fields too. 31 00:01:52,810 --> 00:01:57,310 Now instead of typing one here, we can type range. 32 00:01:57,310 --> 00:01:59,700 We can do the same thing with this magic number. 33 00:02:01,050 --> 00:02:02,990 Let's call it power. 34 00:02:02,990 --> 00:02:09,648 So up here we'll type private const int _power = 1 and 35 00:02:09,648 --> 00:02:14,333 we'll type power here instead of one. 36 00:02:19,641 --> 00:02:23,625 Now it's clear that we're passing the tower's range into the in range of method. 37 00:02:23,625 --> 00:02:27,295 And we're increasing the health of the invader being fired upon by 38 00:02:27,295 --> 00:02:29,455 how powerful the tower is. 39 00:02:29,455 --> 00:02:32,155 It's also more obvious that even though range and 40 00:02:32,155 --> 00:02:36,360 power are both equal to one they have nothing to do with each other. 41 00:02:36,360 --> 00:02:41,360 By using constant variables we just made our code even more self documenting. 42 00:02:41,360 --> 00:02:45,880 In general when you see numbers in code it's best practice to create appropriately 43 00:02:45,880 --> 00:02:48,540 named constant variables for them. 44 00:02:48,540 --> 00:02:53,540 This is especially important when the same number is being used in various places. 45 00:02:53,540 --> 00:02:56,260 Another bonus of doing this is if you want to change the power or 46 00:02:56,260 --> 00:03:00,440 the range of the tower, we can just change the value of the constants, instead of 47 00:03:00,440 --> 00:03:03,970 changing all the various places in the code where the magic numbers might be. 48 00:03:05,000 --> 00:03:06,590 It's been a while since we compiled, 49 00:03:06,590 --> 00:03:09,490 so let's compile again just to make sure everything's still legit.