WEBVTT
Kind: captions
Language: en

1
00:00:00.140 --> 00:00:04.210
Ten years ago, a friend of mine called Imran
wrote about hiring programmers.

2
00:00:04.210 --> 00:00:06.009
He said that he was seeing people being interviewed

3
00:00:06.009 --> 00:00:08.000
with good computer science degrees

4
00:00:08.000 --> 00:00:10.210
who, while they knew all the theory,

5
00:00:10.210 --> 00:00:14.090
had no idea about how to actually code in
the real world.

6
00:00:14.090 --> 00:00:16.000
And he proposed a simple test.

7
00:00:16.000 --> 00:00:18.500
Now, hiring programmers is a tricky job.

8
00:00:18.500 --> 00:00:21.939
I don't think that asking complicated
lateral thinking questions

9
00:00:21.939 --> 00:00:23.339
is a good way to hire people,

10
00:00:23.339 --> 00:00:26.770
and neither is picking folks who you think
will "just fit in".

11
00:00:26.770 --> 00:00:30.210
But a simple test to make sure that someone
can actually code:

12
00:00:30.210 --> 00:00:32.489
yeah, that makes sense.

13
00:00:32.489 --> 00:00:34.340
So here is the test that Imran proposed:

14
00:00:34.340 --> 00:00:37.840
code something that'll play the children's
game of FizzBuzz.

15
00:00:41.220 --> 00:00:42.360
FizzBuzz is really simple.

16
00:00:42.360 --> 00:00:44.219
Traditionally, it is played between two people,

17
00:00:44.219 --> 00:00:47.190
and you count up in turn, 1, 2, 3, 4,

18
00:00:47.190 --> 00:00:48.710
except every time there's a multiple of 3,

19
00:00:48.710 --> 00:00:50.109
you say "Fizz" instead of the number.

20
00:00:50.109 --> 00:00:52.480
And every time there's a multiple of 5, you
say "Buzz".

21
00:00:52.480 --> 00:00:57.740
So: 1, 2, fizz, 4, buzz, fizz,

22
00:00:57.740 --> 00:01:01.950
7, 8, fizz, buzz, and so on and so on!

23
00:01:01.950 --> 00:01:05.890
And when it's a multiple of both,
at 15, you say "FizzBuzz".

24
00:01:05.890 --> 00:01:10.010
Right, so anyone who's going in for a job
in computer programming

25
00:01:10.010 --> 00:01:11.900
should be able to pass this test:

26
00:01:11.900 --> 00:01:16.299
write a program that outputs FizzBuzz for
the numbers 1 to 100.

27
00:01:16.299 --> 00:01:18.329
And it's a clever test for a number of reasons.

28
00:01:18.329 --> 00:01:19.680
First, there are many possible approaches.

29
00:01:19.680 --> 00:01:21.590
I'll run through a couple of them here,

30
00:01:21.590 --> 00:01:23.659
but you folks will be able to come up with
loads more.

31
00:01:23.659 --> 00:01:26.560
If you want, pause the video now
and have a go at it.

32
00:01:26.560 --> 00:01:27.680
The approach that people choose,

33
00:01:27.680 --> 00:01:29.610
and how they work through the problems,

34
00:01:29.610 --> 00:01:33.250
will also give you a good insight into the
style of programmer they are.

35
00:01:33.250 --> 00:01:35.450
Do they quickly just bodge things together

36
00:01:35.450 --> 00:01:38.560
to make it work and fix immediate problems,

37
00:01:38.560 --> 00:01:39.560
like I do,

38
00:01:39.560 --> 00:01:43.469
or do they start out planning for the future
and try to figure out what might change later?

39
00:01:43.469 --> 00:01:46.939
As well as that, FizzBuzz includes something
that's a little tricky

40
00:01:46.939 --> 00:01:49.310
to express well while programming:

41
00:01:49.310 --> 00:01:51.360
If it's a multiple of 3, you do something.

42
00:01:51.360 --> 00:01:53.729
If it's a multiple of 5,
you do something else.

43
00:01:53.729 --> 00:01:53.729
If it's a multiple of both,

