Python for Absolute Beginners Transcripts
Chapter: Cleaner code with common data structures
Lecture: Our Rock Paper Scissors code has issues

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


Talk Python's Mastodon Michael Kennedy's Mastodon