Latest / Talk Python To Me / #266: Refactoring your code, like magic with Sourcery
Transcript
- 0:00Refactoring your code is a fundamental step on the path to professional and maintainable software.
- 0:04We rarely have the perfect picture of what we need to build when we start writing code,
- 0:09and attempts to overplan and overdesign software more often lead to analysis paralysis rather than ideal outcomes.
- 0:16Join me as I discuss refactoring with Brendan McGinnis and Nick Thoppen,
- 0:21as well as their tool, Sorcery, which adds automatic refactoring in the popular Python editors.
- 0:28This is Talk Python To Me, episode 266, recorded May 21st, 2020.
- 0:32Welcome to Talk Python To Me, a weekly podcast on Python, the language, the libraries, the ecosystem, and the personalities.
- 0:52This is your host, Michael Kennedy. Follow me on Twitter where I'm @mkennedy.
- 0:56Keep up with the show and listen to past episodes at talkpython.fm, and follow the show on Twitter via at Talk Python.
- 1:02This episode is sponsored by Datadog and Linode.
- 1:05Please check out what they're offering during their segments. It really helps support the show.
- 1:09Brendan, Nick, welcome to Talk Python To Me.
- 1:12Thank you very much.
- 1:13Thank you for having us.
- 1:14Yeah, it's great to have you here.
- 1:16I'm such a huge fan of refactoring and code quality and all these ways of taking living software and making it evolve, right?
- 1:26I think long gone are the days of we have to plan this perfectly, and then we're going to build the perfect thing that we've thought up, right?
- 1:33And so having this idea of continuously evolving and improving code, it just frees you from worrying about trying to get it all right and you can just get started.
- 1:40And so I'm really excited to talk about sorcery and refactoring with you guys.
- 1:43Awesome. Yeah, we think iteration is super important as well.
- 1:45Sort of trying to get a skeleton of the thing up and running and then sort of tidying up later.
- 1:51Yeah, absolutely.
- 1:52That's kind of how we like to work.
- 1:53No one really understands the domain when they first write the code anyway.
- 1:57You have to write the code, find out all the mistakes that you've made, and then tidy it up, clean it up.
- 2:03Over time, like you say, it evolves into what looks to be a nice quality code base and solves the needs of the users.
- 2:13Yeah, I think that's a super good point because understanding, you don't fully understand it until you've gotten mostly into it.
- 2:20But if you tried to understand it all, it's just so much work.
- 2:23Even if you get it right, it's so much work to get to that point that you might as well have just written it three times.
- 2:27So that's awesome.
- 2:29But before we jump into more of that, let's just get your stories.
- 2:33And Nick, I guess we'll start with you first.
- 2:34How do you all get into programming into Python?
- 2:36So I guess the first program I did was back in school on these graphics calculators we had back in the 90s.
- 2:42And I remember reading a book about complex numbers and being super proud of getting my calculator to draw a fractal.
- 2:47I think it took about 12 hours to draw it.
- 2:50But I end up with a Mandelbrot set on my little calculator.
- 2:54Like the Mandelbrot set or something cool like that.
- 2:56Could you zoom in?
- 2:57I think you could zoom in, but it would take another 12 hours to show you.
- 3:00You could only do it twice because then the battery would run out.
- 3:03Pretty much.
- 3:04I did a little bit of basics and stuff, but I never did programming in a serious way until joining a software company after university.
- 3:10What are you studying at university?
- 3:12So it's maths and philosophy.
- 3:13Okay.
- 3:13That's a cool combination, actually.
- 3:15Yeah.
- 3:15And there's like a lot of logic, which I guess is quite key to programming.
- 3:19And then the first languages I learned in the software company were this mainframe language called IBM RPG.
- 3:24I don't know if you've come across that one.
- 3:27I could not look at RPG source code and tell you what it is.
- 3:30Like I couldn't identify it from code.
- 3:32Yeah, I've heard of it.
- 3:33That's incredible.
- 3:33It's sort of, it's like punch cards.
- 3:35Everything has to be in the same, in the right column kind of thing.
- 3:38Okay.
- 3:39Because it was this finance company.
- 3:40Brendan went there as well.
- 3:41It sounds as useful, but I think I'll stick with Python.
- 3:43Yeah.
- 3:43I had this code base from the eighties.
- 3:46Okay, cool.
- 3:47And you had to program it in a green screen terminal.
- 3:48So that didn't put me off.
- 3:50Yeah.
- 3:50If you go through that, you're definitely good for this industry.
- 3:52If you can make it through that, Steve, you'll be good.
- 3:55And then it was Delphi, which I also look back on as not being super amazing.
- 3:59And then Java.
- 4:01And then it was only when I joined Imperial and started doing sort of machine learning that I got into Python, I guess, mid 2010s.
- 4:08Cool.
- 4:08You're like, why does this language have semicolons?
- 4:10What happened to it?
- 4:11Yeah.
- 4:11I mean, after Java, it just was like a breath of fresh air, really.
- 4:14And when I had to go back to doing Java, it seemed so verbose.
- 4:17Yeah.
- 4:17You're like, why?
- 4:18I can't read this.
- 4:18There's symbols all over what it's trying to tell me.
- 4:21Yeah.
- 4:21Yeah.
- 4:21Curly braces and parentheses and semicolons.
- 4:24Yeah.
- 4:24And the way they do the libraries and things, actually, it seems to be all done in a very verbose way.
- 4:28Yeah.
- 4:28Indeed.
- 4:29Lots of boilerplate.
- 4:30Nice.
- 4:30Cool.
- 4:30Well, Brendan, how about you?
- 4:31The first programming I did was in my final year math degree.
- 4:36So we had a project to do, which was to simulate various mathematical equations.
- 4:44So the ones that I chose were the heat equation.
- 4:47And it would simulate one wall being hot and another wall being cold and various obstacles between the two walls.
- 4:57And then after it ran, it would show this chart of the temperature at different places in the room.
- 5:03And the other thing I simulated was the Schrodinger equation, which quantum mechanics.
- 5:10Yeah.
- 5:10Quantum mechanics is so interesting.
- 5:12And it's just like, it seems like such a weird Twilight Zone alternative reality.
- 5:17And yet it seems to be applying to real reality.
- 5:20It's such a weird world.
- 5:21I love quantum mechanics.
- 5:22Yeah.
- 5:23It's amazing how accurate the predictions are.
- 5:26And yet when you try to understand it, it just has no similarity to reality.
- 5:32Just making that up, aren't you?
- 5:33There's no way this is real.
- 5:35When you get so small, it doesn't match up with what you understand.
- 5:39Yeah, for sure.
- 5:41How can this object be in two places at the same time?
- 5:43What does that even mean?
- 5:45Exactly.
- 5:45Yeah.
- 5:47Well, the equation, I was just simulating a single object in the waveform of it.
- 5:52So it was quite simple.
- 5:54But the way they did it was, here's min GW, go and program it in C, and then just send us off to go and do it.
- 6:03And we knew nothing about how to program.
- 6:06So my programs were just hundreds of lines of statements, absolutely no functions, nothing, no structure.
- 6:13Yeah.
- 6:14I think we probably all got-
- 6:15Probably some memory leaks in there as well if it was written in C.
- 6:17Oh, yeah.
- 6:18The whole thing, yeah.
- 6:19The whole thing was pretty terrible.
- 6:21But you get to the end of the project and you simulate it correctly and you think, I can program now.
- 6:28I know how to program.
- 6:30Even if that's not entirely true, I think you do come away with this feeling of like, oh my gosh, look what I built.
- 6:36Like, this is so awesome, even if it's not really.
- 6:39It's just like that feeling of creating that thing is so cool in those days when you're getting started.
- 6:44Absolutely.
- 6:45I think that, yeah, the first time that I managed to output the heat equation onto the screen where you see the different temperature across the room,
- 6:55they provided us with a charting library to use.
- 6:58That was amazing.
- 6:59And I was like, wow, I can program.
- 7:01I can do anything.
- 7:03I know computers now.
- 7:04Yeah.
- 7:05Yeah, yeah.
- 7:06Super cool.
- 7:07So, yeah.
- 7:08After I finished university, I joined the same company as Nick.
- 7:12I learned RPG and I learned Delphi.
- 7:15I learned Java.
- 7:16And then I achieved quite a senior role in the company.
- 7:21I was part of the architecture team.
- 7:24And I managed to introduce Scala to the company, which, yeah, we did a language comparison and it was between JVM languages.
- 7:32So, one of the languages was actually J Python or Jiton.
- 7:38We didn't go for that or JRuby because it wasn't first class language on the platform.
- 7:43So, really, the main contenders were Java, Scala and Clojure.
- 7:49And we had someone come in and talk to us about Scala, who was really, really amazing.
- 7:54And he convinced us to use it.
- 7:58So, yeah, I ended up leading the project to bring Scala into the company and re-architect a lot of the systems in that.
- 8:07When I left that company, I was very much down the functional programming route.
- 8:12So, I did some more Clojure, which is very functional.
- 8:16I did some Haskell.
- 8:18But then I joined Nick, Imperial College London.
- 8:22And that's where he said he got into the Python.
- 8:28That's where I got into it as well.
- 8:30So, I joined a reading group that was all about deep learning.
- 8:35And I went to the reading group and we read a paper, which was called Differential Neural Computer.
- 8:42And I had no understanding what it meant.
- 8:44And I was like, OK, I'm going to go home and I'm going to implement it.
- 8:48So, I went home and I cracked out some Python, cracked out TensorFlow.
- 8:53And it took me weeks and weeks to implement it because I had no idea what was going on.
- 8:59That was the start.
- 9:02So, I just started implementing more and more of these papers as I went to more and more reading groups.
- 9:06And it tended to be all of the Atari playing reinforcement learning algorithms, which is really fascinating to me.
- 9:16OK, cool.
- 9:17So, I learned how to play Breakout and learned how to play Pong.
- 9:20Right.
- 9:21Maybe some Pitfall in there.
- 9:23I never tried it on Pitfall, actually.
- 9:25That was a good one.
- 9:26Nice.
- 9:28I'm really fascinated with these ways to train AIs around video games.
- 9:33I mean, I'm blanking on one of the options.
- 9:36But there's a handful of libraries that you can kind of plug your AI into the virtual world.
- 9:42So, it has somewhere to interact with and things to interact with.
- 9:46And yeah, that's fascinating.
- 9:47Yeah.
- 9:48I really did find it fascinating.
- 9:50That's why I was doing it every week.
- 9:52And then just around that time, AlphaGo came out as well.
- 9:56And it was like a shock to the world.
- 9:59Suddenly, the hardest game that humanity can play is beaten by a computer.
- 10:05Yeah.
- 10:05And it's one of the first, it might have been the first real AI opponent that used like
- 10:12strategizing rather than just deep exploration of the paths.
- 10:17Right.
- 10:18Like the chess one is like, well, I can hold 12 steps ahead in every direction in my mind.
- 10:23That's more than the chess master.
- 10:26So, right.
- 10:26Like we'll just play them all out, all the possible futures out and then go down the best
- 10:31one.
- 10:31Right.
- 10:31But that's not how AlphaGo worked, which is, I think, part of the magic.
- 10:34Yeah, it's got intuition in a sense.
- 10:36That's the interesting bit.
- 10:37Yeah.
- 10:38Yeah.
- 10:38It was just fascinating the way they did that.
- 10:41I ended up writing a version of AlphaGo to play Kinect 4.
- 10:46Okay.
- 10:46Which is actually pretty strong.
- 10:49That was a really fun project as well.
- 10:52I mean, it can beat me.
- 10:53You're a very strong player.
- 10:56That's awesome.
- 10:56Yeah.
- 10:57I'm an average player out there.
- 10:59Yeah.
- 11:00Yeah.
- 11:00How cool.
- 11:00How cool.
- 11:01All right.
- 11:01So what are you both working on the same project yet again?
- 11:04What are you doing day to day?
- 11:05What are you doing now?
- 11:06We're both working on Sorcery full time.
- 11:08We kind of started working on it back at the end of 2018 from Brendan's flat.
- 11:12So I'd turn up and he'd be still in his pajamas eating cereal.
- 11:16Sit down and code away.
- 11:19Yeah.
- 11:20We're kind of totally focused on making Sorcery as good a refactoring tool as can be really.
- 11:26Yeah.
- 11:26Cool.
- 11:27We'll get into what Sorcery is more later, but what's the quick elevator pitch before
- 11:30we dive into just more general software stuff?
- 11:33I guess we've been pitching it as kind of Grammarly for code.
- 11:36And if you don't know what Grammarly is, it's like it improves the style of the code without
- 11:41changing the sort of meaning or the content.
- 11:43I guess that's what refactoring is.
- 11:44You improve this quality in the structure without changing the functionality.
- 11:48And that's what we aim to do.
- 11:50So as you're writing it, we analyze it and we suggest refactoring improvements sort of
- 11:55as you go.
- 11:55So maybe I got a loop and I'm doing some kind of accumulation into a list and it could say,
- 12:01you know what, that could just be a list comprehension.
- 12:03Yeah, exactly.
- 12:04Yeah.
- 12:04Okay.
- 12:05That sounds awesome.
- 12:06It's easy to just get focused on writing code and then not really worrying about the quality.
- 12:12It's also equally easy to get super obsessed with the quality and not just get the thing
- 12:19done.
- 12:19Right.
- 12:20So I kind of see like these two bimodal distributions.
- 12:23One's like, I don't give a crap.
- 12:24I'm not going wrong as it works.
- 12:25I'm just going to write, write, write.
- 12:26Yeah.
- 12:27And, you know, that's one group of people's philosophy.
- 12:30The other is they're like really slow and super meticulous to get it just right because
- 12:36they want to write it the best way.
- 12:38And it sounds to me like the tool would let people kind of find a middle ground, right?
- 12:44Like write it a little more loose and free.
- 12:47But then it says, oh, by the way, that thing you just wrote, actually, we could make that
- 12:50way better if you just let us.
- 12:51Yeah, that's kind of the idea.
- 12:52And that's some of the feedback we've been getting.
- 12:53That's people write codes a bit more quickly without worrying so much about the quality and
- 12:57it'll kind of tidy up for them.
- 12:59Okay, cool.
- 13:00And the other aspect of it is some people don't actually know what good quality code
- 13:07is.
- 13:07So if you're starting out in Python, you may be able to write the solution, but you won't
- 13:13necessarily know how to write it well.
- 13:15So you may not even know about this comprehensions yet.
- 13:19Right, right, right.
- 13:20And the benefit of using Sorcery in that case is it can teach you the Pythonic way of writing
- 13:29code.
- 13:29This portion of Talk Python To Me is brought to you by Datadog.
- 13:34Are you having trouble visualizing bottlenecks and latency in your apps and you're not sure
- 13:38where the issue is coming from or how to solve it?
- 13:40With Datadog's end-to-end monitoring platform, you can use their customizable built-in dashboard
- 13:46to collect metrics and visualize app performance in real time.
- 13:49Datadog automatically correlates logs and traces at the level of individual requests, allowing
- 13:55you to quickly troubleshoot your Python application.
- 13:57Plus, their service map automatically plots the flow of requests across your application
- 14:02architecture so you understand dependencies and can proactively monitor the performance
- 14:07of your apps.
- 14:08Be the hero that got your app back on track at your company.
- 14:12Get started today with a free trial at talkpython.fm/datadog.
- 14:17So maybe you don't necessarily know the idioms.
- 14:19I think one of the challenges of Python in particular, I mean, all languages have this problem, but
- 14:25Python suffers more than others from it.
- 14:28One is that it's so easy to learn that people feel like they can learn it in a weekend and
- 14:34then they just go write code in it, right?
- 14:36Because like, look, it's a simple little language.
- 14:38There's not a whole lot to it.
- 14:40Which I think is actually not true, right?
- 14:42I still feel like I'm learning Python every day.
- 14:44I'm like, I didn't know, or I should have done this, right?
- 14:48Like, there's all these things.
- 14:49There's just, there's so much nuanced detail to it.
- 14:52But it's easy for people to come from a language like C or Java or something else and just do
- 14:58the Java style programming there or the C style programming there.
- 15:03And not, like you said, not even know that there's this other component, right?
- 15:08They say, oh, Python's crappy.
- 15:10It doesn't have, you know, a four, a numerical for loop.
- 15:14This is crummy, right?
- 15:15When really a better way to do it would be to use enumerate collection where you get the
- 15:20index and the item, right?
- 15:22You don't have to go back.
- 15:22If you didn't know, like that's multiple layers.
- 15:25One, you have to know there's a four in loop.
- 15:26And then two, you have to know that enumerate is a thing.
- 15:29And then you've got to know about tuple and back.
- 15:30And like, that's a pretty complex set of topics.
- 15:34If you've, well, I spent the weekend now, I kind of know, let's, let's finish this project.
- 15:38Yeah.
- 15:38If you took that philosophy.
- 15:39Absolutely.
- 15:40I mean, like you said, the learning, the Python language, and it's so beautifully simple that
- 15:46most people can pick it up in a week to a month's time.
- 15:50But then you've got all of these items and they're not just the different bits of syntax
- 15:56or the enumerate.
- 15:58There's multiple libraries all over the place.
- 16:00And they're the libraries that are built into the Python main library.
- 16:05But there are also all of the libraries out there that you need to accomplish more complicated
- 16:11tasks.
- 16:11Yeah.
- 16:11Just finding out what the best library is for the job is a difficult piece of research often.
- 16:17It is.
- 16:17Sometimes it's something built in, like inner tools or something like that.
- 16:21Or other times it's something you've never heard of because there's 200,000 options and
- 16:26they all have their, their behaviors and whatnot.
- 16:28But if you would grab it like that would take 20 lines down to one, probably be faster at the
- 16:33same time.
- 16:34It's like, it's incredible.
- 16:35Right.
- 16:35That's kind of what I was thinking.
- 16:36I'm like, I've never done learning Python because like, oh, there's this other standard
- 16:40library module I discovered where I wasn't using counter in this way or, you know, like
- 16:45whatever it is, right?
- 16:45There's just all these options.
- 16:46Yeah.
- 16:46We've definitely come across that.
- 16:47Sorry.
- 16:48Come on.
- 16:48Yeah.
- 16:48Yeah.
- 16:49I can imagine.
- 16:50I can imagine.
- 16:50And coming from an academic space, I'm sure you see that a lot there as well.
- 16:54Cause there's probably a lot of people who don't see themselves as a developer, but they're
- 17:01still touching Python, writing code.
- 17:03It's full of code you kind of write once and then don't, don't look at again.
- 17:07So it's kind of, I guess done with a different aim in mind.
- 17:10You're not, they're not so worried about reuse by other people often.
- 17:13Right.
- 17:14Right.
- 17:14It's actually been quite a challenge for us because we learn Python together effectively
- 17:20at the same time.
- 17:22And we've never written Python in a large code base apart from our own.
- 17:29So we've had to learn all of these things ourselves.
- 17:33So over time, we've rewritten bits of the system where we found out, oh, we can use this,
- 17:39this feature of Python that makes it so much better.
- 17:43And, at one point we integrated mypy into our code base and that was a big improvement.
- 17:51And you say, it's slowly learn about these options.
- 17:53And I think a lot of people out there who are learning Python probably fall into that bracket
- 17:58as well.
- 17:58Then they're learning it as their first language.
- 18:01They're not learning it with people who can guide them how best to use it.
- 18:05And it's really hard on your own.
- 18:07I mean, we're experienced developers and I think we're finding it really hard ourselves.
- 18:12Yeah.
- 18:12To do it right and to take full advantage of it, it is.
- 18:15So it's cool to have like extensions for the IDE that will sort of not quite be a paired
- 18:20program partner, but someone to sort of sit there.
- 18:22But you know what, that actually is not the right way to do it, but it's super easy to fix
- 18:26and I can take care of that for you, right?
- 18:28Exactly.
- 18:28Yeah.
- 18:29That's exactly what we...
- 18:31That's awesome.
- 18:31So we talked about code quality and, you know, that's a little bit in the eye of the
- 18:37beholder.
- 18:37It's also a little bit in the trade-off.
- 18:38Like Nick, you talked about this concept of I'm going to write a script and get an answer
- 18:42and never run it again, right?
- 18:44That has a different threshold for code quality than, you know, the core trading engine at a bank,
- 18:51right?
- 18:52Like it would probably be improper to put that much energy into that script that's going
- 18:59to be run once, right?
- 19:00You should just write the thing and get it to work and not worry too much about it.
- 19:03But at the same time, if you're building something to be reused and is important, it's going
- 19:07to be run by lots of people.
- 19:08You really want to get it right.
- 19:10And so I think there's this spectrum and people got to like figure out where they live on it.
- 19:14But no matter where you live, you would like to have better code quality rather than less
- 19:19good code quality by like whatever applies to your situation.
- 19:22Right.
- 19:22Oh, definitely.
- 19:23And it is kind of hard to quantify what code quality is.
- 19:25Sometimes, you know, when you see it, I guess it was reading the book Clean Codes.
- 19:29I think it's by Bob Martin.
- 19:30Robert C. Martin.
- 19:30Yeah.
- 19:31Yeah.
- 19:31I really crystallized it for me.
- 19:32And I was always trying to get the graduates at the old company I worked at to read it.
- 19:36And eventually one of them stole it.
- 19:37So I think, I guess that means they liked it.
- 19:41You won.
- 19:41Yeah.
- 19:41It worked.
- 19:43You converted and they stole your book.
- 19:45I guess the real core of it is just high quality code is easy to read and understand.
- 19:50It just reads like a sort of story.
- 19:52Does this and this and this.
- 19:53Yeah.
- 19:53I really love this idea of clean code and the stuff that Bob Martin talks about.
- 19:57He's got some really good ideas.
- 19:59I don't totally agree with everything he says, but I think there's a lot of good lessons
- 20:02to take from what he's doing.
- 20:04Yeah.
- 20:04Sure.
- 20:05Yeah.
- 20:05One of the really interesting ideas that comes from one of his contemporaries, Martin Fowler,
- 20:10way back at the origins of refactoring.
- 20:13I remember reading the book called Refactoring in 1999 or something like that, just going,
- 20:20my mind is blown, right?
- 20:22I've had this problem of bad code quality and I've had this problem of trying to write it
- 20:27well or to fix it.
- 20:28And then I realized, you know, reading what he was talking about, like, oh, there's this
- 20:32way to take the bad stuff you've already put down as like sediment in the software.
- 20:38It's crystallized and like turn that into something that can be improved and grown over
- 20:42time.
- 20:42And I just, I remember it really changed my way of thinking about programming, like digging
- 20:47into refactoring.
- 20:48So I'm just such a huge fan.
- 20:49How do you guys come across it?
- 20:51It's an interesting one because the code base we worked on our old company was so huge and
- 20:56difficult to change.
- 20:57Often we didn't even try refactoring it.
- 20:59It's just kind of, you did a surgical approach.
- 21:01You went in and tried to understand it and made the smaller changes you could.
- 21:04Please don't break.
- 21:05Just take the new feature without breaking.
- 21:07Or I guess, Brendan, you actually sort of took the other approach and said like, okay, I'll
- 21:12just rewrite this whole bit.
- 21:13Yeah.
- 21:15Well, I mean, it was a risky approach given that the system was not under test at all.
- 21:21So I would only do that on front end components.
- 21:25So the UI, but yeah, sometimes it was, it was just the case of, I don't understand what's
- 21:31going on here.
- 21:32I can see what the functionality currently is.
- 21:34So I'm just going to re-implement it from scratch.
- 21:37You know, if people are in that space, like the whole area of what Michael Feathers talked
- 21:42about with legacy code and like how to take these, these huge systems that are hard to
- 21:48change that you don't necessarily know.
- 21:49They don't have tests and how to like break off little bits that are maintainable.
- 21:53That's such a cool book working effectively with legacy code.
- 21:56I really do enjoy sort of tinkering with code, tracking down bugs, sort of improving it, making
- 22:01it a little better.
- 22:01I think when I see a blank sheet of paper or blank screen, I kind of sometimes find it difficult
- 22:06to start.
- 22:06I think Brendan's a bit better at that.
- 22:08So I, yeah, I super enjoy refactoring.
- 22:10And I guess it's one of the things we're trying to kind of achieve with Sorcery is sort of if
- 22:16there's a machine that can do the refactoring for you, you can be less worried about it
- 22:20being under test because, you know, it's done proper analysis.
- 22:23Because whenever I do refactoring, I break something.
- 22:26And I would lean heavily on the tests.
- 22:29It's almost never seems like a good idea to me to do a refactoring manually.
- 22:33If there's some sort of like tool-based way in which it will happen, right?
- 22:39It's just, you never know what little thing you're going to, you're like, oh, there's that
- 22:42one, that cron job thing that we had that was also using that.
- 22:46Now, apparently it's not going to take it anymore.
- 22:48And with, with Python, you don't have compiling, right?
- 22:50So you're not going to catch the obvious stuff.
- 22:52Like I move this function over here.
- 22:53It's just like, well, don't run that part.
- 22:55It's going to crash.
- 22:56That is one of the challenges with Python.
- 22:58And I mean, the way we've approached it with our Sorcery code base is testing and
- 23:07mypy type annotations.
- 23:09Yeah.
- 23:09I think they give enormous confidence when you're refactoring the code.
- 23:13So nowadays I don't do crazy refactors, crazy rewrites like that.
- 23:18I incrementally improve through small changes.
- 23:23But yeah, I've experimented with that once and realized it's not the way to go.
- 23:28But yeah, the real key is having those tests and the type annotations.
- 23:35You can move something anywhere in the system and you'll get told about all of the errors
- 23:41that you now have.
- 23:42And then you can go and fix all of those.
- 23:44And then you can do the next refactoring and build it up through there.
- 23:49Most of our bugs are in the bits we hadn't added mypy to.
- 23:53So I totally expected to hear automation from you guys and applying Sorcery back unto itself
- 24:00and things like this.
- 24:01And we'll dig into the features in a second.
- 24:03But I didn't expect to hear typing in mypy.
- 24:06I'm personally a huge fan of the type annotations in Python.
- 24:09I think they make working with Python code so much easier.
- 24:13You don't annotate everything, but certain places like this function returns one of these,
- 24:20just knowing like actually it expects to return one of these.
- 24:22It's super helpful and it'll light up the editors as well, right?
- 24:26They can all of a sudden give you autocomplete for what they weren't sure before.
- 24:29But now they know, oh, here's the five things you can do with what you got back.
- 24:32Perfect.
- 24:33How does mypy fit into your world?
- 24:36I think that's pretty interesting.
- 24:37What are you doing with that?
- 24:38We really only use it internally because most code out there doesn't actually have mypy
- 24:43or type annotations.
- 24:45Right.
- 24:45Even if it has some type annotations, there's like sort of a chain of annotations that have
- 24:51to be consistent.
- 24:52Like mypy is a stronger level than just saying, oh, this function happens to return a list.
- 24:56Yeah.
- 24:56I mean, one of the great things about type annotations is you can just look at a function
- 25:02at the definition of it and understand the interface.
- 25:06You don't need to read the code.
- 25:08Without those type annotations, you have to actually read the code and say, oh, actually
- 25:13this is a string and this is an integer and it returns a list of integers or something
- 25:19like that.
- 25:19Yeah.
- 25:20With the type annotations.
- 25:21Yeah.
- 25:21That's a really good point.
- 25:22I like that.
- 25:23So I think type annotations are a form of documentation.
- 25:27They're really, really powerful just from a readability point of view.
- 25:32But then you get all of the security as well of knowing when you've broken the code or when
- 25:37you've not called something correctly.
- 25:39One of the things that is really important to us with Sorcery is that we never break other
- 25:45people's code.
- 25:46So we have to have extremely strong gar...
- 25:50Yeah.
- 25:50So let's take a step back and why don't you tell people about what Sorcery is?
- 25:54How do people use it?
- 25:55So you mentioned that it's a plugin for IDEs, but give us a little bit more detail and then
- 25:59we can talk about how you keep from breaking people's code, which is probably...
- 26:03People probably appreciate that.
- 26:04If you...
- 26:06It acts as a plugin to your IDE.
- 26:09So we've got plugins for VS Code and PyCharm.
- 26:13And as you're coding away, it sits there reading your code and analyzing it.
- 26:18And if it identifies a change to a function that you're working on that will improve the
- 26:26code quality, it'll suggest it to you.
- 26:28And you can review that suggestion.
- 26:30And if you like it, you can accept it.
- 26:33And it'll apply that change in line.
- 26:35And you carry on coding it.
- 26:39It works almost seamlessly in your workflow.
- 26:41Okay.
- 26:42That sounds awesome.
- 26:43Does it change the way the editors work in other ways?
- 26:46Like, for example, does it change the autocomplete or things like that?
- 26:51Or is it really more like the code intentions, like the little light bulb in PyCharm?
- 26:56Yeah, it's kind of exactly like a code intention in PyCharm.
- 26:58That's kind of the thing we've gone with.
- 27:00Yeah.
- 27:01So yeah, it does a little underline.
- 27:02Cool.
- 27:02So as you're going along, you're watching...
- 27:04Oh, there's a little pop-up.
- 27:05I should go see what this is about.
- 27:07Yeah, yeah, exactly.
- 27:08And it runs locally on your machine.
- 27:09I guess that was quite a sort of concern for people.
- 27:11They didn't want their code being sent to the cloud.
- 27:13I can't imagine why.
- 27:14So when we first started it, we were going to do it kind of as a service in the cloud.
- 27:20And we kind of had to do a pivot and get it running locally on the machine.
- 27:23Okay.
- 27:24So somehow behind this, how does it make decisions about refactorings?
- 27:28Is it like an AI-based thing?
- 27:32Is it pattern matching?
- 27:32Like, what is it doing inside?
- 27:34So at the moment, there's no machine learning or AI in it.
- 27:39The way it works is it is essentially pattern matching.
- 27:43So it's looking for...
- 27:44Well, it works at the level of the abstract syntax tree.
- 27:48So it takes the code and it parses it into a data structure.
- 27:53And that data structure will have, for instance, an if node.
- 27:58And within that if node, it will have a function call.
- 28:03And the if node will also have a list of statements.
- 28:07It looks at those nodes and it looks for the patterns, like you say.
- 28:12So, for instance, it might look for a for loop that has an if statement within it that appends to a list.
- 28:20And then it says, okay.
- 28:22Like, that's a list comprehension waiting to be made right there.
- 28:25Yeah.
- 28:26Exactly.
- 28:26And I guess the clever bit is it kind of has these little, lots of little tiny little patterns of improvements it can do.
- 28:33But it can compose those together into like a bigger refactoring.
- 28:36And it's guided by a load of code metrics we've kind of incorporated into it.
- 28:40So we can get into those later.
- 28:41Like cyclomatic complexity and some of those types of things?
- 28:45Yeah.
- 28:45Yeah.
- 28:46So we don't use cyclomatic.
- 28:48We do use cognitive complexity, which I think is like a trademark of SonarCube or something.
- 28:52But it's a different metric.
- 28:54And we use a few we've written ourselves.
- 28:56And so it can kind of chain together little refactorings to do something bigger.
- 29:00So, for example, on the, I don't know if you've seen the Gilded Rose refactoring CARTA.
- 29:06No, tell people about it.
- 29:07So it's kind of this big fantasy.
- 29:11Wait, let's take a step back.
- 29:12What's a CARTA?
- 29:13So a coding CARTA is a coding exercise to improve your programming.
- 29:17Right, right.
- 29:18And there are various ones of these floating around the internet.
- 29:20And Gilded Rose is like this big, complicated set of nested ifs, basically.
- 29:24And it's sort of about this fantasy in, I think.
- 29:28And it takes maybe an hour to kind of manually sort the code out and refactor it.
- 29:33And it's sort of an exercise people do.
- 29:34And our aim when we started Sorcery was to, this was like our initial target problem.
- 29:40So it can kind of do all that work at once by chaining together lots of little refactorings.
- 29:45So it can take the sort of complex mess of spaghetti code and then turn it into something understandable.
- 29:51Right.
- 29:52Instead of having to say, okay, here's a little if statement that could be improved and then apply it again and say, well, now that we have this code, there's another thing we can improve than apply it again.
- 30:02It'll like chain those all together and go, actually, we could roll this all up.
- 30:05Exactly.
- 30:06Yeah.
- 30:06Because when you're doing manual refactoring, that's kind of what often happens.
- 30:08You sort of do a little thing and then you realize, oh, now I can do this.
- 30:11And you might have an aim in mind or you might not.
- 30:14And then you start chaining these things together.
- 30:16And in the end, you're like, oh, now it's an understandable code base.
- 30:19Yeah.
- 30:19Yeah.
- 30:20Very cool.
- 30:21Well, that sounds super, super useful.
- 30:23I know that some refactorings build into certain tools like PyCharm has certain refactorings, but they don't seem to take this more holistic approach.
- 30:31Right.
- 30:32They're like, oh, this list, this list comprehension could be expanded to a for loop if you need it or something like that.
- 30:37But that's kind of as far as it goes.
- 30:38Yeah.
- 30:39And they're kind of very developer driven.
- 30:40You have to know you want to do them.
- 30:41Yeah.
- 30:42And you have to know where you can do them and then you have to do them.
- 30:44So they're very useful if you know you want to do something because it'll do it for you.
- 30:47And like, it won't make mistakes, but they don't sort of, they don't tell you if it's a good idea or not.
- 30:52So our idea is we're kind of suggesting things that we think are good ideas to actually change.
- 30:58This portion of Talk Python To Me is brought to you by Linode.
- 31:01Whether you're working on a personal project or managing your enterprise's infrastructure, Linode has the pricing, support, and scale that you need to take your project to the next level.
- 31:10With 11 data centers worldwide, including their newest data center in Sydney, Australia, enterprise grade hardware, S3 compatible storage, and the next generation network.
- 31:21Linode delivers the performance that you expect at a price that you don't.
- 31:25Get started on Linode today with a $20 credit and you get access to native SSD storage, a 40 gigabit network, industry-leading processors, their revamped cloud manager at cloud.linode.com, root access to your server, along with their newest API and a Python CLI.
- 31:41Just visit talkpython.fm/Linode when creating a new Linode account and you'll automatically get $20 credit for your next project.
- 31:50Oh, and one last thing.
- 31:50They're hiring.
- 31:51Go to linode.com slash careers to find out more.
- 31:54Let them know that we sent you.
- 31:56Let me share one of my favorite concepts from refactoring and then ask you about some of your favorite refactorings.
- 32:02So there's all these different refactorings, even in the early days that Mark Fowler talked about, like, okay, there's a God object and here's how you break it down.
- 32:10Or there's a function that's too long.
- 32:12Here's what you do and so on.
- 32:13But those are all kind of interesting.
- 32:15Like, the most interesting concept around all that stuff to me was the concept of a code smell.
- 32:20Right?
- 32:20Just like, there's something wrong with this.
- 32:24Like, it works, but your nose kind of turns up when you look at it.
- 32:28You're like, there's something wrong with this part of my code or code probably I inherited from somebody else.
- 32:33Right?
- 32:34And the other thing was he would talk about comments and say, often comments have value, but a lot of times they're really just deodorant for these code smells.
- 32:45Like, this is really hard to understand because it's written badly.
- 32:47So let me write a comment that tells people what it really means.
- 32:51Yeah.
- 32:51And then just leave the bad stuff there.
- 32:53Right?
- 32:53It kind of deodorizes the code smell a little bit.
- 32:55And that's this idea of, like, if you have those comments, it's like this underlying thing of, like, you should start thinking of applying these different refactorings.
- 33:03So my question to you with, like, sort of putting that out there is, what are some of the favorite refactorings that you guys are seeing possible with, like, this deeper integration, right?
- 33:14Like, obviously, for loop to Liz comprehension, Liz comprehension to for loop.
- 33:18Like, those are pretty straightforward, but it sounds like there might be either things you just really love or there might be, like, some more interesting, larger refactorings.
- 33:26The main code smell that I think Sorcery does really well with is eliminating duplicate code within a function.
- 33:34And in particular, within different branches of a complicated set of if expressions.
- 33:41So you may have the same body of code in two different places.
- 33:46And Sorcery can restructure the code until there's just a single condition that applies for that block of code.
- 33:54That's cool.
- 33:54Yeah, I think that lets me delete lines of code is always very pleasing.
- 33:57So, yeah.
- 33:58Yeah, yeah.
- 33:59And delete conditionals as well, right, if possible, or simplify them.
- 34:03You know, I feel like the more the word legacy gets applied to a code base, the less you want to do those kinds of things.
- 34:08You're like, I'm pretty sure these three things are the same, but I don't want to be responsible for what happens if I misunderstand that these are actually slightly different.
- 34:17And try to refactor it to a cleaner version.
- 34:19So as much as you can get software, does you go, actually, no, this is totally safe.
- 34:22We got you.
- 34:23It's actually quite interesting because one of the things that has happened is we've suggested refactorings to people and they've gone, this is incorrect.
- 34:35And there's been these examples of eliminating duplicate code and simplifying if expression.
- 34:42It's kind of once you work through the logic.
- 34:43Yeah.
- 34:44We've discovered actually sorcery is correct here.
- 34:49And it turns out your code was either very confusing or possibly had a bug in it, which you have now identified.
- 34:56Right, right.
- 34:57You thought these two things were doing different stuff, but in fact, it has no...
- 35:00The effect of this is not what you had in your mind its actual effect being, right?
- 35:05Yeah.
- 35:05You misunderstood what was actually happening and your mental model didn't match the refactoring result, but that's because it wasn't actually doing that.
- 35:13Exactly.
- 35:14And so actually, once it's done that refactoring, you can say, oh, actually, there is a bug in my code.
- 35:20I can fix it.
- 35:22But you have to have the trust in sorcery to know that it's correct before you're willing to take that step.
- 35:30So it takes a little bit of usage to build up that trust.
- 35:33So how do you guys check that your refactorings are valid?
- 35:37So like we said, we have a library of smaller refactorings, and then we have a search engine that composes those together.
- 35:47So the important thing is making sure each of those individual refactorings in the library is correct.
- 35:53Right, right.
- 35:53Because composing a bunch of things that are correct is not going to break anything.
- 35:56Exactly.
- 35:57Yeah.
- 35:58So the challenge is to try and make sure that those individual ones are correct.
- 36:04So we have lots and lots of tests, and those tests are of the form.
- 36:09Here is a piece of source code, and here is the expected refactored source code.
- 36:15And for each refactoring, there's a multitude of those.
- 36:18But there's also a multitude of tests of the form.
- 36:22Here's a piece of source code that looks similar to these other bits that you have refactored,
- 36:27but you shouldn't refactor it because if you do refactor it, or if you do make the change, you'll break the code.
- 36:34There's got some gotcha in there, yeah.
- 36:37It's not a true refactoring.
- 36:38And it will tend to be things like you're calling a function, and so you can't swap these statements
- 36:45because one of them is actually a global variable.
- 36:48So we have an awful lot of analysis, which determines what statements in a function depend on the other statements.
- 36:57Yeah, it sounds interesting.
- 36:58It turns out to be the hardest problem that we've tried to solve.
- 37:01I can imagine.
- 37:02Have you guys looked at using things like hypothesis or other property-based testing,
- 37:06where it's like, here's a block of code, apply refactoring to it,
- 37:11feed it a bunch of inputs to both versions, and see if as long as you get the same outputs,
- 37:16or things like that?
- 37:17That's a future plan that we have.
- 37:20We do have a second form of testing that we do at the moment,
- 37:23which is, as part of our build process,
- 37:26we run Sorcery over a whole bunch of popular open source libraries and refactor them.
- 37:33And then once they've been refactored, we run their tests over themselves
- 37:39to check that we haven't broken any of their code.
- 37:42Because people have already written a ton of tests for SQLAlchemy, for requests or whatever, right?
- 37:48Exactly.
- 37:49So, I mean, that has identified, I mean, when we first introduced it, identified lots of issues.
- 37:55And since then, it's stopped us releasing any new bugs as far as we're aware.
- 38:02That sounds like a pretty good way to just hit it with enough information that it's going to get caught in the issues.
- 38:07Have you found errors in other libraries because of this and gotten back to them?
- 38:11Like, you know what?
- 38:12This is actually, we thought our stuff was broken, but actually your stuff is broken.
- 38:17I mean, that'd be cool.
- 38:19I mean, sometimes the tests are failing in master.
- 38:22So after a while, we just decided to pick a tag and stick with it and be done with it.
- 38:28But actually, we found that there's just a lot of backcode to review.
- 38:33So we tend not to review it at the moment.
- 38:37Yeah, it's not your job to check all open source libraries for correctness, right?
- 38:42Exactly.
- 38:43And some of the libraries like SQLAlchemy that we do run it on are absolutely enormous.
- 38:48There's hundreds of files and multiple drivers for different database backends.
- 38:54So it takes a long time to run those tests.
- 38:57Yeah.
- 38:57Yeah.
- 38:58We just rely on the test to tell us whether we're good to release or not.
- 39:01Yeah, that's cool.
- 39:02But yeah, the hypothesis-based testing is a very interesting idea that we have talked about.
- 39:09So the way we were considering doing it was exactly how you talked about.
- 39:14You write a piece of code and then you put some inputs and you check what the output is.
- 39:21And then you run Sorcery over it.
- 39:23And the way we were going to do it is actually also write a generator for source code that takes
- 39:29maybe an initial piece of code and does random mutations to it to start off with.
- 39:35So here's a piece of code that Sorcery should refactor.
- 39:38Let's apply a bunch of random mutations to it and then run Sorcery over it.
- 39:46Check the inputs and outputs are the same again.
- 39:48So yeah, the generator for that would have been quite interesting to write.
- 39:53And it is something that we're considering in the future.
- 39:56Okay.
- 39:56Yeah, it sounds cool.
- 39:57It sounds like it would take forever to run, but it sounds like a cool project.
- 40:00Maybe don't run it on every save.
- 40:03All right.
- 40:04Well, let's see.
- 40:06There's a bunch of things I want to ask you about, but I don't want to go over too much
- 40:10on time.
- 40:10So I guess one area that looks interesting to me is we've talked about this being a plugin
- 40:16for an editor that's interactive.
- 40:17You also talk about just applying it to open source libraries.
- 40:21And on the Sorcery homepage, I see that there's a get instant quality of your Python code base,
- 40:28like just point it at your repo and it'll give you some answers.
- 40:31So is there more stuff that it does than just be a plugin?
- 40:34Yeah.
- 40:35Or is there like a CLI way to use it?
- 40:38So it is also available as a GitHub bot.
- 40:41So you install the Sorcery bot into your GitHub repo.
- 40:48And every time you do a pull request, Sorcery will review that pull request.
- 40:53And if it finds any improvements to any of the files that have been touched by the pull request,
- 41:00then it will create a pull request on top of that saying, here's the changes that you can make.
- 41:05An improvement to it.
- 41:07Yeah.
- 41:07Then you can just merge that pull request in straight away.
- 41:10When you first install it, also it can refactor the whole library kind of all at once.
- 41:15Very cool.
- 41:16All right.
- 41:17So let's talk about pricing.
- 41:19So this is something that is free for some people, but it's not free for some other people.
- 41:24What's the story with the whole business model, open source side of things?
- 41:28Like what is, what are you guys offering here?
- 41:30Because it sounds really useful to a lot of people, but at the same time, you are charging some folks for it.
- 41:36So that might, you know, that might influence people's opinion on how they feel about it.
- 41:40Yeah.
- 41:40So the plugins are free at the moment.
- 41:43We think in the future, we'll probably introduce a premium version and still have a free version.
- 41:46I see.
- 41:47So if I'm sitting here and I want to write code on my MacBook on PyCharm, I can just go get it for free.
- 41:52I don't have to pay anything.
- 41:53Yeah.
- 41:53You can just go get it for free right now and not pay anything.
- 41:55And that's whether you're open source or closed source or anything.
- 41:58I could be working for a bank even, huh?
- 41:59You could.
- 42:00We have had users from banks working on it or using it.
- 42:03So yeah.
- 42:04Right on.
- 42:04But there is some business model where you guys charge money for something.
- 42:08So what are you, what is that side of the things?
- 42:10Yes.
- 42:10So for the code review, it's free for open source again.
- 42:13But if you want to use it on a closed source repository, there's a small charge per developer
- 42:18per month, basically.
- 42:19And that's something we've only just released in the last few days, basically.
- 42:23All right.
- 42:24Cool.
- 42:24So basically, if I'm going to apply it to my code base as a autonomous bot type of thing
- 42:30and I'm open source, it's 100% free.
- 42:33Right.
- 42:33So if I was taking care of requests or SQLAlchemy or Flask or whatever, I could just plug it
- 42:38into the Flask repo on GitHub.
- 42:40And all of a sudden it would solve those problems of like, you didn't seriously just give me
- 42:45a four loop that depends to a list, did you?
- 42:46Right.
- 42:48Yeah, exactly.
- 42:49So yeah.
- 42:50Particularly useful for people maintaining large open source libraries because they'll get a
- 42:57lot of pull requests and they may come in at various standards.
- 43:00So it does the initial code review for the maintainer of the project.
- 43:05There's another way of trying it out.
- 43:07If you have a GitHub account and that is simply to star our public repo and the GitHub bot,
- 43:16our GitHub bot will find your most popular Python repository and send you a pull request
- 43:23to refactor your code base.
- 43:26So it's as simple as click a star and you'll get a pull request.
- 43:30Oh wow.
- 43:30Okay, cool.
- 43:31Yeah.
- 43:32I mean, it seems to me really useful to have it just built into GitHub automatically looking
- 43:38over the code because I don't know.
- 43:40You all have worked with different groups of people at different companies, different languages.
- 43:43My experience has been that people that care about code quality and refactoring and testing
- 43:49and maintainability and patterns and all that kind of stuff.
- 43:53There's a massive spectrum on any given team.
- 43:57Some people, it really matters to them and others.
- 44:01Those failing tests and that failing build is just a nuisance.
- 44:04And how do I turn off the build so I don't have to hear about it again?
- 44:07Right?
- 44:07And so having it as part of the repo means that I get kind of applies to everyone.
- 44:15At least it suggests for everyone.
- 44:17Whereas if it's just in the editor, there's going to be the people who love it and the people who just
- 44:22like, how do I uninstall this or disable this?
- 44:24So it doesn't, because it's just, I wrote my code and I don't want it to, you know what I mean?
- 44:28Like there's just, it doesn't matter how much advocacy there is.
- 44:31There's going to be that.
- 44:31And so having that kind of external is pretty cool.
- 44:33Yeah.
- 44:34Like we started doing in the editor because we thought that was kind of the way to really
- 44:37make you write code faster and kind of you hack it a bit and it straight away does the change.
- 44:42But like you're saying, definitely that's why we introduced the GitHub plugin because the
- 44:47code review, because not everyone's at the same level.
- 44:51It kind of brings things up to a level.
- 44:53Yeah.
- 44:53So it's got, it gives you the benefit as a beginner programmer in the team.
- 44:57So you get those code reviews, but also as the experienced developer, it saves you time
- 45:03doing the code review because there's already a tool doing the simple steps.
- 45:08It's not dealing with the architectural elements of it, but it's making sure each function is
- 45:13nicely written.
- 45:14Yeah.
- 45:14I think that makes a lot of sense because it doesn't matter how good you are.
- 45:18You don't want to have to go think of the implications throughout the whole code base.
- 45:21Absolutely.
- 45:22And we're just looking to say the tool set is good.
- 45:24So it's good.
- 45:26Press merge.
- 45:27Yeah.
- 45:28Beautiful.
- 45:28But I do think it's really important that it's an editor because it teaches you, maybe it
- 45:33could teach you.
- 45:33It teaches you the idiomatic, the Pythonic ways of writing things.
- 45:36You're like, I had no idea that I could create a for loop with enum that had tuple unpacking
- 45:41instead of like trying to do a for over range and then pulling out the item and things like
- 45:46that.
- 45:46So it seems like a really cool combination.
- 45:49Yeah.
- 45:49I think definitely that educational thing is something we want to focus on more, like improving
- 45:52our documentation.
- 45:53So I wrote a blog post recently with a few little refactorings.
- 45:56We're doing like why we think they're a good idea as opposed to just what we've done.
- 45:59Is it the one that is called Python refactorings part one?
- 46:02Yeah.
- 46:02Yeah.
- 46:02Yeah.
- 46:02I looked through that.
- 46:03So maybe you could give us a couple of the refactorings out of there that you like.
- 46:07Code hoisting is like one of the best things because anytime you've got duplicate code,
- 46:11you've got a way you can introduce mistakes really easily.
- 46:14So that would be like maybe you have the same code in an if and an else statement.
- 46:18Yes.
- 46:18And it's just duplicated.
- 46:19So often people will write sort of a bit at the end of the same thing in the if and the
- 46:24else or in loads of elifs even maybe because it happens in every branch.
- 46:28It means it always happens.
- 46:29So it doesn't actually need to be in the condition at all.
- 46:31And if you take it out.
- 46:32Exactly.
- 46:33Just put it at the end.
- 46:34It also becomes kind of more clear what the conditional is doing, what it's controlling,
- 46:37because it hasn't got this extraneous thing in it.
- 46:39Another one you have in there is converting from a for loop, which does a yield to a yield
- 46:45from that collection directly, which is pretty nice.
- 46:48I mean, it might even apply to code that was written long ago before yield from was introduced
- 46:52to the language, but yield was there.
- 46:54And you could say, hey, look.
- 46:55Yeah, for sure.
- 46:56Nightgash, you've got quite a few comments.
- 46:57This old way could be gone.
- 46:58Quite a few comments that they didn't realize you could do that.
- 47:00So it's like a lot of people aren't reading every kind of PEP.
- 47:03What?
- 47:04Really?
- 47:05And seeing everything they can do.
- 47:07Strangely, it may seem to us.
- 47:10I know.
- 47:11It's so bizarre.
- 47:11I think I'm sure there's peps that I don't read as well.
- 47:13You know, if I had to pick a single most favorite absolute love it refactoring, it has to be
- 47:21convert like a deeply nested set of code to something with guarding clauses.
- 47:28So it's like flat, right?
- 47:30Instead of going, if this is true, then if this is true, while this is true, if this is true,
- 47:36and you end up like writing, starting on column 40 to write your code, if you negate them all
- 47:42and like return early or break out early or something, it's just so much cleaner.
- 47:46Yeah, so avoiding nesting is...
- 47:47Okay, that case is out.
- 47:48That case is out.
- 47:49That case is out.
- 47:50Now I focus on the essence.
- 47:51Avoiding nesting is one of our course kind of code metrics.
- 47:54Some of the other things I think we didn't touch on is how you get the computer to realize
- 47:58that there's a code smell.
- 47:59It's like writing good code metrics is quite...
- 48:02How do you get a computer to know?
- 48:03It's quite difficult.
- 48:04So there's these metrics like cyclomatic complexity, which...
- 48:09What's that about?
- 48:10It's about avoiding conditionals, basically.
- 48:13Number of decisions.
- 48:14Yeah.
- 48:15How many branches would you potentially go down, right?
- 48:18There's kind of this enhanced version of it we've looked at called cognitive complexity,
- 48:22which is trying to get to an idea of how hard something is to hold in your head.
- 48:26And that really penalizes nesting.
- 48:28How many variables are at play?
- 48:29How many other things like that as well, right?
- 48:31Probably.
- 48:32Yeah.
- 48:32So like that sort of penalizes nesting most of all.
- 48:35So that's kind of like how sorcery knows not to...
- 48:38Like nesting is a bad idea.
- 48:41And then we've written metrics about...
- 48:44Oh, I never really had thought about it that way.
- 48:46But that's exactly the problem is like the reason it sucks so much is like that next test
- 48:50is piled on as a and, and, and, and, and this.
- 48:54And that all the stuff that you've nested yourself into, you've got to think of like all
- 48:57those at the same time while I'm in here.
- 48:59Yeah.
- 48:59Cause the number of things you have to hold in your head, you know, a human can only hold
- 49:02six or seven things in their head at once.
- 49:04Yeah.
- 49:05Maybe if you're exceptional, you can do eight.
- 49:06So like some of our metrics is sort of focusing on how many variables you have to be thinking
- 49:12of and how many conditionals you have to be thinking of when you're sort of halfway
- 49:15down the function and it's gone off to the right somewhere.
- 49:17So we actually call that the working memory metric.
- 49:20Yeah.
- 49:20Oh, cool.
- 49:21That specifically measures the number of variables that are in scope at the current
- 49:26time.
- 49:26So we think you have, if you're reading the code from top to bottom, by the time you've
- 49:32got to the 10th line of code, if you've got seven variables in your head, then you don't
- 49:37understand the, you don't understand the function anymore.
- 49:40You can't keep all that in your head and understand the next page.
- 49:44So we keep having to scroll back and forth instead of just reading it.
- 49:48Yeah.
- 49:48Yeah.
- 49:49Yeah.
- 49:49There's this really interesting saying from a friend of mine that talked about, it went
- 49:54something like when you write code, I guess debugging code is harder than writing code.
- 50:01So if you write code at the very limit of what you're kind of able to write and do and like
- 50:07the most complicated stuff you can do, you probably can't debug it because trying to think through
- 50:11it actually is like a more complex than you kind of just pushed it over your limit.
- 50:15And so there's anytime you can kind of dial that back a bit through refactorings or other
- 50:20stuff, like, you know what, that should be three functions.
- 50:22Then you won't have to think about so hard.
- 50:24Sure.
- 50:24And like the most three things in this part, so much about it.
- 50:26The most interesting figure we found in a scientific paper that analyzed developers was sort of,
- 50:31they spend 70% or we spend 70% of our time trying to understand the codes and only 5% of the
- 50:37code time actually typing.
- 50:38So it's that 70% of the time you really need to cut down on by making it more readable and
- 50:43refactoring.
- 50:44And you have the, that's kind of the whole Zen of Python, right?
- 50:47I think that's why it's a popular language is because it's like clean to read presents itself
- 50:52well.
- 50:53Right.
- 50:53So don't undo that by writing bad code, I guess.
- 50:56Definitely.
- 50:57Yeah.
- 50:58All right.
- 50:59Well, I think this is probably a good place to leave it.
- 51:02You guys, it looks like a really cool project.
- 51:04If people are using PyCharm or they're using VS Code, they could just go get the plugin and
- 51:09give it a try.
- 51:09Right.
- 51:09Yeah, for sure.
- 51:10Just search for sorcery in the marketplace of the ID.
- 51:14Yeah.
- 51:14Okay.
- 51:15So you get it like as a, you go to the plugin marketplace in PyCharm or you do the extensions
- 51:19in VS Code and it'll just be in there.
- 51:21Sorcery with a U.
- 51:22As in computer source.
- 51:24Yeah.
- 51:24Not as in Gandalf.
- 51:25Yeah.
- 51:26Yeah.
- 51:28Yeah.
- 51:28And also if you go to our website, it has full instructions for installing both the plugins
- 51:34and using it on GitHub.
- 51:36And it also has.
- 51:38Right.
- 51:39And people have open source GitHub repo, they should just drop it in there and it'll give
- 51:42them some ideas, huh?
- 51:43Yeah.
- 51:43Give it a try.
- 51:44Absolutely.
- 51:44And links to our documentation as well.
- 51:48Very cool.
- 51:48All right.
- 51:49Now, before I let you out of here, I've got the two questions I always ask at the end of
- 51:53the show.
- 51:53So we'll just be quick since there's two of you.
- 51:55Brendan, how about you go first?
- 51:56You're going to write some Python code.
- 51:57What editor do you use?
- 51:58I use Vim nowadays.
- 52:00I ended up with wrist injuries from refactoring code using control and shift and the arrow keys
- 52:08too much.
- 52:09So I decided to learn Vim.
- 52:10I've ended up with that as well a long time ago and had to like rejuggle a lot of interesting
- 52:16stuff.
- 52:16Have like funky, curvy keyboards and all sorts of stuff.
- 52:20And yeah.
- 52:21Try to use hotkeys rather than mouse a lot.
- 52:23Yeah.
- 52:23It was turning into a real issue.
- 52:25So I had to learn Vim, which slowed me down by about 10 times for 10 weeks.
- 52:31But now I feel as though it's magic under my fingertips.
- 52:35That's awesome.
- 52:36Nick, how about you?
- 52:37Write Python code?
- 52:38I use PyCharm at the moment.
- 52:39So I'm a bit visually impaired and the high contrast mode is just really, really good.
- 52:43Dabble with VS Code a bit.
- 52:45I really like how it starts up super quick, but it's a little difficult to see.
- 52:49So I've made the switch.
- 52:50I can imagine that'll definitely push you over the edge.
- 52:52All right.
- 52:52Then notable PyPI package, maybe not something that everyone necessarily knows, but is like,
- 52:57oh, cool.
- 52:58I found this the other day and you should check it out.
- 53:00Any ideas, recommendations?
- 53:01Amazing one that we've used is this package called NUTKA, which is spelled N-U-I-T-K-A.
- 53:10And it takes your Python code, cross-compiles it into C, and then compiles the C code and creates an executable.
- 53:19And without that package, Sorcery just wouldn't exist as a locally running project because you'd have all sorts of deploy issues.
- 53:32We'd just be delivering all of our source codes with the plugins and the extensions.
- 53:36Interesting.
- 53:37So you're packaging it up.
- 53:38You're packing up Sorcery with NUTKA, huh?
- 53:40Yeah.
- 53:40Exactly.
- 53:40Yeah.
- 53:41How interesting.
- 53:43Okay.
- 53:43Yeah.
- 53:44It's fantastic.
- 53:45It builds in the version of Python that you're using and it reads all the imports to work out which bits of the code it needs to compile.
- 53:54It compiles the whole thing down and it works on Mac Windows and Linux.
- 53:59That's awesome.
- 53:59It's magnificent.
- 54:01Very cool.
- 54:01I had Kay Hayen from the NU-U-K-A project on for episode 174, which is like a year and a half,
- 54:09two years ago.
- 54:10I don't know.
- 54:11Quite a long while ago.
- 54:12But yeah, that's super cool.
- 54:14I didn't realize that it was so flexible in packaging up apps.
- 54:17I thought of more as like Cython, like this little bit we can make faster.
- 54:20So that's good to hear.
- 54:22Very nice.
- 54:23All right.
- 54:23Final call to action.
- 54:24People are interested in sorcery.
- 54:26They're interested in refactoring.
- 54:27What do you tell them?
- 54:28Try it out now.
- 54:29If you have a GitHub account, you can star our repo and try it out in five seconds.
- 54:36Or you can install it and get all of your pull requests refactored.
- 54:43If you're using VS Code or PyCharm, go and install it right now.
- 54:47Try it out.
- 54:47Get your code refactored as you work.
- 54:49And let us know.
- 54:50I mean, we're really keen to get feedback from people and keep on making it better and better,
- 54:53basically.
- 54:54Awesome.
- 54:54Do you guys have like a GitHub repo?
- 54:55Or how should they give you feedback or say, you know, my favorite refactoring is whatever
- 55:01you guys don't do.
- 55:01How do they make that happen?
- 55:03Yeah, we've got the Sorcery AI repo where you can raise issues.
- 55:05Or just email us.
- 55:07And our GitHub repo is sorcery-iai slash sorcery.
- 55:16Very cool.
- 55:16Okay, there.
- 55:17Awesome.
- 55:18Well, Brendan and Nick, thank you both for being here and creating this cool project.
- 55:22Looks awesome.
- 55:22Thanks very much.
- 55:23Thank you very much for having us, Michael.
- 55:25Yep, you bet.
- 55:26Bye-bye.
- 55:26Bye-bye.
- 55:26Bye.
- 55:27This has been another episode of Talk Python To Me.
- 55:31Our guests on this episode were Brendan McGinnis and Nick Thappen.
- 55:35And it's been brought to you by Datadog and Linode.
- 55:38Datadog gives you visibility into the whole system running your code.
- 55:42Visit talkpython.fm/datadog and see what you've been missing.
- 55:46Go throw in a free t-shirt with your free trial.
- 55:48Start your next Python project on Linode's state-of-the-art cloud service.
- 55:52Just visit talkpython.fm/Linode, L-I-N-O-D-E.
- 55:57You'll automatically get a $20 credit when you create a new account.
- 56:00Want to level up your Python?
- 56:03If you're just getting started, try my Python Jumpstart by Building 10 Apps course.
- 56:08Or if you're looking for something more advanced, check out our new async course that digs into
- 56:13all the different types of async programming you can do in Python.
- 56:16And of course, if you're interested in more than one of these, be sure to check out our
- 56:20Everything Bundle.
- 56:20It's like a subscription that never expires.
- 56:23Be sure to subscribe to the show.
- 56:24Open your favorite podcatcher and search for Python.
- 56:27We should be right at the top.
- 56:28You can also find the iTunes feed at /itunes, the Google Play feed at /play,
- 56:33and the direct RSS feed at /rss on talkpython.fm.
- 56:37This is your host, Michael Kennedy.
- 56:39Thanks so much for listening.
- 56:41I really appreciate it.
- 56:42Now get out there and write some Python code.
- 56:43I'll see you next time.
- 57:03Thank you.