44
00:01:53.729 --> 00:01:56.049
If it's a multiple of both,
you do a third thing.

45
00:01:56.049 --> 00:01:58.830
And if it's neither,
you do a fourth thing.

46
00:01:58.830 --> 00:02:01.750
That’s not instantly translatable into code.

47
00:02:01.750 --> 00:02:04.670
So I'm going to show you how I would do it,

48
00:02:04.670 --> 00:02:06.240
the quick bodge-it-together approach,

49
00:02:06.240 --> 00:02:09.709
and then we'll see how we can start
to push it a little bit further.

50
00:02:09.709 --> 00:02:12.410
I'm writing in JavaScript: it's not the best
language in the world,

51
00:02:12.410 --> 00:02:15.370
but it is one of the easiest
to get started with.

52
00:02:15.370 --> 00:02:17.650
First and most obvious step: create a loop.

53
00:02:17.650 --> 00:02:21.250
We're going to want to run through the numbers
1 to 100,

54
00:02:21.250 --> 00:02:23.239
so we create a for loop.

55
00:02:23.240 --> 00:02:26.560
A for loop works like this:
first, run that bit of code.

56
00:02:26.560 --> 00:02:28.080
It creates a variable, i,

57
00:02:28.080 --> 00:02:30.280
and it starts it out with the number 1.

58
00:02:30.290 --> 00:02:32.580
Then every time it reaches the bottom
of the loop, where the bracket is,

59
00:02:32.580 --> 00:02:35.489
it goes back to the top
and runs that bit of code,

60
00:02:35.489 --> 00:02:36.690
increasing i by 1,

61
00:02:36.690 --> 00:02:39.200
and then checks it against that bit of code.

62
00:02:39.200 --> 00:02:43.190
If i is less than or equal to 100, it runs
this code again.

63
00:02:43.190 --> 00:02:45.330
If not, it exits the loop.

64
00:02:45.330 --> 00:02:46.500
So whatever's inside here?

65
00:02:46.500 --> 00:02:47.790
We are doing it 100 times,

66
00:02:47.790 --> 00:02:51.709
and in each loop i
is going to be increased by 1.

67
00:02:51.709 --> 00:02:54.310
Programmers use the letter i for variables
like this because...

68
00:02:54.310 --> 00:02:57.100
er, actually, I have no idea, it's just tradition.

69
00:02:57.100 --> 00:02:58.100
Okay.

70
00:02:58.100 --> 00:02:59.100
Next up.

71
00:02:59.100 --> 00:03:00.870
We need something to output the results.

72
00:03:00.870 --> 00:03:03.340
In JavaScript, the sort
that's in a plain text window,

73
00:03:03.340 --> 00:03:04.810
you use console.log() for this.

74
00:03:04.810 --> 00:03:08.290
If you were putting it in a web page you'd
use something else.

75
00:03:08.290 --> 00:03:10.390
So: console.log(i);

76
00:03:10.390 --> 00:03:12.379
Output whatever is in the i variable.

77
00:03:12.379 --> 00:03:15.200
Test it, and sure enough,
you get 1 through 100.

78
00:03:15.200 --> 00:03:18.700
Sometimes, though, we want it to output
'Fizz' or 'Buzz' instead.

79
00:03:18.700 --> 00:03:20.380
So let's put that in an if statement.

80
00:03:20.380 --> 00:03:21.380
If…

81
00:03:22.180 --> 00:03:23.520
If what?

82
00:03:23.530 --> 00:03:26.560
If i is a multiple of 3, we need to write
'Fizz'.

83
00:03:26.560 --> 00:03:27.600
So: code.

84
00:03:27.600 --> 00:03:30.230
That % sign is the modulo operator.

85
00:03:30.230 --> 00:03:33.340
It returns the remainder
after you divide something.

86
00:03:33.340 --> 00:03:37.170
So here, it's saying, divide i by 3
and return the remainder.

87
00:03:37.170 --> 00:03:39.840
If the remainder is zero, then we know
i is a multiple of 3,

