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