Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:04,100 --> 00:00:11,000
So let me start out by making this rather click baity statement and say that you shouldn't use design
2
00:00:11,000 --> 00:00:14,670
patterns, at least not in the way that you typically think.
3
00:00:14,690 --> 00:00:21,410
We don't use design patterns as a library of tools that we hammer into any problem.
4
00:00:21,590 --> 00:00:24,160
This is called the Golden Hammer syndrome.
5
00:00:24,170 --> 00:00:30,140
If you happen to know a tool really well, such as a hammer, then everything you look at, even if
6
00:00:30,140 --> 00:00:35,240
it is a screw, is going to look like a nail and you're going to want to use that hammer to solve the
7
00:00:35,240 --> 00:00:35,690
problem.
8
00:00:36,230 --> 00:00:38,630
So this can happen with design patterns.
9
00:00:38,630 --> 00:00:40,880
People will get familiar with one or two.
10
00:00:41,480 --> 00:00:44,450
They will love those design patterns, understand them really well.
11
00:00:44,840 --> 00:00:50,690
And whenever there is a problem, they might think about applying the design patterns.
12
00:00:50,690 --> 00:00:54,130
They know this is the wrong way round.
13
00:00:54,170 --> 00:01:01,190
We should be looking at the problems and trying to solve those root cause problems rather than trying
14
00:01:01,190 --> 00:01:02,930
to find a solution that fits.
15
00:01:03,530 --> 00:01:11,270
It's good to study design patterns as a way of seeing how certain templates of problems have been solved.
16
00:01:11,690 --> 00:01:17,270
But often you'll find that once you're really good at programming and design patterns, you find yourself
17
00:01:17,270 --> 00:01:22,370
writing design patterns and then only later realizing that you used a design pattern.
18
00:01:22,760 --> 00:01:24,250
That's the thing about design patterns.
19
00:01:24,260 --> 00:01:29,480
It's often very easy once you understand the reasoning behind them to just simply reinvent them as you
20
00:01:29,480 --> 00:01:29,900
need them.
21
00:01:30,260 --> 00:01:36,500
However, in this course, we are going to study them as a way to reinforce our understanding of good
22
00:01:36,500 --> 00:01:38,090
programming principles.
23
00:01:38,210 --> 00:01:40,670
So what are those good programming principles?
24
00:01:40,970 --> 00:01:47,840
Well, I have put together my 10 coding commandments, which as a provides, I should say, are more
25
00:01:47,840 --> 00:01:53,600
like guidelines because a good programmer will also know when to break the rules.
26
00:01:53,900 --> 00:01:59,030
These are things that I've put together and learnt in my experience as a programmer.
27
00:01:59,360 --> 00:02:01,310
You may come up with different ones.
28
00:02:01,310 --> 00:02:02,960
You may disagree with some of mine.
29
00:02:02,990 --> 00:02:03,740
That's great.
30
00:02:04,190 --> 00:02:06,530
It happens all the time with programmers.
31
00:02:06,530 --> 00:02:08,240
Program is like to disagree with each other.
32
00:02:08,509 --> 00:02:11,840
So do mention in the comments on this video.
33
00:02:11,840 --> 00:02:17,630
If you disagree, if you have other coding commandments or best practices that you think should be included
34
00:02:17,630 --> 00:02:19,130
here, I went for 10.
35
00:02:19,400 --> 00:02:24,590
There are obviously loads more, but 10 is a good number when you're talking about commandments.
36
00:02:24,950 --> 00:02:31,940
So commandment number one is a thy code shall read like English or Spanish or Italian or whatever language
37
00:02:32,120 --> 00:02:35,330
you happen to write your code in.
38
00:02:35,540 --> 00:02:39,470
Typically, people all over the world write in English, which is why I've written the rule like this,
39
00:02:40,400 --> 00:02:44,280
and implicitly that should be without even having to include any comments.
40
00:02:44,300 --> 00:02:47,960
I think that code should be self documenting where possible.
41
00:02:48,170 --> 00:02:55,280
That means that the variable names and the function names and the class names should really explain
42
00:02:55,280 --> 00:02:59,030
what this code is doing, which takes me onto point number two.
43
00:02:59,060 --> 00:03:02,180
Thou shalt take time to name things well.
44
00:03:02,210 --> 00:03:07,140
Naming things well is really hard as we will see a little bit in the next lecture.
45
00:03:07,160 --> 00:03:13,400
And so you must take time to think very carefully about good names for things giving a good name to
46
00:03:13,400 --> 00:03:15,570
something that is incredibly powerful.
47
00:03:15,570 --> 00:03:21,020
It allows you to reason about it in a way that you couldn't when you didn't have a name for it.
48
00:03:21,050 --> 00:03:25,560
Imagine trying to talk about a tree without the word tree.
49
00:03:25,580 --> 00:03:31,880
If you were just talking about that big woody leafy thing all the time, you just wouldn't be able to
50
00:03:31,880 --> 00:03:34,130
have a decent arboreal conversation.
51
00:03:34,140 --> 00:03:37,460
So naming things is important and we should take time to do it.
52
00:03:37,460 --> 00:03:39,390
It allows us to read the code easily.
53
00:03:39,390 --> 00:03:43,610
It allows us to comprehend our code takes us on two point number three.
54
00:03:43,610 --> 00:03:47,180
They classes shall do one thing by this.
55
00:03:47,180 --> 00:03:51,660
We mean that they are at a consistent level of abstraction.
56
00:03:51,680 --> 00:03:58,070
If you have got an audio class, an audio player, then it should only do things relating to playing
57
00:03:58,070 --> 00:03:58,490
audio.
58
00:03:58,500 --> 00:04:05,060
It should not do anything related to animation that is obviously outside of the remit of the one thing
59
00:04:05,060 --> 00:04:06,470
that class should do.
60
00:04:06,500 --> 00:04:09,290
Now, obviously, the levels of abstraction here can change.
61
00:04:09,290 --> 00:04:12,650
You could have a class that's responsible for playing the entirety of your game.
62
00:04:12,650 --> 00:04:17,329
Obviously, within that remit, it does a lot of things, but it probably forms out some of that work.
63
00:04:17,390 --> 00:04:23,540
Similarly, you might have a class that is super, super narrow minded that just perhaps spawns an object
64
00:04:23,540 --> 00:04:24,980
and that goes for functions.
65
00:04:24,980 --> 00:04:31,220
Two functions should just do one thing you don't want to function that is this error message and then
66
00:04:31,250 --> 00:04:31,970
do that.
67
00:04:32,000 --> 00:04:36,950
You want functions that just do one thing within their level of abstraction.
68
00:04:36,950 --> 00:04:42,470
Again, they'll usually be a lower level of abstraction than the classes they reside within, but that's
69
00:04:42,470 --> 00:04:43,700
the important part.
70
00:04:43,850 --> 00:04:49,940
Number four, thou shalt use as little state as possible will get into why this is a problem in the
71
00:04:49,940 --> 00:04:50,510
next lecture.
72
00:04:50,540 --> 00:04:56,840
Often, a source of bugs is that you've got a mutable state, things that can change, variables that
73
00:04:56,840 --> 00:05:02,810
can change, and each variable that can change represents a different state of your program could be
74
00:05:02,810 --> 00:05:03,230
in the.
75
00:05:03,400 --> 00:05:08,830
All variables you have, it gets exponentially more states that your program can be in, and so these
76
00:05:08,830 --> 00:05:12,670
are more states for your program to go wrong in.
77
00:05:12,760 --> 00:05:17,470
So using less of that makes your program easier to reason about and less bug prone.
78
00:05:17,590 --> 00:05:21,890
We'll talk a lot more about all of these points throughout the course.
79
00:05:21,910 --> 00:05:26,440
So with this, just an overview at this stage, and we'll go into far more detail.
80
00:05:26,440 --> 00:05:29,560
So don't worry if you don't understand everything at this stage.
81
00:05:29,590 --> 00:05:33,100
Thou shalt not refactor before the third repetition.
82
00:05:33,100 --> 00:05:34,590
This is point number five.
83
00:05:34,600 --> 00:05:40,720
And essentially what it's saying is that we need to be able to see a pattern before we refactor for
84
00:05:40,720 --> 00:05:41,310
that pattern.
85
00:05:41,320 --> 00:05:44,230
If you've done something twice, it may not be a pattern yet.
86
00:05:44,230 --> 00:05:51,340
It might not be something that needs a method taking out or a class creating or any of that stuff until
87
00:05:51,490 --> 00:05:58,780
you've done something for the third time often, then you might find there is time to create a new function
88
00:05:58,780 --> 00:06:03,820
to abstract that shared functionality or a new inheritance hierarchy.
89
00:06:04,030 --> 00:06:05,050
Those kinds of things.
90
00:06:05,050 --> 00:06:09,370
Usually you need to see a pattern emerging first, so don't do that too early.
91
00:06:09,430 --> 00:06:13,840
Number six, thou shalt not use a static and global state again.
92
00:06:13,840 --> 00:06:15,280
We'll see this in a couple of lectures.
93
00:06:15,280 --> 00:06:20,350
Time it goes hand in hand with point number four, which is to use as little state as possible.
94
00:06:20,350 --> 00:06:26,860
Global and static state is doubly bad because it's more state, but also it can be changed anywhere
95
00:06:26,860 --> 00:06:28,780
and accessed anywhere in your program.
96
00:06:28,780 --> 00:06:34,540
So it has a lot more possibility to go wrong and to make other things go wrong in your code.
97
00:06:34,600 --> 00:06:39,280
Commandment seven thy classes shall be highly cohesive goes hand in hand with commandment.
98
00:06:39,280 --> 00:06:39,670
Eight.
99
00:06:39,670 --> 00:06:44,650
Thy classes shall be loosely coupled If you have never heard of these two terms before, I'm going to
100
00:06:44,650 --> 00:06:47,320
go into detail on these in a couple of lectures time.
101
00:06:47,380 --> 00:06:50,890
So hold your horses and we will understand it better than commandment.
102
00:06:50,890 --> 00:06:55,090
Number nine, thou shalt use composition over inheritance.
103
00:06:55,360 --> 00:06:58,420
You may have come across the idea of composition.
104
00:06:58,420 --> 00:07:01,390
You almost certainly came across the idea of inheritance.
105
00:07:01,690 --> 00:07:05,890
Inheritance is where a class is said to be is a something.
106
00:07:05,890 --> 00:07:10,000
So for example, you might have a crocodile is an animal.
107
00:07:10,930 --> 00:07:12,710
Those are the kind of is a relationship.
108
00:07:12,710 --> 00:07:17,170
The composition is typically a has a relationship, a car has a wheel.
109
00:07:17,560 --> 00:07:20,260
So you might have a car class, which has a wheel class.
110
00:07:20,260 --> 00:07:26,710
And typically, we prefer to use composition in programming because it allows us for a lot more flexibility.
111
00:07:26,710 --> 00:07:31,240
Again, will cover this a lot more in a couple of lectures time and find late tenth commandment.
112
00:07:31,240 --> 00:07:33,310
Thou shalt follow the law of D�MONTRER.
113
00:07:33,310 --> 00:07:38,650
I find this the kind of the least important of the commandments, but it is a useful guideline and as
114
00:07:38,650 --> 00:07:43,240
we'll see later, I will indeed be calling it the guideline of D'Amato because I think it is a rule
115
00:07:43,240 --> 00:07:48,760
that you will be breaking reasonably often, which is why I put it as the last commandment.
116
00:07:49,030 --> 00:07:52,000
OK, so these are my ten coding commandments.
117
00:07:52,000 --> 00:07:55,030
I'd be interested to know what your best practices are.
118
00:07:55,090 --> 00:07:57,190
Please go and share them in the comments.
119
00:07:57,190 --> 00:08:03,790
And we're going to be using these best programming principles to understand and evaluate the design
120
00:08:03,790 --> 00:08:06,640
patterns that we will see later in the course.
121
00:08:06,910 --> 00:08:12,250
But in the next couple of lectures, we're going to dive in to a few of these to try and unpack why
122
00:08:12,280 --> 00:08:13,260
they are important.
12726
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.