1 00:00:05,280 --> 00:00:07,280 -Hi. In this video, we are going to look 2 00:00:07,600 --> 00:00:10,920 at how the Debugger might help you to understand your methods better. 3 00:00:11,240 --> 00:00:13,640 I chose to use the package MyCounter 4 00:00:13,960 --> 00:00:17,000 that we implemented in the redo of weeks 1 and 2. 5 00:00:17,320 --> 00:00:21,400 Basically, we are going to look at this test right here, "testCounterDecrement", 6 00:00:21,720 --> 00:00:26,840 that was using and initiating a Counter starting at 5 and decremented two times, 7 00:00:27,160 --> 00:00:31,680 and checking that the instance variable count was actually 3. 8 00:00:32,160 --> 00:00:35,080 We can open the Debugger on this instance 9 00:00:35,400 --> 00:00:38,720 by right-clicking and going over to Debug tests 10 00:00:39,040 --> 00:00:41,120 or pressing the shortcut Cmd+D. 11 00:00:42,080 --> 00:00:45,440 By clicking on that, you can see that you have got your Debugger open 12 00:00:45,760 --> 00:00:47,920 with "Break" written on top of it. 13 00:00:48,240 --> 00:00:51,200 This is because it has created a breakpoint 14 00:00:51,520 --> 00:00:53,960 right at the beginning of your test method. 15 00:00:54,280 --> 00:00:57,440 Within the Debugger, you can see that there are three different areas 16 00:00:57,760 --> 00:01:01,320 that are really important. You have got, first, the execution Stack on top. 17 00:01:01,640 --> 00:01:05,800 This execution Stack is composed of all the different methods that were called 18 00:01:06,120 --> 00:01:09,080 in order to give the test the ability to execute itself. 19 00:01:09,400 --> 00:01:13,480 You have got a lot of graphical or process creation methods. 20 00:01:13,800 --> 00:01:16,760 But the one that is the most interesting to us is the last one. 21 00:01:17,080 --> 00:01:19,480 The actual call to testCounterDecrement. 22 00:01:19,800 --> 00:01:26,520 This area here is the body of the source of the method that is being executed. 23 00:01:26,840 --> 00:01:28,960 The highlighted portion is a version 24 00:01:29,280 --> 00:01:31,920 that is currently being inspected by the Debugger. 25 00:01:32,240 --> 00:01:35,000 We can get into that more later on. 26 00:01:35,320 --> 00:01:38,440 The final part consists of all the different variables 27 00:01:38,760 --> 00:01:43,680 that the system has for now and their value at this time in the execution. 28 00:01:44,000 --> 00:01:47,120 Finally, we can navigate in the execution Stack and in the Debugger 29 00:01:47,440 --> 00:01:49,080 with the different functions on top of it. 30 00:01:49,400 --> 00:01:53,640 Basically, Proceed will resume the execution up to either its end 31 00:01:53,960 --> 00:01:58,640 or the next error. Restart will relaunch the debug operation. 32 00:01:58,960 --> 00:02:01,480 Then, Into and Over are the ones we will use right now. 33 00:02:01,800 --> 00:02:05,560 Into helps you dive into the source code of the highlighted method 34 00:02:05,880 --> 00:02:09,160 while Over will make the execution go to the next method. 35 00:02:09,480 --> 00:02:12,120 If I press Over, we can see that right now 36 00:02:12,440 --> 00:02:14,280 startingAt is the method being highlighted. 37 00:02:14,600 --> 00:02:17,800 This is the method we implemented in the class side of the counter. 38 00:02:18,120 --> 00:02:22,240 If you would like to have a look at it, we can simply press Into. 39 00:02:22,560 --> 00:02:26,360 This will bring us to another step in the execution Stack. 40 00:02:26,680 --> 00:02:29,360 We are now in the startingAt in the Counter class side. 41 00:02:29,680 --> 00:02:32,040 You can see that "new" is now highlighted. 42 00:02:32,360 --> 00:02:36,160 We do not really want to dive in the "new" source code, 43 00:02:36,480 --> 00:02:40,440 so what we will do is press Over. This will highlight the affectation. 44 00:02:40,760 --> 00:02:44,880 And you can see here that, for now, c has the value nil. 45 00:02:46,600 --> 00:02:48,120 When we will press Over, 46 00:02:48,800 --> 00:02:51,600 you can see that it has now the value of "a Counter". 47 00:02:52,160 --> 00:02:58,680 We can look into it and see that for now its attributed count is equal to nil. 48 00:02:59,000 --> 00:03:01,640 But we will set it right away right here. 49 00:03:01,960 --> 00:03:05,360 Let us dive into this count to see if it is actually a setter. 50 00:03:05,680 --> 00:03:08,360 We can see that it is a setter. A nice thing we can do, 51 00:03:08,680 --> 00:03:11,840 we can highlight, for example, anInteger, press Cmd+D to print it. 52 00:03:12,160 --> 00:03:14,200 You can see that it is equal to 5. 53 00:03:14,720 --> 00:03:17,080 You can see that this part there is equal to nil 54 00:03:17,400 --> 00:03:19,360 while this part here is equal to 5. 55 00:03:20,600 --> 00:03:23,880 This is extremely useful. We press Over. 56 00:03:24,200 --> 00:03:25,960 This will execute the method count 57 00:03:26,280 --> 00:03:31,000 and we can now look at our temporary variable, c, 58 00:03:31,560 --> 00:03:36,680 and look on the Raw tab that it now has a value 5 as its count attribute. 59 00:03:37,000 --> 00:03:39,320 This will return the c, OK? 60 00:03:39,640 --> 00:03:44,600 Right now, we came back to our testCounterDecrement body, 61 00:03:44,920 --> 00:03:47,800 and we can see that c is still equal to nil. 62 00:03:48,480 --> 00:03:52,120 But we are highlighting the affectation, so by pressing Over, 63 00:03:52,440 --> 00:03:56,120 we will give c a value, the value of "a Counter" starting at 5. 64 00:03:56,440 --> 00:03:59,520 Now, it is equal to "a Counter" and if we go to the Raw tab, 65 00:03:59,840 --> 00:04:01,600 you can see that count is equal to 5. 66 00:04:02,160 --> 00:04:05,840 This is really helpful to understand the objects you work with. 67 00:04:06,160 --> 00:04:09,240 Right now, the decrement method is highlighted. 68 00:04:09,560 --> 00:04:13,160 Once again, we dive into and look at, for example, count. 69 00:04:13,480 --> 00:04:16,760 Now, it is equal to 5 and "count -1" is equal to 4. 70 00:04:17,080 --> 00:04:19,440 So, basically it will simply do this affectation. 71 00:04:19,760 --> 00:04:22,280 Press Over, Over, Over. 72 00:04:22,600 --> 00:04:24,920 If we look back at our counter, 73 00:04:25,240 --> 00:04:30,240 we can now see that its count is equal to 4. Press Over again, 74 00:04:30,560 --> 00:04:35,240 Over again, and now we can look back at our c. 75 00:04:35,560 --> 00:04:40,320 It should have a count equal to 3 because we decremented two times. 76 00:04:40,920 --> 00:04:44,240 We can inspect this and we know that it is equal to 3, 77 00:04:44,560 --> 00:04:47,240 so everything should be fine, we can press Proceed. 78 00:04:47,560 --> 00:04:51,080 This is a small look into the Debugger, 79 00:04:51,400 --> 00:04:53,240 but it is really helpful for you to understand 80 00:04:53,560 --> 00:04:56,920 both the workflow of your methods and your objects better. 81 00:04:57,240 --> 00:04:59,120 Another thing that is important to note 82 00:04:59,440 --> 00:05:02,280 is that you can do the same thing from within the Playground. 83 00:05:02,600 --> 00:05:05,600 For example, if I copy/paste this piece of code, 84 00:05:05,920 --> 00:05:09,600 highlight it, right-click, and go over to Debug it, 85 00:05:09,920 --> 00:05:12,200 or you can press the Cmd+Shift+D shortcut, 86 00:05:12,520 --> 00:05:15,760 it will open the Debugger where you can do basically the same thing. 87 00:05:16,080 --> 00:05:20,600 If I press Over a couple of times, we can look inside the value of c, 88 00:05:20,920 --> 00:05:24,120 see that it is a counter with a value of 5 as count. 89 00:05:24,440 --> 00:05:28,320 We can decrement it, look back at it, it is equal to 4. 90 00:05:28,640 --> 00:05:33,080 You can do a couple of things and the last thing that you should not forget 91 00:05:33,400 --> 00:05:36,400 is that the Debugger is a working environment, and if, for example, 92 00:05:36,720 --> 00:05:42,080 I dive into the method decrement, and I change it to -2, 93 00:05:42,400 --> 00:05:44,080 and I save this, because you can save this. 94 00:05:44,400 --> 00:05:46,280 The orange corner meaning that it is not saved. 95 00:05:46,600 --> 00:05:48,120 So, I save it pressing Cmd+S, 96 00:05:48,440 --> 00:05:54,640 and my decrement method is now this: "count := count -2". 97 00:05:54,960 --> 00:05:57,880 If I press Proceed... I can just close it. 98 00:05:58,200 --> 00:06:01,400 If I go over to my test, it might not work 99 00:06:01,720 --> 00:06:04,960 because we changed our decrement method, and if I click on it, 100 00:06:05,280 --> 00:06:07,480 you can see that I obtain 1 instead of 3. 101 00:06:07,800 --> 00:06:10,240 So, the Debugger is a working environment, 102 00:06:10,560 --> 00:06:14,040 and it will really help you understand the flow of your methods 103 00:06:14,360 --> 00:06:18,160 and your objects better. So I can only encourage you to use it.