Effective PyCharm Transcripts
Chapter: Refactoring
Lecture: Sourcery plugin

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We've seen that PyCharm has many many refactoring is and it's really good, the ones that it does great.
0:08 There's actually a couple of higher order ones that could be useful to us. There are some extensions and plugins for PyCharm.
0:16 That might might be something you want to consider. So for example, here's Sourcery 'sourcery.ai'
0:22 This is a plug in that has a free version and a paid version that you can add to PyCharm that will do more refactoring help in much the same
0:30 way that you would expect. Right. These little code intentions with 'alt+enter' it's like that but it has sourcery refactoring suggestions
0:36 as well. Let's take a look in our project. There's not too many errors and warnings going on. If you go through here, you don't see anything.
0:45 But let's go over to this dragon. There's something interesting here when we were in this section with PyCharm,
0:50 it did tell us that this was not needed, but it didn't have this yellow box here. Technically the reason this is not needed is Python says,
1:02 well you're gonna go this path and created or this path and created and then use it here. To me. I really like to have it declared for sure.
1:10 What if we change this to an if else all of a sudden this becomes a potential run time error about that variable not being defined.
1:17 So I like to do it this way, we had this warning, we can address that in a second but what we're going to see will actually take this away anyway,
1:25 We're riding this out in a long hand four line type of thing here. Sourcery says, you know what?
1:32 We could replace us with. Just an if expression instead of a statement, we could just replace it like this. 'fire_modifier = 5' If breathes fire.
1:42 Self published fire else one and now we don't need that. There's no conditional really. Fire modifiers always going to be defined.
1:50 There's a bunch of things like this that are pretty neat. For example, let's look at another one that we can contrive here.
1:58 Now let's suppose I was going to go over here and create something like a list of all the levels of the creatures. So we could say levels equals this.
2:07 Now again, this is contrived. So I'm not saying it really makes sense in the context of our game, but just to show off some examples,
2:13 we could say 'for_c in creatures' ' levels.append C.level'. All right, so that's all pretty good.
2:22 But notice now Sourcery not PyCharm is suggesting that we can do better. All right, let me make a comment here so we can see it permanently if
2:33 we go over here and says you know what? That could actually be a list comprehension, convert the loop into a list comprehension boom,
2:40 there we go. So levels levels are. Now just see that level four, seeing creatures. So these are the higher order things that sorcery does that.
2:48 PyCharm doesn't kind of, the more Pythonic type of things, let me show you one more conditional that I think is going to be pretty cool
2:57 So over here we have our statement like this, what if we wanted to say is strong, it's going to be base role if base role is not none, else,
3:16 not strong, something like that. Okay, actually we'll just do the test like if based role, so notice now we get this re factoring suggestion,
3:27 well then we got to print out now this says we can simplify the expression using or so here we had an if condition if it's five,
3:40 if this case else one, but check this out, we can just say is strong base roll or not strong, so we have not just converting,
3:48 you know like a conditional over to an expression but even these non obvious simplifications like it's either base role or is this other one?
3:57 You don't need this whole if expression so I personally like sorcery, I do use it some of the time but it's not a super strong,
4:06 you must go and use it endorsement, this is more awareness, like hey look there's these other tools and if you really
4:12 get into re factoring you can check them out, it will take you further.


Talk Python's Mastodon Michael Kennedy's Mastodon