1 00:00:00,390 --> 00:00:04,190 Many people consider C# to be a fairly verbose language where 2 00:00:04,190 --> 00:00:05,800 curly braces abound. 3 00:00:05,800 --> 00:00:09,770 On the other hand, C# contains a lot of syntactic sugar. 4 00:00:09,770 --> 00:00:12,620 Syntactic sugar is a feature in a programming language 5 00:00:12,620 --> 00:00:15,410 that helps with writing more concise code. 6 00:00:15,410 --> 00:00:20,500 They provide alternative ways to write the same code using fewer keystrokes. 7 00:00:20,500 --> 00:00:25,700 We recently saw one example of syntactic sugar, the ternary if statement. 8 00:00:25,700 --> 00:00:30,205 So instead of typing this, we only need to type this. 9 00:00:30,205 --> 00:00:33,871 Another example is auto-properties, 10 00:00:33,871 --> 00:00:38,860 where instead of typing this we only need to type this. 11 00:00:38,860 --> 00:00:44,240 The result of using syntactic sugar in general is shorter more concise code. 12 00:00:44,240 --> 00:00:48,510 When code gets long and requires lots of scrolling in order to read it all, 13 00:00:48,510 --> 00:00:51,800 it can get difficult to see how it all works together. 14 00:00:51,800 --> 00:00:54,370 On the other hand, when code gets condensed, 15 00:00:54,370 --> 00:00:56,590 it can also become difficult to read. 16 00:00:56,590 --> 00:01:00,160 This is the balance beam that all programmers need to walk. 17 00:01:00,160 --> 00:01:03,030 Code should be both clear and concise. 18 00:01:03,030 --> 00:01:05,320 But above all, it should be clear. 19 00:01:05,320 --> 00:01:07,470 So when choosing to use syntactic sugar, 20 00:01:07,470 --> 00:01:11,300 we should always ask ourselves, is this clear and more readable? 21 00:01:12,590 --> 00:01:15,510 It turns out we can make the code we've written so far for 22 00:01:15,510 --> 00:01:18,270 the Invader class even more concise. 23 00:01:18,270 --> 00:01:21,470 Let's take yet another look at the Location property. 24 00:01:21,470 --> 00:01:24,450 Really, all this property does is call a single method and 25 00:01:24,450 --> 00:01:27,720 return whatever it returns, nothing more. 26 00:01:27,720 --> 00:01:31,201 We could make this even more obvious by writing it like so. 27 00:01:37,068 --> 00:01:40,990 This line is completely equivalent to the previous code. 28 00:01:40,990 --> 00:01:42,770 Location is still a property. 29 00:01:42,770 --> 00:01:46,699 The Location property reduces to just calling _path.GetLocationAt. 30 00:01:46,699 --> 00:01:50,672 So long as this part on the right side evaluates to a MapLocation, and 31 00:01:50,672 --> 00:01:54,795 you can write it in a single statement, then you can write it like this. 32 00:01:54,795 --> 00:01:58,050 Of course, this only makes sense with computed properties. 33 00:01:59,100 --> 00:02:01,900 We can do something similar with methods. 34 00:02:01,900 --> 00:02:04,550 Let's take a look at the Move method. 35 00:02:04,550 --> 00:02:07,170 The Move method only has a single line in it. 36 00:02:08,390 --> 00:02:09,560 We can rewrite it like so. 37 00:02:13,240 --> 00:02:17,560 This looks very similar to what we just did with the location property. 38 00:02:17,560 --> 00:02:19,450 Move is still a method though. 39 00:02:19,450 --> 00:02:22,000 We know that because we still have the parentheses here. 40 00:02:23,680 --> 00:02:25,280 The other difference between methods and 41 00:02:25,280 --> 00:02:28,790 properties written this way, is that properties always return a value. 42 00:02:29,880 --> 00:02:32,200 Our Move method doesn't return a value. 43 00:02:32,200 --> 00:02:32,990 It's still void. 44 00:02:34,280 --> 00:02:36,500 It just adds one to the _pathStep field. 45 00:02:37,520 --> 00:02:40,300 Because the Move method only contained one line, 46 00:02:40,300 --> 00:02:43,000 we can write it as a single line, like so. 47 00:02:43,000 --> 00:02:45,560 You don't have to write your code this way. 48 00:02:45,560 --> 00:02:47,810 This is purely syntactic sugar. 49 00:02:47,810 --> 00:02:50,950 I'm only showing it to you here because it's likely that you'll see this in other 50 00:02:50,950 --> 00:02:51,500 code you read.