Effective PyCharm Transcripts
Chapter: Refactoring
Lecture: Creating constants

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Let's look through our little application here, it's quite simple, we have our main, it prints out the header it prints out the game loop,
0:08 but the header part, it's kind of hard to see, it's this cool dragon thing that I added, just some ascii art.
0:13 but maybe we could make this a little more maintainable because this is kind of a problem, like I'd like to put this aside
0:21 and just say okay, give me the text that is a little dragon picture, all right. So what we can do is we can come over here and highlight this
0:31 and we can say I'd like to extract, it could be a variable, I guess it could be a parameter or a field, but let's say constant.
0:40 Now, in Python, constants are really conventions, there's no language mechanism to make a variable technically constant,
0:48 so the convention is to make the name of the variable all upper case, so when we say extract constant, we get something like this,
0:58 and let's scroll up if we can without messing this up and see what happens. So we put it up at the top, there's this thing called S,
1:05 and look, here's our string, so we'll go back down here and we'll call this DRAGON_TEXT and of course, up here it's naming it DRAGON_TEXT.
1:12 So now if we look at our print header, okay we print out a new line, dashes, DRAGON_TEXT, dashes, we could even make that a constant,
1:22 so if we change it, it changes only one, truly it's just using these two places like two lines 30 and 32 so let's not worry about it.
1:31 But it's really cool how we can extract these constants, we could do the same for here, we could make a constant
1:36 and if we replace, notice it will find all usages of it maybe we want to tie this to the dragon or wizard,
1:46 maybe we don't, because it's not related, this time it is not, so we'll say the level where did it get that from?
1:53 Let's call this starting hero level, make it nice and explicit, where did it get the level from?
2:01 It's got it up here, it got it right from there, that's pretty awesome, right? So I just go in some random number it's like
2:10 hey this number here represents the parameter being passed to the thing called the level, so let's guess that, that's pretty sweet.
2:17 So we can use this extract constant thing and these typically go at the top of the file
2:22 so right here we can configure a few things at the top and then play our game.


Talk Python's Mastodon Michael Kennedy's Mastodon