Effective PyCharm Transcripts
Chapter: Refactoring
Lecture: Introducing variables

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Let's turn our attention to this get defensive roll method right here.
0:06 So it's a super simple method, but it turns out that it's not entirely easy to debug and it's not also clear what this 3 means,
0:14 and what is this— okay, if you have played enough Dungeon and Dragon that's a 12-sided dice right there, but what does it mean, right?
0:22 It doesn't have to be entirely obvious, the only thing obvious is this level, so somehow your defensive roll is based on like
0:28 a number times a random number times your level. So let's make this more explicit.
0:32 First of all, something I like to do a lot if I am doing debugging is I would like to put a break point here
0:39 and stop and see what value came back for the random int, I can't do that here, you can sort of determine it in reverse
0:46 by doing some division, but that's not the same thing. So we can easily fix that by highlighting this, CRTL T for refactoring and say create a variable
0:54 and we're just going to call it something stupid based on there, so let's call this roll, so that's what you roll,
1:00 so now it's getting more obvious what's happening, the level times the roll of your 12 sided dice times 3— what the heck is that?
1:07 We could do the same thing, we could come over here and say, I'm going to create a variable, this will be modifier, and that makes it more legible,
1:17 maybe we want this modifier to be configurable as well from the outside, maybe under different circumstances a different modifier is used.
1:25 Maybe we did this refactoring and it was kind of crummy so we can say, oh that wasn't great, we're actually making it less clear
1:31 if we just had three times it would be more obvious. So we could do a refactoring in reverse and say
1:37 inline and it says, we're going to inline this modifier back, this sort of the reverse type thing.
1:44 Now down here again, let's suppose we're going to make this configurable, so I can say make this a parameter, and then it goes up there as i, equals 3
1:55 so the default value in the signature doesn't really change, all the function, all the people calling to get defensive roll,
2:02 will just use the default value, but it could be overwritten, so let's call this modifier.
2:10 So now, this is pretty understandable, your defensive roll is the modifier, which by default is 3, times the roll, times the level,
2:17 and I did that without typing a single character of code, I just did some CTR T action, technically, you could use the mouse,
2:25 I named this, maybe that sort of counts, but you just saw I could totally use the tools. Now, this function is one line long, it's totally simple,
2:34 but, imagine on a much larger, much more complicated situation, this would be really valuable.


Talk Python's Mastodon Michael Kennedy's Mastodon