Effective PyCharm Transcripts
Chapter: Refactoring
Lecture: What is refactoring?

Login or purchase this course to watch this video and the rest of the course contents.
0:02 Now that we've written our code and saved it in source control, it's time to make it better, to refactor it, to look at the code that we've written,
0:14 find out where we've maybe done something not so great, we've got a method with like 10 arguments, or we've got duplicated code all over the place,
0:22 or we've got some huge long expression that's like indecipherable even to the author of that expression.
0:28 So, we'll see that PyCharm has a bunch of refactoring tools built-in. I want to really emphasize that refactoring here
0:37 is not the same as some kind of like find text and replace across all the files; it's super important to realize the distinction that
0:47 PyCharm understands your entire project, it loads it into an abstract syntax tree, understands literally how the symbols are being used
0:56 and then uses that for refactoring, so it's way more than just find and replace and it's really powerful. So, let's dig into it.
1:05 Before we get into the tooling though, let's talk about what refactoring is in the first place.
1:10 I feel like people often say, "Oh I'm refactoring my code," and what they really mean is they're changing their code,
1:16 maybe they are kind of cleaning it up while they're adding features that are making it better in some way; but changing code is not refactoring code.
1:24 Refactoring is a very precise thing. So if we look over at Wikipedia, they say, "Refactoring is the process of restructuring existing computer code—
1:34 changing the factoring— without changing its external behavior. Typically, refactoring applies a series of standardized, basic, micro refactorings,
1:44 extract method, rename variable, things like this, each of which usually is a tiny change in the source code
1:50 that either preserves the behavior of the software or at a minimum, does not modify its conformance to functional requirements."
1:57 So, refactoring is changing the internal structure of your code while making it behave more or less the same.
2:04 So, that's great, but how do we know when to refactor, we can inline variables and then we can take the results that we can extract the variable;
2:13 often you see these two things, there's a refactoring and then there's sort of anti refactroing,
2:18 or a refactoring in opposite direction, a negation or something. How do we know when to apply these refactorings,
2:24 because we can just twiddle with our code all day long and get nowhere. So let me introduce you to the idea of code smells,
2:30 so a code smell is a problem with code that is not technically not working, right you can look at some method and you go ugh,
2:40 you kind of turn up your nose at it and you go, "That's not so great", but it's working, it's working code.
2:46 There are different things that might make you turn up your nose at particular code so probably the best way to understand these
2:53 is through some examples. Duplicate code— if you have identical or very, very similar code
3:00 in more than one location, let's say three or more locations, there's a really good chance that that code should be made into a method
3:07 that then you call from those places, right. So if you have duplicate code, this is often a copy and paste type issue, but not always,
3:16 and that can really be a sign that you need an extract method refactoring. If you have a very large class, it's doing too much, it's 500 lines long,
3:25 maybe you should break it into smaller classes and sort of assemble it through composition, and to this larger class that is not so large now,
3:33 kind of anti version of the large class is the freeloader or a lazy class it doesn't really do much, so like it has one method and one variable,
3:40 why the heck is it a class, it could just be a function. Too many parameters— if I've got a function with 15 parameters,
3:46 it's super hard to know what that 11th one is doing right, it could do things like use named parameters
3:53 to make a little bit better, but still, not great. There's this whole list of these ideas of code smells, so when you're thinking about refactoring,
4:01 take a minute and study this idea of code smells, it will really help you understand what the symptoms are
4:07 and often what the appropriate refactoring to apply to it is.


Talk Python's Mastodon Michael Kennedy's Mastodon