88
00:03:39.840 --> 00:03:40.909
and we run that code.

89
00:03:40.909 --> 00:03:42.760
And if the remainder's anything else,

90
00:03:42.760 --> 00:03:46.040
then i is not a multiple of 3,
and we skip it.

91
00:03:46.040 --> 00:03:48.580
So now again for 5.

92
00:03:48.580 --> 00:03:51.470
Now what about FizzBuzz, if it's a multiple
of both 3 and 5?

93
00:03:51.470 --> 00:03:53.290
Well, that's already been done for us, right?

94
00:03:54.440 --> 00:03:56.340
then both these tests are
going to return true,

95
00:03:56.340 --> 00:03:59.019
and we get 'Fizz' and 'Buzz'.

96
00:03:59.019 --> 00:04:01.140
So the last bit: if neither of thode are true,

97
00:04:01.140 --> 00:04:02.700
just output the number.

98
00:04:02.700 --> 00:04:04.660
In this case, we want that:

99
00:04:04.660 --> 00:04:08.180
Okay, it doesn't look simple,
but I promise it is.

100
00:04:08.180 --> 00:04:10.020
That means "not equal to".

101
00:04:10.020 --> 00:04:12.379
And that means "and".

102
00:04:12.379 --> 00:04:14.420
So this whole section will only return true

103
00:04:14.420 --> 00:04:17.540
if both the bits in brackets are true.

104
00:04:17.540 --> 00:04:18.540
Cool.

105
00:04:18.540 --> 00:04:19.540
Let's run it.

106
00:04:19.540 --> 00:04:20.540
And that's... well, it's OK, I guess.

107
00:04:20.540 --> 00:04:21.540
It does the job.

108
00:04:21.540 --> 00:04:23.050
Except... what's going on at 15?

109
00:04:23.050 --> 00:04:24.050
That doesn't say FizzBuzz.

110
00:04:24.050 --> 00:04:26.730
That says "Fizz" and then "Buzz".

111
00:04:26.730 --> 00:04:27.730
Two separate lines.

112
00:04:27.730 --> 00:04:28.730
Two separate answers.

113
00:04:28.730 --> 00:04:29.730
That is not technically correct,

114
00:04:29.730 --> 00:04:31.630
'cos it needs to say 'FizzBuzz' there.

115
00:04:31.630 --> 00:04:32.780
One line.

116
00:04:32.780 --> 00:04:34.500
So we can add a few more tests.

117
00:04:34.500 --> 00:04:36.630
Let's add a separate one for FizzBuzz there,

118
00:04:36.630 --> 00:04:38.000
using what we've learned already.

119
00:04:38.000 --> 00:04:40.420
If both of those tests return true, print
"FizzBuzz".

120
00:04:40.420 --> 00:04:45.140
And then let's add some more tests in there,
so they don't fire twice.

121
00:04:45.140 --> 00:04:47.280
This is getting quite ugly.

122
00:04:47.280 --> 00:04:49.450
Repeating yourself like this is a sign of
dodgy code.

123
00:04:49.450 --> 00:04:52.720
It's clumsy, it's difficult to read
and difficult to maintain.

124
00:04:52.720 --> 00:04:54.730
So let’s change it around a bit.

125
00:04:54.730 --> 00:04:57.670
Let’s test for both being true first.

126
00:04:57.670 --> 00:04:59.030
Now we can use an ‘else’ statement:

127
00:04:59.030 --> 00:05:00.970
if it’s not a multiple of both,

128
00:05:00.970 --> 00:05:02.950
is it just a multiple of 3?

129
00:05:02.950 --> 00:05:04.530
And if it’s not, if is just a multiple of
5?

130
00:05:04.530 --> 00:05:05.820
And if it’s still not,

131
00:05:05.820 --> 00:05:08.600
then we know to just output the number.

132
00:05:08.600 --> 00:05:10.780
But to me, this is still dodgy.

133
00:05:10.780 --> 00:05:12.030
Because if someone says,

134
00:05:12.030 --> 00:05:15.300
'okay, now we want it to work on
multiples of 7, not 5',

