ten years ago a friend of mine called
Emraan wrote about hiring programs he
said that he was seeing people being
interviewed with good computer science
degrees who while they knew all the
theory had no idea about how to actually
code in the real world
and he proposed a simple test now hiring
programmers is a tricky job I don't
think that asking complicated lateral
thinking questions is a good way to hire
people and neither is picking folks who
you think will just fit in but a simple
test to make sure that someone can
actually code yeah that makes sense
so here is the test that Imran proposed
code something that'll play the
children's game of fizzbuzz fizzbuzz is
really simple traditionally it is played
between two people and you count up in
turn one two three four except every
times a multiple of three you safe is
instead of the number and every times a
multiple of five you say buzz so one two
fizz four buzz is seven eight piece
puzzle and so on and so on and when it's
a multiple of both at fifteen you say
fizzbuzz right so anyone who is going in
for a job in computer program should be
able to pass this test write a program
that outputs fizzbuzz for the numbers
one to a hundred and it's a clever test
for a number of reasons first there are
many possible approaches I'll run
through a couple of them here but you
folks will be able to come up with loads
more if you want to pause the video now
and have a go at it the approach that
people choose and how they work through
the problems will also give you a good
insight into the style of programming
they are do they quickly just barge
things together to make it work and fix
immediate problems but like I do or do
they start out by planning for the
future and trying to figure out what
might change later as well as that
fizzbuzz includes something that's a
little tricky to express well while
programming if it's a multiple of three
you do something if it's a multiple of
five you do something else if it's a
multiple of both you do a third thing
and if it's neither you do a fourth
thing that's not instantly translatable
into code so I'm going to show you how I
would do it the quick budget together
approach and then we'll see how we can
start to push it a little bit further
I'm writing in JavaScript it's not the
best language in the world but it is one
of the easy
to get started with first and most
obvious step create a loop we're gonna
want to run through the numbers one to a
hundred
so we create a for loop a for loop works
like this first run that bit of code it
creates a variable I and it starts out
with a number one then every time it
reaches the bottom of the loop where the
bracket is it goes back to the top and
it runs that bit of code increasing I by
one and then checks against that bit of
code if I is less than or equal to a
hundred it runs this code again if not
it exits the loop so whatever's inside
here we are doing it a hundred times and
in each loop I is going to be increased
by what programmers use lighter I four
variables like this because actually I
have no idea it's just tradition uh-huh
okay next out we need something to
output the results in JavaScript the
sort that's in a plain text window you
use console.log for this if you putting
it in a webpage you use something else
so console dot log brackets I output
whatever is in the I variable test it
and sure enough you get one through a
hundred sometimes though we want it to
output fizz or Buzz instead so let's put
that to be an if statement if if what if
I is a multiple of three we need to
write fizz so code that percentage sign
is the modulo operator it returns the
remainder after you divide something so
here it's saying divide I by three and
return the remainder if the remainder is
zero then we know I is a multiple of
three and we run that code and if the
remainder is anything else then I is not
a multiple of three and we skip it so
now again to five
now what about fizzbuzz if it's a
multiple both three and five well that's
already been done for us right if it's a
multiple of both then both those tests
gonna return true when we get fizz and
Buzz so the last bit if neither of those
are true just output the number in this
case we want that okay it doesn't look
simple but I promise it is that means
not equal to and that means and so this
whole section will only return true if
both the bits in brackets are true cool
let's run it and that's well it's okay I
guess it does the job except let's go on
at fifteen
that doesn't say fizzbuzz that's his
fizz and then Buzz to set
what lines two separate answers that is
not technically correct because it needs
to say fizzbuzz there one line so we can
add a few more tests let's add a
separate one for fizz buzz they're using
what we've learned already if both of
those tests return true clean fizz bus
and none let's add some more tests in
there so they don't fire twice this is
getting quite ugly repeating yourself
like this is a sign of dodgy code it's
clumsy it's difficult to read that's
difficult to maintain so let's change it
around a bit
let's test for both being true first now
we can use an else statement if it's not
a multiple of both is it just a multiple
of three and if it's not is it just a
multiple of five and if it's still not
then we know to just output the number
but to me this is still dodgy
because if someone says okay now we want
to work on multiples of seven not five
and that's a common sort of follow-up
question for something like this you
have to remember to change all the fives
not a big problem in a section of code
this small but once you start working on
something big this is really bad
practice ideally you shouldn't run the
same test twice but how else can it be
done well here's how I wrote this the
first time I tried it create a variable
called output that's just an empty
string a blank bit of text nothing in it
if I is a multiple of three you add fizz
to that line of text you don't output
anything yet you just store that for the
future if I is a multiple of five you
add buzz to that line of text don't
replace it just add it and then don't
test the number again test that string
of text if it's empty we know none of
those tests are true so you just make
the text the value of I now at the end
of the loop you output well the the
output so now if whoever's running the
interview says make it work on sevens
not fives you only have to change one
thing in an obvious place if they say
make it work on Three's five seven
eleven and thirteen you just copy and
paste that line and it does programming
that without statements would be a
nightmare now I think there are still
better ways to code this particularly he
wants to plan long into the future
after all those lines there you know I'm
technically repeating myself but I'll
leave fixing that as a problem for
someone else this is good enough for me
now there are still some companies who
use this particular test for hiring
particularly because you know well some
interviewers can't think of anything
better but there are so many problems
like it things that a lot of computer
science degrees don't teach because
they're all about the theory me I'm more
in favor of bodging things together and
making them work well enough just don't
leave too much of a mess but whoever
comes along to maintain your code once
you're done with it thank you to
everyone here at the Center for
computing history in Cambridge
philosophy film and thank you to all the
proofreading team who helped make sure I
got my script right
you