Effective PyCharm (2021 edition) Transcripts
Chapter: Refactoring
Lecture: Introducing variables

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Highlight the value of this ability to create variables and other related items.
0:05 I want to focus in on an incredibly simple little function here.
0:08 Get defensive role. So when a creature is attacked either it's you as the wizard
0:14 or something that is being attacked like a dragon or a small animal.
0:19 There's going to be this call to get defensive role.
0:21 If we look what's happening here it says well there's a three times a random number
0:26 between one and 12 times your level.
0:29 So your defensive ability has something to do with your level.
0:32 And what is this? Random?
0:34 Well remember this is like tension in the dragon.
0:36 So that's a 12 sided die right there.
0:38 So you're rolling your dice, you're multiplying that by your level and then there's some
0:41 kind of three here. I think that might be like a modifier or something.
0:47 Yeah so this is not entirely obvious what's happening.
0:50 We'll see if we can actually do a lot to make it much much cleaner.
0:54 Another thing that I like to do a lot is I would like to put a
0:57 breakpoint here and see what the value of that role was.
1:02 There's no real way to do it.
1:03 You can do division like a divide by the level and by the three and then
1:07 get the number back and figure out what it was.
1:09 But that's not the same as just seeing the value in the Debugger.
1:13 Let's create a variable for this.
1:15 It'll give us a good name.
1:16 So we don't need a comment and it will give us a variable that we can
1:19 inspect in the Debugger and it will shorten this thing up.
1:23 So what we can do is we can ctrl+t to re factor again and we
1:27 can introduce a variable. Now it takes a guess as the return value of randint
1:32 And so is it random?
1:34 No this will be dice or die roll like that.
1:39 What do we get when we rolled our 12 sided dice?
1:41 We got this or value. This is so now it's getting to be clear what's
1:46 happening. The defensive role is some number we're not sure about yet.
1:50 Times the dice the dice roll times your level.
1:54 That's pretty straightforward. Now let's say this is a modifier and maybe three is usually
1:59 what we use but in certain circumstances we might want to even change it.
2:03 So we'll create a variable for this as well and I is not ideal.
2:06 So let's call it modifier and maybe even put it like this.
2:11 So now we have a modifier times the die roll times the level that is so
2:16 much clearer. Now remember this is a really small example,
2:20 it was literally that one line of code but it's already getting more clear and if
2:24 you apply these ideas at say the function level you grab three or four lines of
2:28 code and you give it up function that has a name even better but naming these
2:34 variables here already is making a big difference.
2:37 Now I did say we want the modifier to be adjustable by default.
2:41 I wanted to be three but I would like whoever calls this function to potentially some
2:45 other circumstance, passing a different modifier.
2:48 So what we have here. Well obviously that's not gonna work.
2:51 So let's go and actually put this back so that will show us the anti create
2:55 variable. We go over here,
2:56 stay in line, PyCharm says we're going to put this into one location.
3:00 There we go and now we're back.
3:02 We have three times that I stroll times level.
3:05 Now to make this configurable, let's make it a parameter to this function.
3:10 So let's go here and we can say we want to introduce a parameter,
3:14 not a perimeter object. That would be interesting.
3:16 But there's just one thing. So this will do.
3:18 And up here it says I notice it gave it a default value.
3:23 That means whoever whatever part of code is calling,
3:26 get defensive role without passing a modifier because there's a default value,
3:30 it will continue to work as is now if we want to pass a value,
3:34 we can The name i is not ideal.
3:36 So let's give it a modifier and then we have it.
3:39 It's very clear what the mechanism,
3:41 what the function for defensive role is.
3:43 It's the modifier, times the dice roll,
3:45 times your level. Boom. But we did that by introducing variables where we had
3:50 just little computations and expressions, let's run this out by just seeing what happens if
3:54 I hit the do bugger this time around and I go and attack whatever.
4:00 Check this out. The dice roll was four.
4:04 The modifier is three and my level Level right there, 75
4:08 75. Awesome. So it also has enhanced the debugging experience because now I can
4:13 see exactly all the pieces in play.
4:16 We'll get to debugging more later,
4:18 but it's definitely a benefit of this little change we made.