Python for Absolute Beginners Transcripts
Chapter: Cleaner code with common data structures
Lecture: Our Rock Paper Scissors code has issues
0:00 As a user rock, paper, scissors is a pretty good game.
0:03 It's a decent rendition of the real game
0:05 that kids used to play all the time to settle scores
0:08 decide who gets to sit in the front seat
0:09 or has to sit in the middle in the back.
0:11 But as a piece of software, it has issues.
0:15 Lets take a quick look at just one of those.
0:17 Remember this play round where we're actually
0:19 trying to figure out whose the winner?
0:21 When you get the rule of the first player
0:23 the rule of the second player
0:24 and then there's this huge ugly, complex thing.
0:27 I mean just, oh my goodness.
0:29 This is rough.
0:30 So if you think about having to read this and understand it
0:33 or verify every single case is correct
0:36 you know if I throw a paper, and they throw a scissors
0:38 are you sure that's right?
0:39 Are you sure? It's not easy to verify.
0:42 So this part here, this red part.
0:44 This is the trouble, what are some of the problems?
0:47 Just some. Well it's hard to read.
0:50 It's not easy to just quickly skim through
0:51 and see what's happening and make a lot of sense.
0:53 You've got a whole lot of ideas in your head.
0:55 You've got the outer if else if, else if
0:57 and the inner if else if, else if.
1:00 You got the names, name zero, name one, who's who
1:03 and so on who threw what.
1:04 So it's a little bit tricky it's hard to read.
1:06 It's also hard to maintain, you need to make a change
1:09 in the software like I said verifying that every aspect
1:12 of it is correct, that's a challenge, again hard to verify
1:15 and not extensible.
1:17 In software extensibility means the ease or ability to add
1:21 new features or new capabilities to the software.
1:24 What if we wanted five way rock paper scissors?
1:27 We want rock paper scissors sponge and wolf I don't know.
1:31 We want to have all those things play, think about how much
1:34 more complicated this gets, how much...
1:36 You know all these things sort of compound it's hard
1:38 to verify that adding those new features is working right
1:40 and so on.
1:41 What we're going to do is we're going to rewrite this
1:43 using something called dictionaries and it's going to get
1:45 dramatically simpler and even more interesting to add new
1:48 features to it to add five way, seven way, even 100 way
1:52 rock paper scissors we'll have exactly the same code
1:56 and it won't get any more complicated.
1:58 Now the data structure describing the rules basically will
2:02 be slightly more complicated for each one of course
2:04 but not nearly the degree at which we have here.
2:07 We have in this case we have some form of common notarial
2:10 problems, so if we have five plays instead of three
2:13 we have five outer if's so that's two more, but we also have
2:17 five inner steps and branches in each one.
2:19 So we have 25 bits instead of nine like we have here.
2:23 So as we add more it gets multiplicatively worse
2:26 in this case and what we're going to do with dictionaries
2:28 is it'll be just the same, it'll be wonderful.