Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:04,290 --> 00:00:08,880
So here is a very commonly repeated quote about computer science.
2
00:00:09,240 --> 00:00:14,880
There are only two hard things in computer science cache invalidation and naming things.
3
00:00:15,040 --> 00:00:22,440
And this is usually attributed to Phil Carlton, an engineer who worked at silicon graphics and Netscape,
4
00:00:22,440 --> 00:00:24,870
among other famous companies.
5
00:00:25,380 --> 00:00:31,380
And this is a very sometimes added to this quote, by the way, is and off by one errors.
6
00:00:31,390 --> 00:00:35,730
So there's only two hard things cache invalidation naming things and off by one errors.
7
00:00:36,180 --> 00:00:41,520
I tend to think that really the first two of the most important, although that second one is kind of
8
00:00:41,520 --> 00:00:41,820
funny.
9
00:00:42,210 --> 00:00:45,570
So what is cached cache invalidation?
10
00:00:45,570 --> 00:00:47,100
We'll get to naming things in just a second.
11
00:00:47,100 --> 00:00:52,950
I think it's the easiest thing to unpack, but cache invalidation doesn't necessarily mean something
12
00:00:52,980 --> 00:00:56,460
if you haven't necessarily got a theoretical grounding in computer science.
13
00:00:57,030 --> 00:00:59,370
So what do we mean by caching?
14
00:00:59,760 --> 00:01:02,070
Well, caching is when we store of value.
15
00:01:02,070 --> 00:01:04,860
That's for performance reasons or for ease.
16
00:01:05,099 --> 00:01:06,780
We could have just calculated.
17
00:01:07,530 --> 00:01:13,920
So for example, we've got a square and the only information we need to know about a rectangle in this
18
00:01:13,920 --> 00:01:14,110
case.
19
00:01:14,330 --> 00:01:17,670
Only thing we need to know about a rectangle is its width and height.
20
00:01:18,330 --> 00:01:23,100
And from that, we can have set width height on this.
21
00:01:23,640 --> 00:01:29,430
If this was a class, we could also have a get area method and we could calculate the area using the
22
00:01:29,430 --> 00:01:30,900
width and height by multiplying them.
23
00:01:31,920 --> 00:01:38,460
However, if we decided, Oh, we want to do this for performance reasons or just for simplicity's sake,
24
00:01:38,460 --> 00:01:41,920
we didn't feel like writing a getter.
25
00:01:42,390 --> 00:01:45,960
We just go and store the area in a new variable.
26
00:01:45,960 --> 00:01:50,850
So we've just added another piece of state, another variable into the mix in this class.
27
00:01:51,360 --> 00:01:56,910
And because it's a cache, because it is derived from other data and could easily be read arrived,
28
00:01:57,420 --> 00:02:04,210
there is a problem of invalidating this cache every time I call set width or set height.
29
00:02:04,230 --> 00:02:08,130
I need to remember to write code that updates the area.
30
00:02:08,160 --> 00:02:10,680
Otherwise, this cache becomes invalid.
31
00:02:10,680 --> 00:02:18,720
The very variable stored in the area is now no longer the real area, not representing the current width
32
00:02:18,720 --> 00:02:20,730
and height of this rectangle.
33
00:02:21,780 --> 00:02:24,750
So that will seems like, OK, well, I've just got to remember to store it in two variables.
34
00:02:24,750 --> 00:02:28,110
I can go ahead and do that, and at the time, that will seem like a good idea.
35
00:02:28,110 --> 00:02:34,020
Then somebody else will come along and write a method like double width and height and you.
36
00:02:34,500 --> 00:02:39,030
And they won't remember when know that area is a cache and needs to be updated every time.
37
00:02:39,030 --> 00:02:43,920
Width and height is updated, so they just go ahead and naively double the width and height and think
38
00:02:43,920 --> 00:02:45,000
everything will be fine.
39
00:02:45,210 --> 00:02:45,780
It isn't.
40
00:02:45,930 --> 00:02:52,680
Area becomes invalid every time this method is up is called, and that is how bugs are introduced by
41
00:02:52,680 --> 00:02:53,580
cache invalidation.
42
00:02:53,590 --> 00:02:55,230
Now, this is a trivial example.
43
00:02:55,440 --> 00:03:01,410
However, in reality, these bugs get far more obscure and hard to pick up, so that's why it's really
44
00:03:01,410 --> 00:03:07,530
important whenever introducing new variables that you think very carefully about all my caching something.
45
00:03:07,950 --> 00:03:09,960
Could I just calculate this some other how?
46
00:03:10,560 --> 00:03:16,590
Here are a few a couple of common signs that you can look out for just to save yourself the hassle of
47
00:03:16,590 --> 00:03:18,620
introducing the caching in the first place.
48
00:03:18,640 --> 00:03:21,300
If, if in doubt, calculate don't cache.
49
00:03:21,600 --> 00:03:27,240
And even if it's not that efficient, you think it might cost you something in terms of efficiency in
50
00:03:27,240 --> 00:03:27,660
the long run?
51
00:03:27,930 --> 00:03:32,610
Trust me, it probably won't, and it will cost you in terms of bugs.
52
00:03:33,120 --> 00:03:35,880
So a common thing to say is Boolean flags.
53
00:03:35,880 --> 00:03:42,900
If if you've got a Boolean around the place, for some reason, Brazilians tend not to be tend not to
54
00:03:42,900 --> 00:03:48,150
be state that there are some states, but for example, a common Boolean flag might be is jumping.
55
00:03:48,390 --> 00:03:51,620
Could you not calculate whether you are off the ground instead?
56
00:03:51,630 --> 00:03:56,490
Because then some other way that you could calculate whether you are jumping rather than storing a Boolean
57
00:03:56,490 --> 00:04:02,010
variable about that, as probably it's probably the right state, you can probably in a lot of cases
58
00:04:02,010 --> 00:04:06,710
with Boolean flags, you can calculate that from some other state.
59
00:04:06,720 --> 00:04:07,710
So that's an important one.
60
00:04:08,670 --> 00:04:09,530
Get components.
61
00:04:09,540 --> 00:04:13,830
So this is unity specific when you have get components and store the result of that in a way you're
62
00:04:13,830 --> 00:04:15,940
actually caching the result of that.
63
00:04:15,960 --> 00:04:19,630
What happens if a new component is added later on?
64
00:04:19,649 --> 00:04:23,520
What happens if that component gets deleted and a new one gets created?
65
00:04:23,520 --> 00:04:27,660
Your cache, your pointer to that component will go out of date.
66
00:04:27,660 --> 00:04:32,070
Now, this is reasonably safe in unity and probably one that you can ignore.
67
00:04:32,070 --> 00:04:34,950
But just be aware that that is a cache that you're creating.
68
00:04:34,950 --> 00:04:37,260
There is not necessarily.
69
00:04:37,890 --> 00:04:42,870
If you've if you think of the state as being, what is the current component on this game object, you're
70
00:04:42,870 --> 00:04:44,220
not necessarily going to get that.
71
00:04:44,220 --> 00:04:47,460
You're going to get the component that was on the game object at a wake.
72
00:04:47,790 --> 00:04:52,380
So that was a cache having to remember to update something in code.
73
00:04:52,390 --> 00:04:56,760
So if you've got some function and you always find yourself saying to yourself, I need to remember
74
00:04:56,760 --> 00:04:57,870
that that needs to be updated.
75
00:04:58,260 --> 00:05:00,000
Then you probably have a cache.
76
00:05:00,930 --> 00:05:03,540
Updating variables on callbacks is quite common.
77
00:05:03,540 --> 00:05:03,710
One.
78
00:05:03,830 --> 00:05:09,680
If, like on collision and on collision and on collision exit, you find yourself again update something
79
00:05:09,680 --> 00:05:15,590
like a Boolean flag or maybe something else, you might find that you have got yourself into a situation
80
00:05:15,590 --> 00:05:20,480
where you have to do that with callbacks because you are trying to keep a cache up to date.
81
00:05:20,960 --> 00:05:25,760
It's not to say that all caches are bad and that you should never cache but avoid it if you can help
82
00:05:25,760 --> 00:05:25,880
it.
83
00:05:25,910 --> 00:05:29,300
Now on to the slightly easier, good naming.
84
00:05:29,540 --> 00:05:31,610
Part of that quote?
85
00:05:32,330 --> 00:05:34,310
Here are some good naming tips.
86
00:05:34,670 --> 00:05:40,490
So typically, the measure of a code quality is w.e.f per minute.
87
00:05:40,550 --> 00:05:42,160
Well, at least that's a nice way to think about it.
88
00:05:42,170 --> 00:05:44,990
You want to have whoever is looking at your code.
89
00:05:45,320 --> 00:05:51,560
Be confused as few times as possible when they're reading it, and that includes you in the future.
90
00:05:51,570 --> 00:05:58,190
So you want to name things so that everybody understands what you're talking about us where good naming
91
00:05:58,190 --> 00:06:00,500
comes in, it's all about good communication.
92
00:06:00,500 --> 00:06:02,630
Ultimately, code is communication.
93
00:06:03,620 --> 00:06:04,990
Something like this is terrible.
94
00:06:05,000 --> 00:06:12,320
You've got an integer named T. And then in a comment, you're saying seconds since the event happened.
95
00:06:12,560 --> 00:06:17,690
So second, since I did something like the character was shot or the character died.
96
00:06:18,110 --> 00:06:18,920
Those kinds of things.
97
00:06:18,920 --> 00:06:22,550
But you've called the variable T. So anyway, you're using this in the code.
98
00:06:22,760 --> 00:06:26,900
You'd have to go and find this place where it's declared, find the comment.
99
00:06:27,230 --> 00:06:28,880
You're going to go WCF.
100
00:06:28,880 --> 00:06:31,270
What is this t about?
101
00:06:31,280 --> 00:06:32,690
What am I supposed to do with it?
102
00:06:33,050 --> 00:06:37,340
If you'd named it something sensible to begin with, like seconds since event happened, even if it's
103
00:06:37,340 --> 00:06:43,970
a long variable name everywhere that variable is used, you will know exactly what it's for.
104
00:06:43,970 --> 00:06:49,160
And it's going to be clear to the reader of the code, including yourself in the future, that this
105
00:06:49,160 --> 00:06:50,390
was what it was meant to do.
106
00:06:51,920 --> 00:06:57,020
General rule of a good practice is that classes should be named as nouns.
107
00:06:57,950 --> 00:07:02,190
Avoid names such as manager, controller, processor.
108
00:07:02,210 --> 00:07:06,290
It's really not clear what a manager does or what a controller or processor do.
109
00:07:06,740 --> 00:07:09,590
They are kind of cop out names, in my opinion.
110
00:07:09,590 --> 00:07:12,290
We add them because we don't really know what this thing is supposed to do.
111
00:07:12,290 --> 00:07:13,730
It's an audio manager.
112
00:07:13,730 --> 00:07:14,650
What does it do with audio?
113
00:07:14,660 --> 00:07:17,120
What exactly is its purpose?
114
00:07:17,480 --> 00:07:19,230
Is it an audio source?
115
00:07:19,250 --> 00:07:20,780
Is it an audio player?
116
00:07:21,020 --> 00:07:26,090
Things like that are much more meaningful than calling it a manager or a controller or processor.
117
00:07:26,120 --> 00:07:32,090
So if you find yourself and I find this quite often that I want to call something a manager, stop and
118
00:07:32,090 --> 00:07:34,380
ask yourself, what does it actually do?
119
00:07:34,400 --> 00:07:36,290
What do I really want to call this thing?
120
00:07:37,520 --> 00:07:43,700
Methods should be verbs, typically, so you do something with a method to a class, which is a noun.
121
00:07:44,180 --> 00:07:50,660
So those are good rules to follow in terms of object oriented programming and be consistent.
122
00:07:50,750 --> 00:07:55,280
Once you've chosen to call something something, use that term all the time.
123
00:07:55,280 --> 00:07:59,930
Don't go switching between terms and confusing the heck out of somebody who is reading it something
124
00:07:59,930 --> 00:08:04,100
like fetch value and then later having a get value in a retrieve value will leave.
125
00:08:04,100 --> 00:08:09,500
People wondering is fetch different to get or is retrieve different to fetch?
126
00:08:09,770 --> 00:08:11,180
You just don't know.
127
00:08:11,180 --> 00:08:16,640
As the reader of that code, if you have chosen the exact same word, then you are sure that the same
128
00:08:16,640 --> 00:08:17,960
thing is probably happening.
129
00:08:18,590 --> 00:08:23,960
And finally, if you're not sure, go and talk to somebody else about how to name something.
130
00:08:23,960 --> 00:08:28,910
You'll often find that they go, Oh, well, what you're talking about is blah blah blah, and they
131
00:08:28,910 --> 00:08:33,130
know exactly what word you intended to use or so on.
132
00:08:33,760 --> 00:08:40,460
I will often find that I have very lengthy discussions about naming something Well, we do this inside
133
00:08:40,460 --> 00:08:44,510
the team very regularly will go and talk about like, what should we call this?
134
00:08:44,780 --> 00:08:49,970
And we will spend 15 20 minutes talking about naming something very specific.
135
00:08:50,210 --> 00:08:53,390
And that's because naming so, so important in communications.
136
00:08:53,390 --> 00:08:54,590
So don't underrate it.
137
00:08:54,590 --> 00:08:55,550
Spend time on it.
138
00:08:55,820 --> 00:08:58,580
Good naming will save you a lot of time later on.
139
00:08:58,880 --> 00:09:04,550
So there are going to be lots of challenges like this one in this course to share your own examples
140
00:09:04,550 --> 00:09:09,350
from your own experience, because I think this is much more interesting than my clinical examples that
141
00:09:09,350 --> 00:09:11,030
I can come up with on the spot.
142
00:09:11,510 --> 00:09:15,140
So where have you used cash in your code?
143
00:09:15,140 --> 00:09:18,080
Go and share in the discussions for this lecture.
144
00:09:18,320 --> 00:09:19,910
Where have you cashed something?
145
00:09:20,510 --> 00:09:23,990
Where have you ever had a cache invalidation bug?
146
00:09:23,990 --> 00:09:28,610
Not everyone will have had this if you've been programmed for a short while, but if you've been probing
147
00:09:28,610 --> 00:09:30,280
for a long while, I'm sure you have.
148
00:09:30,290 --> 00:09:32,750
Can you point to any cases where you like done it?
149
00:09:32,960 --> 00:09:36,980
I should have updated that variable and I forgot to, and it led to a bug.
150
00:09:37,760 --> 00:09:41,600
What is your variable name of shame?
151
00:09:41,930 --> 00:09:46,220
What is the worst name you've given to a variable function or class?
152
00:09:46,570 --> 00:09:54,350
And let me know down in the discussions I would love to read and share my own there as well.
153
00:09:54,350 --> 00:10:01,310
In the next lecture, we're going to be looking at the sources of spaghetti ification spaghetti fying
154
00:10:01,580 --> 00:10:03,530
in your code, and I'm not talking.
155
00:10:03,570 --> 00:10:07,980
About the kind of tasty sources I'm talking about, the points where they originate from.
156
00:10:08,250 --> 00:10:10,320
Let's dive in to that lecture.
15824
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.