Latest / Talk Python To Me / #150: Technical Lessons Learned from Pythonic Refactoring
Transcript
- 0:00Does your code smell? Does it have a weird fragrance? It turns out code smells are a real
- 0:04thing and an amazing conceptualization of suboptimal design. This week you'll meet Yanni
- 0:09Chung who has some practical and real-world advice on using refactoring and Python to improve your
- 0:16code and wash away those bad smells. This is Talk Python To Me, episode 150, recorded January 31st, 2018.
- 0:23Welcome to Talk Python To Me, a weekly podcast on Python, the language, the libraries, the ecosystem,
- 0:42and the personalities. This is your host, Michael Kennedy. Follow me on Twitter where I'm @mkennedy.
- 0:47Keep up with the show and listen to past episodes at talkpython.fm and follow the show on Twitter
- 0:53via at talkpython. This episode is brought to you by ParkMyCloud and Rollbar. Please check out what
- 0:59they're offering during their segments. It really helps support the show. Talk Python To Me is partially
- 1:03supported by our training courses. Python's async and parallel programming support is highly underrated.
- 1:09Have you shied away from the amazing new async and await keywords because you've heard it's way too
- 1:14complicated or that it's just not worth the effort? With the right workloads, a hundred times speed up
- 1:20is totally possible with minor changes to your code. But you do need to understand the internals and
- 1:25that's why our course, async techniques and examples in Python show you how to write async code
- 1:31successfully as well as how it works. Get started with async and await today with our course at
- 1:37talkpython.fm/async.
- 1:39Yeni, welcome to Talk Python.
- 1:41Hey, yeah, very excited to be on here.
- 1:43Yeah, it's great to have you. I saw your talk, you know, virtually via YouTube, PyCon.de, and that was
- 1:49really, really interesting. So I wanted to make, give you an opportunity to come here and share
- 1:53your technical lessons learned from a refactoring with everybody.
- 1:58Yeah, thanks. It was also a great learning experience for me with that many Pythonistas
- 2:02there. And I also learned a lot from people's opinions. We had a great chat.
- 2:07You know, one of the things I think is a little bit counterintuitive or ironic is
- 2:10probably when you see those presentations given, the person that learned the most from that
- 2:15presentation is the person presenting it, right?
- 2:18Yeah.
- 2:19Because you had to do all the research and the thinking and it's not just what came out,
- 2:23but it's like the whole experience, right?
- 2:25Yeah, pretty cool.
- 2:27So before we get though into the details of all the refactoring stuff that you talked about,
- 2:31which is really interesting, let's talk about your story. How'd you get into programming in Python?
- 2:35Yeah, that's a long story. So when I was in high school, I don't actually know programming as a
- 2:41thing. So I was just really into math and sciences. So yeah, like I like the problem solving part of it.
- 2:47I even liked physics as a subject. And yeah, like from there, I kind of know that I like the
- 2:53application side of things because I feel like, you know, by doing those high school problems,
- 2:57I can solve real world problems. Not really, but that's how I got into it. And in university,
- 3:03very naturally, I just applied for engineering. And I didn't really think about pursuing software
- 3:10engineering at all because I didn't know it's a profession, but there's one requirement course
- 3:15that I need to take for engineering. And that was CS21, Intro to Programming. So after I took it-
- 3:21What language was that?
- 3:21That was in Python. So yeah, after that, taking that class, I think Python set a very high bar for me.
- 3:28So I didn't really get too much into other languages anymore because of how much I liked Python. It's
- 3:35such a neat and beautiful language, I would say. So yeah, pretty much after that class, I decided to
- 3:41switch my major into computing science instead, and I dropped engineering.
- 3:45That's really wild. So it really connected with you and you're like, I found something I like,
- 3:51better. I think that's an important part of college actually, is I'm always kind of blown
- 3:57away that people just go into college knowing what they want and they just do that. Because to me,
- 4:01college was you go and you try a bunch of things and a few things connect and you sort of follow that
- 4:07path. Yeah. So what kind of engineering were you doing before you switched into programming?
- 4:11So it's general engineering. We didn't really pick the discipline yet. By the time I had to do that, I already switched out. So I didn't get to that part of it.
- 4:21Yeah. I think it's a pretty smart thing to use Python as the first language to teach to people.
- 4:27Because I really feel like in Python, you spend less time thinking about the syntax.
- 4:32You get to think much more about the logic of the code and computer science itself.
- 4:37So compared to other languages, which we learn later, like C++, C, which are great because you get more of the happenings under the hood.
- 4:47I think Python really took all these distractions out from the get go.
- 4:52Yeah, I agree. I think it's a really good learning language. What surprises me about Python is there's a lot of languages that are kind of simple in that regard, like Visual Basic, like six, you know, the early Visual Basics and things like that.
- 5:04They were kind of like that, but they would always they would hit this really hard limit as soon as you try to do very complicated stuff with them.
- 5:11And Python seems to have struck a balance where it's simple in the beginning, but it can grow into larger applications still.
- 5:19Yeah, it's totally an art how to write good Python code.
- 5:22Yeah, that's for sure. So what do you do these days?
- 5:26Recently, I've been leading a project. So I'm a full stack engineer. I don't only code on Python. I also work on JavaScript and React stuff.
- 5:34So recently we've been working on such a project. Sometimes I could be doing interviewing or mentoring some other people in the company or preparing for a Python podcast.
- 5:45Of course, of course. And you work at Yelp, right?
- 5:48Yeah.
- 5:49Yeah. That sounds like a really fun place to be.
- 5:51Yeah, I think so. So I work in Yelp and Hamburg. So not in the San Francisco office.
- 5:56Here we focus more on the business side of things. So selling ads, helping local businesses find customers and things like that, which I find I find it to be very interesting.
- 6:12Yeah, like I really enjoy being here.
- 6:14I'm sure. Is Yelp pretty big in Europe? I only know it through the US. When I lived in Germany, I didn't actually use Yelp very much.
- 6:21There are definitely like some competitors in Europe as well, but it's growing. It's getting its ground.
- 6:27Yeah. And you actually have more offices in Europe, which is kind of cool, right? London and Hamburg.
- 6:32Yeah. Well, the good thing about like working in a European office is you get to fly around in Europe, you know, like you get to go to all these like fun European conferences, which is very nice.
- 6:42Yeah, that's one of the things I really liked about being there is you're so much closer to many amazing places to visit.
- 6:50Like where I live in Portland, it's three hours to Seattle to the north.
- 6:54It's 10 hours to San Francisco to the south, and it's eight hours to Boise to the east.
- 6:59And those are the three closest cities. Like you're in Paris in two and a half hours, or you're in Vienna or wherever. It's really just such a different experience there. So enjoy it. That's cool.
- 7:10Yeah. But like the tech scene, I would say it's like very much concentrated in the US. Like say SF, you have such a huge technical community. But in Europe, it's definitely more spread out. So you need to like fly from one place to another.
- 7:22Maybe like a bigger tech hub would be Berlin. But still, it's really, really widespread.
- 7:27Yeah, that's an interesting observation.
- 7:28Cool. So let's talk about your topic that you covered at your talk, which is refactoring. And I think refactoring, you know, it's interesting that we've had programming.
- 7:41I don't know, like what you might consider programming, maybe since the 60s, right? Do you consider punch cards? I don't know.
- 7:47But we've had programming for quite a while. But refactoring as an idea didn't really come out until, I don't know, was it early 90s, mid 90s, something like that?
- 7:58With the whole Martin Fowler thing? Yeah.
- 8:00Yeah.
- 8:01So it finally got sort of formalized an idea. So maybe just really quickly for everyone listening, because I know people kind of use refactoring and just changing your code to be better.
- 8:11And maybe adding features and whatnot while you're doing it as interchangeable. So maybe just quickly give us like a definition of refactoring.
- 8:19So code refactoring is pretty much the process of restructuring existing computer code.
- 8:24So that's pretty much meaning changing the refactoring without changing its external behavior. And it also improves the non-functional attributes of the software.
- 8:33So from the outside, it should appear as if it didn't change, but you might change the way the algorithm is implemented or something to that effect, right?
- 8:44Yeah. Pretty much changing the design, like improving the design of it, but without changing the functionality of what your software is supposed to do.
- 8:51Book that kind of announced that was Martin Fowler's book. And you talked a little bit about that in the internet talk as well.
- 8:56And I just looked it up. It was published in 1999. I mean, that's like height of dot com boom days, right?
- 9:04It seems pretty far down the line for when that came into existence.
- 9:08Yeah, but this is kind of like, I don't know, the Bible for refactoring. In a lot of ways, I feel it's like a start of discussion.
- 9:14But in a way, a lot of those principles can still be applied to other languages and in other situations.
- 9:21So I would really like to use this book as like an introduction to what refactoring means.
- 9:28But later on in the podcast, I think we'll also touch on Python might not need that many of these refactoring patterns.
- 9:35Right. So one of the things that was interesting, you know, so there was a lot of stuff that came around the same time.
- 9:41So Martin Fowler's refactoring concepts, we had Kent Beck and I think maybe Martin Fowler was also participating in that with sort of extreme programming, paired programming.
- 9:51We had the whole design patterns in the Gang of Four book.
- 9:54There were some really interesting foundational ideas brewing in that time.
- 9:59And certainly the design patterns thinking.
- 10:03And it seems like just from looking at Martin Fowler's work, that it's very much based on languages like Java or Smalltalk or these heavily object oriented sort of object only languages.
- 10:20So I think it's interesting to compare that back to Python because it doesn't always make sense or there's problems you'll find in Python that don't appear in the list of, say, the code smells, which we'll talk about or the refactoring techniques because it's fundamentally a different way to structure code in Python.
- 10:37Yeah, I would totally agree with that.
- 10:39Like a lot of the problems that the refactoring patterns try to solve, I think inherently in Python's language design, it's already solved.
- 10:47So we can definitely explore into that a little bit later.
- 10:50Right.
- 10:51And then at the same, yeah, I think another one, though, so for example, that I think is kind of an issue you might run into is what I don't know, I would call is like a long module or something, right?
- 11:03Where everything is just crammed into one file and it probably should be broken apart to be more easily understood.
- 11:10Right.
- 11:11But that doesn't appear anywhere in the traditional literature because they're talking about things like Java where you just have one class per file.
- 11:19So if the file is long, that means the class is long.
- 11:21Right.
- 11:21But that's not necessarily the case in Python.
- 11:24I feel like there's like refactoring obviously applies to Python, but some of the traditional history of it is maybe an 80% match.
- 11:33Yeah, I would agree.
- 11:34Like a lot of the ideas translate, but like how you achieve that might change because of the language and how to apply that.
- 11:41Yeah, for sure.
- 11:41All right.
- 11:42So let's start by thinking about just why, what are the benefits of refactoring?
- 11:47So first of all, I think it cleans up tech debt.
- 11:51And for those who are not as familiar, tech debt tends to build up when you take shortcuts during development.
- 11:58There might be a better solution, but instead you took like a shorter one so that you can save some developer time.
- 12:04But these tradeoffs, you need to pay pretty much like pay for your debt later.
- 12:08You need to spend more time to fix the implementation, things like that.
- 12:12Or maybe when you have the spec, it's not complete yet or, you know, the products change, the code changes.
- 12:19So the thing you design right now might not be applicable for the future.
- 12:23And in this case, refactoring really helps it get into shape again.
- 12:27But I just think that it's like a good habit to have.
- 12:30Whenever I write a function, like it's very few times that I can get it right the first time.
- 12:35So upon like thinking a little bit more about it or look at it the next day, there has to be, you know, there can be a lot of improvements can be done.
- 12:43You know, maybe you can extract it to the function, name it better, things like that.
- 12:47And also, I just think that it really saves productivity because I've worked on legacy code before.
- 12:53And my experience was just, you know, I come to work, I look at all these code and I, okay, finally, I understood what it means.
- 13:01Okay.
- 13:01The next day I'll start implementing new features.
- 13:04And then I go home, like I had a nice sleep.
- 13:07I came back and then I just forgot what it does.
- 13:09And the code doesn't really help because it's so convoluted.
- 13:13So imagine that, but like times 10, you know, the whole team pretty much does the same thing.
- 13:18So if some people can be responsible for refactoring this, then everybody doesn't have to face the same problem.
- 13:25It's almost like a golden rule of programming in a professional environment is you talked about the Boy Scout rule, which was let's leave the place.
- 13:36In this case, the code in a better state than we found it every time we interact with it.
- 13:41So that's a pretty interesting idea.
- 13:43Yeah.
- 13:43That was one of the first things they tell us at Yelp.
- 13:46You know, it's like a common courtesy.
- 13:47You don't want to hand worse code to your colleagues, you know, than you found it.
- 13:52So that's where the Boy Scout rule comes from.
- 13:55Yeah, that's pretty cool.
- 13:57I think another sort of real world analogy applied to code that would be appropriate there would be the broken window theory.
- 14:05So like for those of you who don't know about that, that's in, I believe it came out of New York City.
- 14:10And there used to be, the city was kind of run down and there's a lot of crime.
- 14:13And they said, what can we do to make these neighborhoods nicer?
- 14:15And there was some psychologist or something that realized that just a few broken windows and a little bit of decay sort of communicated to the people of that neighborhood that, oh, this place, we don't care what it's like.
- 14:27It's kind of broken.
- 14:28And what does it matter if we break it a little bit more?
- 14:31Whereas if you come upon really nicely factored code that's really clean and beautiful to read, you don't want to go in there and mess it up, right?
- 14:38Yeah.
- 14:38The expectation is if I'm going to contribute to this, this is beautiful.
- 14:41I'm not just going to throw junk on it, right?
- 14:44So I think that's an interesting psychology about it as well.
- 14:47Yeah, that's a very good analogy.
- 14:48At the same time, I like what Martin Fowler mentioned in his book.
- 14:52He said he's a lazy developer.
- 14:54So the fact that he's working on this code right now means that it's very likely for him to work on this in the future.
- 15:00So just refactoring right now makes sure he can understand it easier in the future.
- 15:05Yeah, yeah.
- 15:06Yeah, a lazy developer is a good developer.
- 15:09Productive laziness.
- 15:10Yeah, exactly.
- 15:12Yeah, so you said also, in addition to cleaning up technical debt, it'll actually save you productivity now and over time, right?
- 15:20Yeah, I would agree.
- 15:21Because after refactoring, supposedly your code should look simpler, increase the readability.
- 15:27So in this case, the code can also be more usable and easier to maintain.
- 15:32It's good for you.
- 15:33And this is the good thing about Python.
- 15:36Python already reads very nicely, I would say.
- 15:39Like semantics is very easy to read.
- 15:41So if you can write good Python code, your code pretty much self-document.
- 15:46And that's a huge win.
- 15:48Imagine an organization like Yelp, right?
- 15:51We have over 300 microservices.
- 15:55And then we have even like a monolith of code base.
- 15:58There is it's impossible for you to write documentation to cover every line of what you write, right?
- 16:04So in this case, if you can write code that is readable, that is easy to read, then onboarding new people.
- 16:10If they if you have like a new hire and intern in your team, then they can immediately jump on.
- 16:16Like it really, I think it really brings a lot of value to the team.
- 16:21This portion of Talk Python To Me is brought to you by ParkMyCloud.
- 16:26The last time you parked your car, did you leave it running?
- 16:28No?
- 16:29Well, then why are you leaving your cloud resources running?
- 16:32Every year, $13 billion are wasted on cloud instances that no one is using.
- 16:38Don't let any of those be yours.
- 16:40ParkMyCloud automatically identifies and eliminates wasted cloud spend, saving you 65% on AWS, Azure, and Google's cloud.
- 16:49You're up and running quickly with a 10 minute setup and no scripting required.
- 16:53Plus, govern users and easily integrate into your DevOps process.
- 16:57See why ParkMyCloud was chosen by McDonald's, Unilever, Fox, and more.
- 17:02Start a free trial today at parkmycloud.com slash talkpython.
- 17:07So you talked about these 300 microservices and some monolithic code as well.
- 17:15How do you guys think about, say, refactoring from one larger service into microservices?
- 17:23Or maybe the reverse, maybe taking, say, five microservices.
- 17:27These are really not doing enough to be independent things.
- 17:30We're going to smush them back together.
- 17:31Whenever we build new features, definitely, it's always in the back of our mind.
- 17:36Can we make that into a separate service?
- 17:38Can we modularize it a little bit better so that we don't have to contribute code to the giant monolith?
- 17:44But yeah, at the same time, you know, if you have more microservices, there is always overhead.
- 17:49So it's really a decision that is, you need to, like, find a good balance between it, I would say.
- 17:55Yeah, for sure.
- 17:56There's definitely trade-offs there.
- 17:58Another benefit to refactoring, I think, is maybe overlooked.
- 18:03I'm not entirely sure how often, say, managers take this into account in business, like sort of decision makers.
- 18:09But it seems to me, as a developer working at a company, if you always work on code that seems to have no care, no craft, it's just kind of thrown together and not well factored.
- 18:23You might decide either you'll be unhappy or you might just leave.
- 18:26And so maybe you are actually losing your best developers because they don't want to work with the crappy code.
- 18:32And so refactoring it into, you know, for all the reasons you already talked about, cleaning up debt and stuff, it seems like it almost could be like an HR issue.
- 18:39Yeah, for sure.
- 18:41Like, who doesn't like to work on clean code, nicely designed code?
- 18:44Exactly.
- 18:46Like, I've certainly worked on some projects where I'm like, wow, this does not look fun to work on.
- 18:52Like, why do I, what is even going on here, you know?
- 18:55Like, crazy variable names and huge stuff crammed into one function or a store procedure or something, right?
- 19:01Yeah, for sure.
- 19:02Yeah, another benefit, maybe, what do you think?
- 19:04Flexible software?
- 19:05I think there are different thoughts into how you should write software because some people think that, you know, software is kind of like a throwaway thing because products evolve so quickly that, you know, your code probably doesn't live very long.
- 19:19Like, after one year, two years, you have to rewrite it.
- 19:22Things come up and then, you know, you look at code that's supposedly to be very well designed before.
- 19:27But, you know, two years after it's not, you know, code tends to rot.
- 19:31And so, one thing about refactoring is the thing that came out from refactoring should be easy to modify.
- 19:37So, I think refactoring actually complies with this thinking, you know.
- 19:42If it's easy to read, you know exactly what to change.
- 19:45You know exactly what to throw away.
- 19:47And that's very important.
- 19:48Yeah, absolutely.
- 19:49And so, a lot of the refactory stuff also applies to breaking things into smaller, more well-understood pieces, right?
- 19:59Instead of one huge log function, maybe, like, a couple of functions and, like, a class that represents the data or something.
- 20:05So, it actually just makes it easier to evolve and change over time anyway.
- 20:10Yeah.
- 20:11Like, a counter example would be CSS.
- 20:13You know, you can only add lines to it and not really take it out because you don't know, like, you know, once you take out something, it breaks.
- 20:19So, this is exactly the situation we want to avoid.
- 20:23That's a really good way of thinking of CSS.
- 20:25And I definitely noticed that because you're like, well, but what about that one page that nobody realizes there?
- 20:32Or there's that admin section that you don't want to redesign that.
- 20:35So, you know, I find, like, the changes go into, like, the bottom of the CSS file.
- 20:42Or if it gets really out of control, like, you'll create, like, this page overrides those CSS settings and we'll include that file after this one, right?
- 20:50Yeah, I think it's, like, a nightmare.
- 20:52But, like, recently I'm working on a React project, which makes it better because you have CSS modules.
- 20:57But, you know, again, these theories of, hey, like, modularize your code can apply to every language, even CSS.
- 21:04Yeah.
- 21:04Do you use anything like less or SAS or just straight CSS?
- 21:08Yeah, we do use SAS as well.
- 21:11But I think the good thing about CSS module is you can declare, you know, this element is what needs this class.
- 21:17So, in a way, you can know exactly what is used and what is not used.
- 21:22Right.
- 21:22Yeah, yeah.
- 21:23The dead selector stuff is really bad.
- 21:26It's even worse than regular code.
- 21:28Yeah.
- 21:28Yeah.
- 21:29So, that's sort of the benefits of refactoring.
- 21:32When do you think we should refactor?
- 21:34Like, what are some of the best times in sort of the software lifecycle?
- 21:39I think, yeah, that's a very good question.
- 21:41I think it's really an iterative process.
- 21:44So, whenever you add code, you know, remove code, or when you're understanding code, when you're fixing bugs, those are always very good timings for it.
- 21:53And another thing I would also want to point out is during code review.
- 21:56So, if after this podcast, you're more into this refactoring things during code review, then maybe you can comment, hey, actually, you can do this better.
- 22:06And you can raise the code quality for your entire team.
- 22:10And, yeah, I would really want to push for the value of code review.
- 22:14I think part of this might be, like, deciding what's important during code review, right?
- 22:20Like, are you reviewing for performance?
- 22:22Are you reviewing just for our code standards?
- 22:25Are you reviewing for correctness?
- 22:28Like, this sort of cleanliness aspect of refactoring probably would make a really good checkbox in the review process.
- 22:36Yeah, definitely.
- 22:37I've also read up some articles on how to do code reviews and stuff.
- 22:41And your suggestion is perfect.
- 22:43I think it's, you know, assigning one reviewer to review that part of the code or that aspect of writing code is very helpful.
- 22:51Because, yeah, it can also come with one problem with refactoring is, say, if your whole team is all, like, very enthusiastic about refactoring.
- 23:01And you can imagine how much ideas people can have, you know, asking you to rewrite the code.
- 23:06And it can also get into not so great of a situation because you'll have a lot of iterations, right?
- 23:12One suggestion can be having several people in the design, like, involving several people in the design phase.
- 23:18So come up with something.
- 23:20And then one person can be responsible to work with the person who's writing code to do refactoring.
- 23:26That tends to streamline the process a little bit.
- 23:29I think also there's probably what you might consider, like, micro refactorings versus we're going to change the whole architecture, right?
- 23:37Like, I have some long function.
- 23:39I broke it into three and I changed the parameters to be more clear versus, oh, this actually should be another service.
- 23:45We should use queuing here.
- 23:46And this, let's make the database NoSQL.
- 23:48And, like, that's kind of, like, that's another level, right?
- 23:51Yeah, I think that's definitely, like, a do-later.
- 23:54Let's follow up on that.
- 23:57Exactly.
- 23:58But that's the thing about, like, refactoring on a legacy, a system versus a new project.
- 24:02Definitely, it's much harder when it comes to a legacy system, especially code that you're not familiar with, code that some other people wrote.
- 24:10There are so many things that you think you can improve.
- 24:13But how do you, you know, when do you start and when do you stop?
- 24:16You know, it's important.
- 24:17You can't just, like, it's a black hole.
- 24:19You just continue to refactoring until the end of time.
- 24:22So I think it's important to time box yourself as well.
- 24:25One suggestion will be, say, like, this week I spent one day on this.
- 24:29And I'll wait a bit, you know, like, I'll learn a little bit more about the code.
- 24:33And then next week I'll come back again and do another day of refactoring.
- 24:37Yeah.
- 24:37And you could probably do a couple of interesting things.
- 24:41You could probably run things like Flake 8 against it or other.
- 24:45There's probably not just pure refactoring problems.
- 24:48There's probably a host of problems.
- 24:50And so, you know, you could approach it sort of, I'm just going to make it a little bit better every day until it's not so bad.
- 24:58Right, the Boy Scout rule.
- 24:59Yeah, exactly.
- 25:00Apply it to this.
- 25:01One particularly tricky challenge with legacy systems, I think, you know, this typically happens at larger organizations, is there's some not very high profile project.
- 25:14But it's somehow really important that if it stops working, it's going to be a big problem, some kind of back-end thing or something.
- 25:22And the person who created it is either no longer on the project or left the company.
- 25:27Right.
- 25:28It's written in, like, you know, Python 2.5 or some old thing.
- 25:32And it's not currently your problem, but you would like to make it better.
- 25:37But you know if you break it, it's your problem all of a sudden, right?
- 25:40Like, if you try to make it better and you break it, you now own it because it was working and you're the one who made it not work.
- 25:46And who even knows how to deploy this thing again, right?
- 25:48That can be a real quick challenge.
- 25:49That sounds like a government problem.
- 25:51Yes.
- 25:52Yeah, I've definitely seen this at some big companies.
- 25:55Yeah.
- 25:56There's actually a book I want to give a quick shout-out to about this legacy system in particular called Working Effectively with Legacy Code by Michael Feathers.
- 26:05And he has some really interesting ideas of how to basically take a huge existing system and partition off little parts you're going to change and make them testable, flexible, refactorable without overwhelming – without trying to, you know, boil the ocean and change everything all at the same time.
- 26:23So it's pretty cool.
- 26:24A lot of techniques there.
- 26:26Oh, yeah.
- 26:26I'll definitely check it out.
- 26:28Yeah, it's really cool.
- 26:28Unfortunately, a lot of it is sort of C and Java, but the ideas in there are really interesting.
- 26:33I mean, some of them are so insane.
- 26:35It's like we're actually going to change the way the linker works to trick the system to do certain things consistently while we're making other changes in C, right?
- 26:45I mean, it's like really quite far out there.
- 26:47But you're like, oh, I didn't even think that we could take it that far.
- 26:51There's a lot of – I'm sure people will get good ideas from it even if it's not in Python.
- 26:54Yeah, for sure.
- 26:55Another problem that I think you run into around this kind of improvement but no features has to do with selling this idea to your manager.
- 27:05And I think while a lot of the modern software development methodologies are really nice, like Scrum, for example, the concept of a sprint in, you know, like a two-week or a month-long sprint, you know, you're going to sign up for some work, right?
- 27:22Like, well, where does refactoring fit?
- 27:24Like if I'm already fully booked on time, how do I go and say I'm actually going to do only half as much because I'm going to make things better?
- 27:31Like, well, we actually just need new features.
- 27:33This is really important.
- 27:34So forget the better.
- 27:35All right.
- 27:35How do you have that conversation, do you think?
- 27:37Yeah, I think it's a very important conversation to have.
- 27:40But the fact that you're thinking about refactoring, you know, like when you're adding new features, you know, you sense that something is wrong.
- 27:46That might be an indication that we need to do it right now.
- 27:50So I think the way to communicate with a product manager or with your engineering manager is, hey, if we don't do the refactoring right now, it's actually going to take six weeks.
- 27:59But if we do the refactoring, you know, spend one week on it, it'll be easier to add feature.
- 28:04So four weeks.
- 28:05I think that usually communicates the idea across.
- 28:08Yeah, I find that does work sometimes.
- 28:11What I've done in certain circumstances where it was like, look, we're just really busy right now.
- 28:16We just need to go fast and we'll deal with it later sort of mentality.
- 28:19It wasn't super as explicit as that, but where that's kind of implied, you know what I mean?
- 28:26Where it's clear that the people would much just rather have that feature right now.
- 28:31But that was if that seemed like it was always the case, you know, it's sort of like if everything is urgent, then nothing is urgent.
- 28:39It's kind of like that.
- 28:40Right.
- 28:40There is also the tech debt thing, right?
- 28:42Like, you know, we need to deal with it later if we keep on building out this tech debt.
- 28:47You know, it's not like we don't have to do it.
- 28:49Never.
- 28:49It's going to come back to you.
- 28:51It's going to haunt you one day.
- 28:52Yeah, absolutely.
- 28:53I feel like that will work well in a place like Yelp where it's a pretty technical company.
- 28:58Let's say I'm just going to completely make up a company that I don't really know whether they're technical or not.
- 29:03But let's say I work for like a food production company that makes like cereal.
- 29:08Right.
- 29:08Like those managers probably don't know or care about what technical debt is.
- 29:13They just want like the new feature for their website or something.
- 29:16What I found in those situations, I was just start adjusting my estimates.
- 29:22To include refactoring and testing instead of saying, well, I'm going to spend this much time on the feature and this much time on refactoring, this much time on testing.
- 29:30I'm going to say this feature takes X.
- 29:31Exactly.
- 29:33If I'm going to work as a professional developer, that means refactoring and cleaning up technical debt and putting in tests.
- 29:38And you ask how long it takes, this is how long it takes.
- 29:42Right.
- 29:42You just don't say it's done.
- 29:43Right.
- 29:44You're giving a professional opinion.
- 29:45I totally agree with that.
- 29:47Yeah.
- 29:47So I think you've got to adjust maybe per like what kind of environment you're in.
- 29:52But it's, it is a little tricky to say, I'm going to take a bunch of time and do nothing in terms of what you see that we get.
- 29:59I'm going to not do anything.
- 30:00Right.
- 30:01But of course, it's actually making it much better for all the reasons we talked about.
- 30:05Yeah.
- 30:05Yeah, exactly.
- 30:06Yeah.
- 30:07I do think that your, your warning earlier was really interesting though, is because there's an absolute possibility that you just like go refactoring and pattering crazy and just go like, all right, we're going to keep changing this and keep changing this.
- 30:21And I always see this, like it could really be unended.
- 30:24So I guess one of the things that might be interesting to talk about is like, when do you know that you should refactor and how should you go about that?
- 30:33Yeah.
- 30:33This is a very interesting topic.
- 30:35So in the same book, actually, Ken Beck coined the term of code smell.
- 30:39So pretty much means it's a surface indication that usually corresponds to a deeper problem in the system.
- 30:47So I would like to make a metaphor with cheese.
- 30:50Sometimes like, you know, the key word of this is the indication.
- 30:54So sometimes cheese can smell very strong, you know, especially those like French cheese.
- 30:59And then like you think there is something wrong, but when you eat it, it's actually good.
- 31:03So code smell is an indication of a problem.
- 31:06But you really need to take some time to investigate into it if, you know, it's actually a problem.
- 31:11So you have like a very long function, but it actually does one thing.
- 31:15So, okay, perfect.
- 31:15This concept of a code smell is absolutely just like captured my imagination when I first heard of it because it's so perfectly captures what is wrong.
- 31:25It's like if you look at some code and your nose kind of wrinkles up, you know, like, oh, look at that.
- 31:30Like if it works, it's actually working just fine.
- 31:33But to get in there and to be with it is a little unpleasant.
- 31:36Like that is just the perfect idea of this code smell, right?
- 31:40Yeah.
- 31:40Well, that's the thing.
- 31:41Like, you know, any programmer can write things the machine can compile.
- 31:45But then only good programmers can write code that, you know, humans can read.
- 31:50Yeah, absolutely.
- 31:51And so I think what's interesting about the code smells is it's not just like, hey, there's this idea of smelly code, but there are actually smells, flavors of smell, like types of smells that then are prescriptive of different refactorings, which is super interesting.
- 32:09And that's kind of what we were talking about at the beginning where some of the smells are more applicable to, say, like Java than they are to Python.
- 32:15But still, there's plenty of Python analogies here.
- 32:19Before we get into the code smells, one thing that also Martin Fowler talks about that just I think is so perfect is he talks about the idea of code comments being deodorant for code smells.
- 32:31Yeah.
- 32:32I really like that analogy.
- 32:34So if, you know, if you have a lot of comments on your code, then that probably indicates that you didn't write it very clearly.
- 32:41That's why you need to write comments to explain yourself.
- 32:44So it's important to know that, you know, when you write a comment, you should address why you're writing this, but not what you're trying to say.
- 32:52If it's a what problem, then maybe you should rename it or try to use some variables to explain what you're trying to do here.
- 32:59I think this might be the most important idea of this entire code smell thing is literally every time I go to write a comment, I stop and go, why am I writing this comment?
- 33:09Is it really that I should just stop and rename the function?
- 33:12Is the function badly named?
- 33:14Are the parameters badly named?
- 33:15Is the function too big?
- 33:17And so I could break it into smaller pieces.
- 33:18So each one can then be really clearly named because right now it seems like the name would be a paragraph, right?
- 33:24All of those types of things.
- 33:25Right.
- 33:26Exactly.
- 33:26Yeah.
- 33:27And people all the time try to fix these with code comments.
- 33:30And it's just like, just delete the comment, make the variable name three characters longer, but understandable.
- 33:35Right.
- 33:36Exactly.
- 33:36Totally agree.
- 33:38That's awesome.
- 33:39All right.
- 33:40So maybe take us through some of the various code smells and how we might fix them.
- 33:44Sure.
- 33:44I can like vaguely categorize them into different classes.
- 33:48So first you have long and complex code.
- 33:51You have useless code, coupled code and inappropriate naming.
- 33:55I think we can go through them one by one.
- 33:57So let's start with the long and complex code.
- 34:00Sometimes in your program, you can see very long functions and classes.
- 34:05That might be an indication that your class or function is not doing one thing, just one thing.
- 34:11So, you know, that violates the single responsibility principle and the drive principle, you know, don't repeat yourself.
- 34:17So in this case, maybe we can extract the function or classes so that everything is encapsulated well.
- 34:25When you're doing this, like when you're using this technique of extracting functions, though, beware of pass by reference versus pass by value.
- 34:33It's like this mistake everybody makes and you pass in a dictionary rather than just viewing, just getting it.
- 34:40You're modifying what is inside and, you know, the list dictionary, there are mutables.
- 34:45It's very dangerous.
- 34:46It's evil.
- 34:47Yeah.
- 34:47Yeah, you do have to be careful about that.
- 34:51I think this is probably one of the most common things you run into is just something started out small and it grew and it grew and it grew and nobody wanted to really mess with it.
- 35:01They just wanted their feature in there.
- 35:03And so they added a little bit more, you know, another if clause, another conditional or whatever, right?
- 35:09Yeah, adding new keys to your dictionary, like you don't even know when you like pass by, you passed in the dictionary and in the end, the dictionary totally got muted.
- 35:17And yeah, it's very hard to keep track in this way.
- 35:20Yeah, for sure.
- 35:20And some other long and complex code problems as well, say temporary field.
- 35:25So that pretty much means you call a function and you cast it into a variable.
- 35:30But sometimes if you're not using this variable for a few times, you can just call it in line.
- 35:35Otherwise, it gets really confusing.
- 35:37And when you're trying to extract functions, those are the things that can prevent you from doing it very simply.
- 35:42And one other thing I want to point out is the conditional complexity, which I think most developers probably encountered.
- 35:50Me too.
- 35:50You know, you just want to write something very simple if else and you ended up having three, four layers of nested conditional logic.
- 35:58And that's just really, really hard to read.
- 36:01Yeah, I find that people do that a lot because they, how is the right word?
- 36:06They're testing for success.
- 36:07So I'm going to say, I'm going to do a loop.
- 36:11And then if this thing I want to work on is true, I'm going to go into that part.
- 36:16And then if this other condition also is true, I'm going to go in it.
- 36:19You end up like almost scrolling right just to read what the code is doing, you know?
- 36:24That's where the line limit came in for PEP 8, I guess, you know.
- 36:28But 79 is very, very strict.
- 36:29I would have to say, you know, you can probably modify it for your own need.
- 36:33But that could possibly stop people from writing too long of a code.
- 36:37Maybe you're trying to encompass too much information on one line.
- 36:41Yeah, for sure.
- 36:42Well, and if people are looking for something concrete, you're like, okay, I know this is not good, but what do I do?
- 36:48Like you can reverse the if statements.
- 36:50You can do what's called a guarding clause that'll say, if it's not good, either skip this time to the loop or return early.
- 36:57Instead of if yes, yes, yes, if yes, if yes, if you just go, if no, return, if not this, return, if not that, return.
- 37:04And then what's flat below is the actual thing you want to do.
- 37:07That can really help.
- 37:08Exactly.
- 37:08Yeah, like totally.
- 37:09Like guarding clauses is a good way to go.
- 37:11And in here, I would even value simplicity.
- 37:14And I would even sacrifice, you know, the shortness for the conditional complexity.
- 37:21So even being more verbals, having more verbal statements, I think it would help instead of introducing the nested conditionals.
- 37:29Yeah, nesting is bad.
- 37:30Yeah, exactly.
- 37:31I think that's also mentioned in the send of Python.
- 37:34You have flat is better than nested.
- 37:36So it's good to remember that.
- 37:38Just import this if you find yourself three levels deep.
- 37:41Yeah, exactly.
- 37:42This portion of Talk Python To Me has been brought to you by Rollbar.
- 37:48One of the frustrating things about being a developer is dealing with errors.
- 37:52Relying on users to report errors, digging through log files, trying to debug issues,
- 37:57or getting millions of alerts just flooding your inbox and ruining your day.
- 38:01With Rollbar's full stack error monitoring, you get the context, insight, and control you need to find and fix bugs faster.
- 38:07Adding Rollbar to your Python app is as easy as pip install Rollbar.
- 38:11You can start tracking production errors and deployments in eight minutes or less.
- 38:16Are you considering self-hosting tools for security or compliance reasons?
- 38:20Then you should really check out Rollbar's compliant SaaS option.
- 38:23Get advanced security features and meet compliance without the hassle of self-hosting, including HIPAA, ISO 27001, Privacy Shield, and more.
- 38:33They'd love to give you a demo.
- 38:34Give Rollbar a try today.
- 38:36Go to talkpython.fm/Rollbar and check them out.
- 38:40So what are some more in this area?
- 38:42As we mentioned, the mutable problem.
- 38:45Using dictionary as a param is pretty dangerous.
- 38:48Just now we talked about why.
- 38:49So I would suggest using named tuple because that's the thing.
- 38:53If I see a function and I'm debugging something, I see it passing in as a param, I'll probably cry a little bit inside.
- 39:01Say it's just like a location, right?
- 39:03I have so much imagination in my mind.
- 39:06Could it be like a latitude, longitude?
- 39:08Like is it a number?
- 39:09A string?
- 39:10Could it be like city, country?
- 39:12I have no idea.
- 39:13I need to throw in the debugger.
- 39:15I need to like try to run the program and see what exactly is inside.
- 39:20But if you use named tuple, it's defined.
- 39:22You know exactly what is inside.
- 39:24And when it comes to Python 3.6, there's type annotation.
- 39:28So really like no questions asked.
- 39:31You don't have to guess anything.
- 39:32I think it's a very good way to go to prevent a lot of the work that has to be done and prevent bugs.
- 39:39I really like that suggestion.
- 39:41It's great because it takes this kind of unknown thing and captures it into something that you know exactly what's there, how to access it.
- 39:48And the type annotation reinforces that that is actually the thing that's going there.
- 39:53Exactly.
- 39:54So you don't have to worry about the, because it's immutable, you don't have to worry about it being changed.
- 40:00So less bug will be introduced.
- 40:02Yeah, that's true.
- 40:03Yeah, very, very cool.
- 40:04Another one that I want to throw in here that does not have a code smell, but I want to give it a name.
- 40:09I'll run this name by you and see what you think.
- 40:11You know, you talked about dictionaries and parameters.
- 40:14That's kind of hard.
- 40:15When I see a method that takes star args, star star, kwrgs, just like I'll just take anything.
- 40:21You name it, you don't name it.
- 40:22I don't care.
- 40:23Just give it to me.
- 40:24I'm just like, oh my goodness.
- 40:25What do I do here?
- 40:26Like I had such a hard problem switching data centers in S3 because I needed to change like the encryption mode.
- 40:35It was like the craziest thing with the Bodo API.
- 40:37And I went to look in the, it was like this.
- 40:40And I went to look at the documentation.
- 40:41And one of the kwrgs was itself a dictionary, which only had a name and had no description of what even goes into the dictionary.
- 40:49I'm just like, oh, how do I supposed to do this?
- 40:51So my proposed code smell name for those types of methods are starry calls.
- 40:57Yeah, perfect.
- 40:58You're coining the term today.
- 41:00Starry call.
- 41:00I like it.
- 41:01Starry call.
- 41:02There you go.
- 41:02Yeah, some people call it black magic, you know, the quarks that got passed in.
- 41:06Yeah, I guess like Python, the structure of Python, it really lets you do so many powerful things.
- 41:12But at the same time, you really have to be responsible about it because with great power comes with great responsibility.
- 41:18Yeah, for sure.
- 41:19Yeah, I understand why these methods sometimes exist.
- 41:23But I feel like a lot of times people are just like, well, it's easier than just like making people name the orders and we'll just let them put whatever and we'll figure it out.
- 41:29It's like, yeah, but that doesn't actually help them use it.
- 41:32You know what I mean?
- 41:32Yeah, exactly.
- 41:33So the next section is useless code.
- 41:35Right.
- 41:36So as you mentioned, too many comments is definitely a deodorant.
- 41:40Definitely we should write down why, but not what you're trying to do.
- 41:44If that's the case, then you probably should consider renaming or explaining it.
- 41:49One very common one is just the duplicated code.
- 41:53So yeah, adhering to the dry principle, you should extract functions or considering an inheritance.
- 41:59Well, in Python, you have other ways to do it as well, which we might cover later.
- 42:04It's the composition pattern, which might work a little bit better in Python's case.
- 42:09And sometimes you have dead code.
- 42:11Well, because your code is not modularized, it's very hard to tell if your line is being executed.
- 42:18But there are some IDs that are smart enough to tell you if the code is not executed or if some variables are not used.
- 42:24So that can be a good help.
- 42:26Or sometimes you have lazy classes.
- 42:29So you have this one class that doesn't really have any functions, only have some fields.
- 42:35Yeah, like pretty much some fields.
- 42:37And in this case, you can just replace it with a named tuple.
- 42:40And that just makes things easier because maintaining classes takes energy, takes time.
- 42:46Yeah.
- 42:46And actually, named tuples use less memory than I ever give a class anyway.
- 42:49So it's probably slightly more efficient.
- 42:51Yeah, I would agree.
- 42:53Yeah.
- 42:53So one tool, these are all great.
- 42:54And they all can drive me crazy.
- 42:56Like I've spent untold hours getting hold of some project, looking at some method, going, I don't understand how this is working in this environment.
- 43:07Like it really seems like this doesn't work.
- 43:09And so I'm trying to understand this code.
- 43:11And then it turns out that like after a lot of piecing stuff together, like, oh, the reason it doesn't seem to have any effect is because it's never called.
- 43:20Like, oh, my gosh.
- 43:23Oh, no.
- 43:23Right.
- 43:23Yeah.
- 43:24You know, no, it's just so frustrating.
- 43:25It's a little bit of the broken window syndrome.
- 43:27It's just like people left it in there, but they were afraid to take it out.
- 43:30Right.
- 43:30Yeah.
- 43:31The CSS.
- 43:31Yeah, exactly.
- 43:32It's like the CSS problem.
- 43:33The other one I want to give a shout out to, which is really just like it's so delightful,
- 43:37is in PyCharm, you can open up a huge project.
- 43:42Let's say it has like 100 files.
- 43:43You don't have to select anything or do anything.
- 43:45You can just go to a menu.
- 43:46I can't remember where it is.
- 43:47But you can say find duplicate code and it will compare like blocks.
- 43:52It'll just go, oh, this sort of test here is done actually in 20 places.
- 43:56You could just make that a method.
- 43:57Yeah.
- 43:58And that's pretty cool because you don't have to guide it.
- 44:00You just say go find the duplicates and it'll somehow like put that all together.
- 44:03I love all these like tools that can help us refactor code.
- 44:06It's awesome.
- 44:07Yeah, for sure.
- 44:08So what's the next section?
- 44:09So we'll talk a little bit more about coupled code.
- 44:11So you have something called the message chains.
- 44:14Pretty much means function A calls function B and then function B calls function C.
- 44:20So when one thing changes, say in the chain, right, function C changes, there is a ripple
- 44:26effect.
- 44:26So everything has to change.
- 44:28All your functions have to change.
- 44:29And for functions that have this message chain problem, it's very, very hard to test.
- 44:35So in this case, you know, the productivity is just drained away from first writing the function,
- 44:41understanding the function and then writing tests, which is really not great.
- 44:45Yeah, that can be one of those problems where you try to make some small change down at some
- 44:50lower level and it like cascades through every layer of the application.
- 44:54You feel like you're changing so many files just in order to like, well, like let's take
- 44:59an example.
- 45:00Like I want to add an extra parameter to the creation of an object way down low.
- 45:04Well, that means the method that calls that has to pass it, but the class that calls it
- 45:07doesn't have it.
- 45:08So its constructor has to take another parameter, which, and then just creates this like this
- 45:14sort of combinatorial explosion of like, why am I just doing this everywhere?
- 45:17This is crazy.
- 45:18Right.
- 45:20Yeah, I would totally agree.
- 45:21So if we can flatten it out a little bit, maybe function A can call function B and then
- 45:26function A can call function C.
- 45:28That'll make it a little bit better and easier to test.
- 45:31Yeah, for sure.
- 45:32Or maybe even there's some other mechanism from getting that information deep down there.
- 45:36Like maybe it's stored in the database instead of passed or I don't know.
- 45:39It really depends.
- 45:40Yeah.
- 45:41Or create static functions.
- 45:42Those are always great.
- 45:44Pure functions.
- 45:45They're dumber.
- 45:46It's easier to test.
- 45:47I don't know what to make the next one.
- 45:49Indecent exposure.
- 45:50Oh, it's very clear.
- 45:51It's exposing your privates to other classes.
- 45:55That sounds very indecent.
- 45:56Yeah.
- 45:57If your class is consistently calling like functions from another class, then it's better
- 46:02to combine it probably.
- 46:03So that means you have code that's too modularized in a way that maybe if they share the same
- 46:10context, they should be put together.
- 46:12That's an interesting comment because I feel like one of the things that's funny about
- 46:17refactoring and the code smells is they often have what I want to coin as refactorial inverses,
- 46:25right?
- 46:25Like multiplicative inverses, right?
- 46:27Like there's inline variable and there's create variable.
- 46:30There's inline method.
- 46:31There's extract method.
- 46:33There's push this to subclass.
- 46:35Pull it down.
- 46:37Pull the subclass.
- 46:38Push it to all the derivative ones.
- 46:40There seems to be like this thing and this undoing thing often in refactoring.
- 46:45And it really is context driven, right?
- 46:47Yeah.
- 46:47That's why I say it's an iterative process because once you have added some code, the situation
- 46:52is different again.
- 46:53And maybe what could be code smell before is not a code smell now.
- 46:57Or if you, upon investigating, the code smell actually doesn't point to anything.
- 47:02So it really, it's a constant effort, I would say, to keep your code good along the lines.
- 47:09Yeah.
- 47:09It's also why you can basically refactor for infinite time because you can do the thing
- 47:14and then you undo the thing.
- 47:15Then you do the thing in a different way.
- 47:17Yeah.
- 47:18So the last section you wanted to cover in this area was inappropriate naming.
- 47:23Yeah.
- 47:23I would love to cover this area because it's one of the three hardest problems in computing,
- 47:28right?
- 47:29You have cache invalidation, you have threading, and then you have naming.
- 47:33That's right.
- 47:34And this is something I care a lot about as well.
- 47:37Like it's kind of really closely tied to the code comment stuff and so on.
- 47:41Yeah.
- 47:41That I would totally agree because Python is dynamically typed.
- 47:45So in a way, when you create a variable, it doesn't really have to type information stuck
- 47:50to it.
- 47:50And in this case, you know, with great power comes with great responsibility.
- 47:55We need to name things right because we kind of didn't have this, some extra information
- 48:00as other languages would have had.
- 48:03You know, naming variables correctly, naming modules correctly.
- 48:06One thing you recommend are keyword arguments or at least calling functions in the keyword
- 48:11argument style, right?
- 48:12Definitely.
- 48:13So in this case, you don't have to have like 10 terminals open to see the function definition
- 48:18when you're stumbled upon this code, right?
- 48:21Immediately, you know what is being passed into the function.
- 48:24So I think it's just a more efficient way because it's always good to be explicit than implicit
- 48:30by the sense of Python.
- 48:31Yeah, for sure.
- 48:32Another thing that I think can be challenging is sort of implicit values or magic values.
- 48:40See what I mean by that?
- 48:41It's like you had an example around a function that took the mood of a person and the mood
- 48:46could be like one, two, three, or the sets of numbers.
- 48:50And is three good?
- 48:51You don't know, right?
- 48:52Like it's really hard to understand that stuff, right?
- 48:55Yeah, I would totally agree.
- 48:56Like it's definitely like indicating the direction is important, right?
- 48:59So mood bigger than three.
- 49:01What does that mean?
- 49:01Is it happy?
- 49:02Is it sad?
- 49:03So casting it into a variable called is happy equals mood bigger than three can have a
- 49:09wonderful effect of documenting your own code.
- 49:12For sure.
- 49:12And Python recently added enumerations as well, enum classes.
- 49:17And if there's only four moods, you know, making that an explicit enumeration.
- 49:22So there's like a sad sort of blase or like hair, like then there's happy, super excited, right?
- 49:28Like it would be really clear that way.
- 49:31And that'd be a pretty good refactoring too.
- 49:32Yeah, for sure.
- 49:33So this is all well and good.
- 49:35And I would like a better list of Python code smells to guide us, but there's still plenty
- 49:41to work with from sort of existing, existing literature and writing and stuff.
- 49:46So how do you go about developing your code nose?
- 49:51I definitely agree on reading literature on this, but I think it's just a skill that you develop
- 49:56over time.
- 49:57So if you actually just look at some legacy code, you can find a bunch for sure.
- 50:02And just seeing it day to day, you know, like just read more of that and try to refactor
- 50:09more of that.
- 50:09I guess we can learn, you know, at which point we need to refactor this.
- 50:13Is this an indication of a problem?
- 50:16It really comes with experience.
- 50:18And also the code review process can help you learn from other people in your team or, you
- 50:23know, whoever is reviewing.
- 50:24Yeah, I think the code review process is super helpful if at least that's being incorporated
- 50:29to the code review.
- 50:30Yeah.
- 50:30If you're working more on your own and, you know, there's plenty of people who work even
- 50:34in companies, but there's, they're kind of more or less on their own.
- 50:38They're not in a technology company.
- 50:39That code review can be more or less absent.
- 50:42And so one of the things that came to mind while you're thinking about this is like, or
- 50:46you're speaking about this to me is, you know, there's all these code smells.
- 50:49You could take like one code smell a week and say, all right, I'm going to work on the conditional
- 50:55complexity problem this week.
- 50:56So anytime I'm writing code, if I see, I feel myself writing like that fourth indentation
- 51:01level, I'm going to like remind myself to apply this refactoring or this, this, this technique.
- 51:06And you could just take them one at a time, right?
- 51:08Cause they seem overwhelming altogether, but they're pretty simple by themselves.
- 51:12That's definitely a good idea.
- 51:14One of the dangers of refactoring, I mean, it's extra high in legacy systems, but in
- 51:18general, it's a danger is that your intention is to refactor code, but your actual outcome
- 51:24is you've changed code, right?
- 51:25It behaves differently.
- 51:26So what's the role of testing here?
- 51:28I think testing is very important in the workflow.
- 51:32So what I would suggest is writing integration tests first for the code you're about to refactor,
- 51:37if it's not present yet.
- 51:38And then during your refactoring process, you know, after you extract the function, after
- 51:43you change the variable names, run it over again to check if the functionality of your code
- 51:48base has changed.
- 51:49So that really helps you to limit loss.
- 51:51You know, like after you change one thing, it's very easy to spot out what has been changed.
- 51:56And after refactoring is done, you can start introducing unit tests to test that the functions
- 52:02you've introduced actually uses the right logic.
- 52:05So with this workflow, I think we got our ground covered.
- 52:09Yeah.
- 52:09Yeah.
- 52:10That sounds like a good, a good way to do it.
- 52:12And what test frameworks use like the built-in one, use pytest, use Nose?
- 52:16Yeah.
- 52:16At Yelp, we definitely have a built-in one, but there is pytest as well, which is very, very
- 52:20similar.
- 52:21You can just assert things.
- 52:23You know, there are a lot of inbuilt functions that you can use.
- 52:26Also, one thing that Python is great is you can use mocks.
- 52:30So that's, you know, if you have a network call that you don't want to actually make during
- 52:35your testing, then you can mock things out.
- 52:37And that's really, really convenient.
- 52:38Yeah.
- 52:39Very nice.
- 52:39So maybe let's talk about some of the tools that you could apply here, because in Martin
- 52:45Fowler's 1999 book, he literally shows you the manual steps at every level.
- 52:52Like, okay, here, first you create this variable, you put this piece here, and then you do this
- 52:56step.
- 52:56And it's really painful, but there's more we can do these days, right?
- 53:01I do think so.
- 53:02So one thing is, you know, the styling part of it first.
- 53:06So we have PEP 8, right?
- 53:07PEP 8 is a Python enhancement proposal that talks about, it's essentially a Python style guide.
- 53:14So how you're supposed to structure the white spaces, how do you comment things, how do you
- 53:20use the string quotes, things like that.
- 53:23So that there are a lot of tools that can help you.
- 53:25There's the PEP 8 tool, there's Flake8 that can also help you check conditionals as well, I believe.
- 53:32Does it check for things like dead code and stuff like that as well, like unused parameters
- 53:36or methods not called?
- 53:37Yeah, that's big.
- 53:38Yeah, or PyLint.
- 53:39And there are a lot of things that we can use that can be programmatic about it.
- 53:44I would want to mention Raymond Hedinger's talk.
- 53:47He was mentioning how PEP 8 can become a nightmare because you just have someone from the team
- 53:52that bugs everyone about, hey, your trailing comma is off, you know, your white space is off.
- 53:58But you know, there are actually tools that can do that.
- 54:01And also, you should PEP 8 onto thyself, you know, not to bug everyone in a team about it.
- 54:07It's like a style guide.
- 54:08And it should surf like one, right?
- 54:10Because if at the same time you're pissing off your colleagues or if readability is not
- 54:16improving because of this style guide, then maybe it's not worth it to do it in the first
- 54:20place.
- 54:21Yeah.
- 54:21And you mentioned it is beyond PEP 8 talk.
- 54:23That is really quite insightful.
- 54:26It's a good example of how just following strict rules can lead to actually less readable code.
- 54:33Whereas if you let it slip just a little bit, but you're creative about it, there's actually
- 54:38better ways or maybe even not so much that you break PEP 8, but that PEP 8 is not the end.
- 54:45There's actually more important stuff to think about than PEP 8 about how you structure code,
- 54:49whether it's more Pythonic and the way that it works.
- 54:52It's a really good talk.
- 54:53I think it's done during PyCon as a PyCon talk beyond PEP 8.
- 54:57One thing he also mentioned is rather than thinking too much about PEP 8, maybe you should
- 55:03think about the problem P versus NP.
- 55:05So, you know, like silence.
- 55:07What is the P?
- 55:08What is the NP?
- 55:08It's Pythonic versus not Pythonic.
- 55:12That's right.
- 55:13That's right.
- 55:14Because you could write perfectly PEP 8 compliant, very non-Pythonic code, or you could just think
- 55:21beyond it and actually make the code better.
- 55:23Yeah, it's a good talk.
- 55:24And it was at PyCon, I believe, in that.
- 55:26So there's a video version, which I'll try to add in the show notes for people.
- 55:30Yeah, I really like that talk.
- 55:31Yeah.
- 55:32So maybe that's a good place to leave it for the refactoring stuff directly.
- 55:37But this was super interesting to talk about.
- 55:39Thanks for sharing your thoughts on that.
- 55:41One thing I did want to point out to people, folks ask me often about like where to get
- 55:46jobs and how to get into Python.
- 55:48Maybe they're doing C and they want to like move over to a place that actually does Python.
- 55:52So you at Yelp, you guys are hiring, right?
- 55:55Definitely.
- 55:56We have so many empty seats to fill and we really want you to be here.
- 56:00We use a Python backend.
- 56:01We have over 300 microservices and we have millions of search requests per year.
- 56:07There are a lot of interesting projects, you know, data for you to play with.
- 56:11So definitely we would love to have you here.
- 56:14Yeah, it sounds like a fun place that cares about code quality and craft.
- 56:18Very nice.
- 56:18Yeah.
- 56:18And we have offices in San Francisco or if you want to go to Europe as well, we have London
- 56:24and Hamburg.
- 56:24Cool.
- 56:25All right.
- 56:26So before we get out of here, let me ask you the two questions.
- 56:29So if you're going to write some Python code, what editor do you use?
- 56:32Well, since I'm a full stack engineer, I would actually suggest using Cloud9.
- 56:37I don't know if you've heard of it before.
- 56:38So it's pretty much you can code on the browser.
- 56:41It's super easy to set up.
- 56:42You can just like git clone the whole repo into whatever environment you're working with
- 56:47and you can pair programming real time.
- 56:49And it's just seamless for full stack development.
- 56:52Good for Python down to CSS.
- 56:54It's great.
- 56:55That's really cool.
- 56:56And I really like the real time collaboration.
- 56:58It's sort of like Google Docs for code.
- 57:00Yeah, definitely.
- 57:01And, you know, it's in the cloud, so you can pick up your progress wherever you left it.
- 57:06You could code on your iPad if you want.
- 57:07Perfect.
- 57:08Yeah.
- 57:09Yeah, that's right.
- 57:10All right.
- 57:11And then notable PyPI package.
- 57:13Like what's one out there that maybe people don't know about that is pretty cool.
- 57:16Just now we mentioned PEP 8 or Flake8.
- 57:18There are pretty cool links that can help you get your quote quality there.
- 57:23And yeah, I think definitely you should check out the sign of Python because I think it's
- 57:28pretty much the key rules of this Python language.
- 57:32There are a lot of things that we covered already, like flat is better than nested.
- 57:36Simple is better than complex or visibility counts.
- 57:40But whenever you make a decision when it comes to writing code, then when in doubt, import this.
- 57:46Yeah, that's perfect.
- 57:47All right.
- 57:49Final call to action.
- 57:49People are excited to get started with refactoring or do it better.
- 57:53What would you say to them?
- 57:54Just start right now.
- 57:55Like you don't need a lot of tools.
- 57:56You don't need anything.
- 57:57Start reading materials.
- 57:59Start learning the patterns or code smells one by one.
- 58:02I think it's a fun journey ahead.
- 58:04Yeah, I totally agree.
- 58:05Thanks for that.
- 58:06And thanks for being on the show.
- 58:07It was great to talk to you.
- 58:08Yeah, thank you.
- 58:08It was great.
- 58:09Yep.
- 58:09Bye.
- 58:10This has been another episode of Talk Python To Me.
- 58:13Today's guest has been Yeni Chong.
- 58:16And this episode has been brought to you by ParkMyCloud and Rollbar.
- 58:20Do you hear that sucking noise?
- 58:23That's your cloud provider making you pay for your idle instances.
- 58:26Turn on ParkMyCloud, plug the leaks, and save money.
- 58:29Visit talkpython.fm/park to get started.
- 58:32Rollbar takes the pain out of errors.
- 58:35They give you the context and insight you need to quickly locate and fix errors that might have
- 58:40gone unnoticed until your users complain, of course.
- 58:43As Talk Python To Me listeners, track a ridiculous number of errors for free at
- 58:48rollbar.com slash talkpythontome.
- 58:51Are you or a colleague trying to learn Python?
- 58:53Have you tried books and videos that just left you bored by covering topics point by point?
- 58:57Well, check out my online course, Python Jumpstart, by building 10 apps at talkpython.fm slash
- 59:03course to experience a more engaging way to learn Python.
- 59:06And if you're looking for something a little more advanced, try my Write Pythonic Code course
- 59:11at talkpython.fm/pythonic.
- 59:13Be sure to subscribe to the show.
- 59:16Open your favorite podcatcher and search for Python.
- 59:18We should be right at the top.
- 59:20You can also find the iTunes feed at /itunes, Google Play feed at /play, and
- 59:25direct RSS feed at /rss on talkpython.fm.
- 59:29This is your host, Michael Kennedy.
- 59:31Thanks so much for listening.
- 59:32I really appreciate it.
- 59:33Now, get out there and write some Python code.
- 59:35I'll see you next time.
- 59:55Thank you.