135
00:05:15.300 --> 00:05:18.440
and that's a common sort of follow-up question
for something like this,

136
00:05:18.440 --> 00:05:21.520
you have to remember to change all the 5s.

137
00:05:21.520 --> 00:05:23.540
Not a big problem in a
section of code this small,

138
00:05:23.540 --> 00:05:26.160
but once you start working on something big,

139
00:05:26.160 --> 00:05:27.750
this is really bad practice.

140
00:05:27.750 --> 00:05:31.630
Ideally, you shouldn't run the same test twice.

141
00:05:31.630 --> 00:05:33.520
But how else can it be done?

142
00:05:33.520 --> 00:05:36.360
Well, here's how I wrote this
the first time I tried it.

143
00:05:36.360 --> 00:05:39.340
Create a variable called "output" that's just
an empty string.

144
00:05:39.340 --> 00:05:41.940
A blank bit of text, nothing in it.

145
00:05:41.940 --> 00:05:45.320
If i is a multiple of 3, you add "Fizz" to
that line of text.

146
00:05:45.320 --> 00:05:48.190
You don't output anything yet,
you just store that for the future.

147
00:05:48.190 --> 00:05:51.240
If i is a multiple of 5, you add "Buzz" to
that line of text.

148
00:05:51.240 --> 00:05:53.450
Don't replace it: just add it.

149
00:05:53.450 --> 00:05:56.250
And then, don't test the number again --
test that string of text.

150
00:05:56.250 --> 00:05:57.640
If it's empty,

151
00:05:57.640 --> 00:05:59.440
we know none of these tests were true,

152
00:05:59.440 --> 00:06:02.080
so you just make the text the value of i.

153
00:06:02.080 --> 00:06:05.720
Now at the end of the loop, you output…
well, the output.

154
00:06:05.720 --> 00:06:07.800
So now, if whoever's running the interview
says

155
00:06:07.800 --> 00:06:09.910
'make it work on 7s, not 5s',

156
00:06:09.910 --> 00:06:12.920
you only have to change one thing
in an obvious place.

157
00:06:12.920 --> 00:06:16.550
If they say 'make it work on
3s, 5s, 7s, 11s and 13s',

158
00:06:16.550 --> 00:06:19.540
you just copy and paste that line
and it just does.

159
00:06:19.540 --> 00:06:23.020
Programming that with else statements
would be a nightmare.

160
00:06:23.020 --> 00:06:25.340
Now I think there are still
better ways to code this,

161
00:06:25.340 --> 00:06:27.620
particularly if you wanted to plan
long into the future.

162
00:06:27.620 --> 00:06:29.880
After all, those lines there...

163
00:06:29.880 --> 00:06:32.580
I'm technically repeating myself.

164
00:06:32.580 --> 00:06:35.300
But I'll leave fixing that as a problem for
someone else.

165
00:06:35.300 --> 00:06:36.490
This is good enough for me.

166
00:06:36.490 --> 00:06:40.830
Now there are still some companies who use
this particular test for hiring,

167
00:06:40.830 --> 00:06:45.310
particularly because, y'know, some interviewers
can’t think of anything better.

168
00:06:45.310 --> 00:06:47.060
But there are also many problems like it:

169
00:06:47.060 --> 00:06:48.940
things that a lot of computer science degrees
don't teach

170
00:06:48.940 --> 00:06:51.520
because they're all about the theory.

171
00:06:51.530 --> 00:06:52.530
Me?

172
00:06:52.530 --> 00:06:55.980
I'm more in favour of bodging things together
and making them work well enough.

173
00:06:55.980 --> 00:06:58.020
Just don't leave too much of a mess

174
00:06:58.020 --> 00:07:02.060
for whoever comes along to maintain your code
once you're done with it.

175
00:07:02.480 --> 00:07:05.480
Thank you to everyone here at the Centre for
Computing History in Cambridge

176
00:07:05.480 --> 00:07:06.800
for letting me film

177
00:07:06.800 --> 00:07:10.070
and thank you to all the proofreading team
who helped make sure I got my script right.

