Latest / Talk Python To Me / #18: Python Anti-patterns and other mistakes
Transcript
- 0:00Often, the most important lessons we learn are what not to do.
- 0:03Today, we'll be talking about bad Python code and Python anti-patterns with Andreas Duvez.
- 0:09It's show number 18, recorded Thursday, July 1st, 2015.
- 0:13Welcome to Talk Python To Me, a weekly podcast on Python, the language, the library,
- 0:43the ecosystem, and the personalities.
- 0:45This is your host, Michael Kennedy.
- 0:46Follow me on Twitter, where I'm @mkennedy.
- 0:49Keep up with the show and listen to past episodes at talkpythontome.com,
- 0:53and follow us on Twitter, where we're at Talk Python.
- 0:55This episode, we'll be talking to Andreas Duvez from Quantified Code about Python anti-patterns.
- 1:02This episode is brought to you by CodeShip and Hired.
- 1:06Thank them for supporting the show on Twitter via at CodeShip and at Hired underscore HQ.
- 1:13I don't have any major news for you this week, so let me introduce Andreas so we can get right to the show.
- 1:19Andreas is a physicist and software developer with a passion for code quality.
- 1:23He leads the development of algorithms and software at Quantified Code.
- 1:27Andreas, welcome to the show.
- 1:29Thanks.
- 1:30Thanks for having me here.
- 1:32It's great to have you here.
- 1:33We're going to talk about design patterns, both good design patterns and bad design patterns or anti-patterns,
- 1:40with the stuff you guys have going on at Quantified Code.
- 1:43Exactly.
- 1:44So you've built a bunch of awesome tools, both visual and static analysis, and we're going to talk about that.
- 1:50But before we get into it, let's just take a step back down memory lane and, you know,
- 1:55tell everyone how you got started in Python and programming and what's your story?
- 1:58How did you get here?
- 1:59So my background is actually not in software development, but in physics.
- 2:04And I made my first contact with Python while I was doing my PhD, which was about, like,
- 2:10experimental quantum computing.
- 2:12So we did experiments with microwaves and lots of signal processing.
- 2:19And when I arrived at our lab, we had, like, a lot of equipment that we needed to, like, control
- 2:25using programs, and we had a lot of data that we needed to analyze, to visualize, and to process.
- 2:32And we did that using various programming languages, for example, Matlab and C and C++.
- 2:38And we were also using tools like LabVIEW that help you to, like, define interfaces, graphical user
- 2:46things, and, like, control your measurement equipment.
- 2:50And then I remember when I came to the lab, this was basically a huge mess because we used, like,
- 2:57four different programming languages and the graphical programming interface of LabVIEW, which is nice in principle,
- 3:04but which makes it really, really difficult to write complex software systems with that.
- 3:09And so I said, hey, there has to be a better way to do this.
- 3:13And one of our postdocs, he used Python in his job in the U.S., and he introduced me to it.
- 3:19And so I started, like, getting interested in that, and I tried to, like, replace more and more of our software stack with Python.
- 3:26And after about one year or half a year, I think, we had replaced most of the data acquisition and data analysis things we had in the lab with Python programs.
- 3:37So that was kind of, like, the first contact with me, for me, with that language.
- 3:41And also when I, like, that I saw that it's really powerful and you can basically do anything with it.
- 3:48And, yeah.
- 3:49Yeah, that's great.
- 3:51I think a lot of people come to Python for the data science component of it.
- 3:55And I think, like you were saying, the fact that it's a full stack, legitimate, end-to-end programming language means you don't have to just live within those little shells of what MATLAB will do or LabVIEW will do.
- 4:07But you can bring it all together.
- 4:08Yes, exactly.
- 4:09Yeah.
- 4:10And, I mean, back in 2009, when I started using it, the tooling was already pretty good.
- 4:15But if you have a look at it now in 2015, it's really amazing what kind of an ecosystem people have created.
- 4:23You know, for example, the IPython notebook, NumPy, SciPy, and all these other tools that we can use to, as you say, do almost anything that we can imagine.
- 4:32So it's really, really amazing to see.
- 4:35Yeah, it is really amazing.
- 4:36And it's great.
- 4:38And I think the thing that, among other things, that surprises me is it's free.
- 4:42You look at MATLAB and Mathematica, you pay tons of money to be locked into these, like, somewhat confusing.
- 4:47Yeah, exactly.
- 4:48And it's the power of open source.
- 4:50So I think, yeah.
- 4:52You're doing all this cool programming and getting into Python for your work at Quantified Code.
- 4:57This is a company you co-founded.
- 4:58Who do you co-found it with?
- 4:59And what do you guys do there?
- 5:01So I co-founded it with an old friend of mine.
- 5:04And we're doing static code analysis for Python, but also other programming languages.
- 5:09And the reason we started this company was that I saw during my PhD that, hey, it's really hard to write good software because I didn't have a background in software development.
- 5:18So I wrote lots of very, very bad code.
- 5:21And I made a lot of mistakes.
- 5:23And I thought it would be really nice to have, like, a system, an automated system that would tell me how to write good code.
- 5:29So that's when I got interested in all these things like static code analysis.
- 5:32And I started looking into what kind of tools existed for Python and other languages.
- 5:37And I found that most of the tools that we're using today are still not good enough or they could be much better.
- 5:43And so that's kind of our mission to, like, make these tools much better and allow anyone to, like, have access to really high-quality, state-of-the-art code analysis.
- 5:53Yeah, that's great.
- 5:54One of the problems with a lot of static code analysis is it's very good at finding the small rules.
- 5:59Like, hey, you should have white space between these things.
- 6:03You should have a new line here.
- 6:04Your line shouldn't be this long.
- 6:06Your variables should be named this way.
- 6:07Yeah, exactly.
- 6:08But that's, like, the stuff that robots can do, you know?
- 6:10The stuff that's really valuable is the hard-learned lessons of if you write your code this way, it's easier to maintain.
- 6:17If you write your code that way, it will actually run faster or it will be less error-prone.
- 6:22And that's normally what you get by working with kind of a mentoring apprenticeship, you know, working in a team.
- 6:28But it's also the whole concept of design patterns, right?
- 6:32Yes, exactly.
- 6:34I mean, design patterns certainly help people to become productive and to use solutions that are better proven, so to say, and tested in the field.
- 6:43I mean, a design pattern in that sense is just like a reusable solution that you can take and implement in your own code.
- 6:51And, I mean, I wasn't even aware of that, but I also made use of design patterns, which are covered, like, from the internet for the work I did during my PhD.
- 6:59I used, for example, the observer pattern and the command pattern and other things, which I just, like, yeah, intuitively could adapt to my own needs and use in my projects.
- 7:13And the great thing about this is also that it gives you, like, a common language to talk to other people, you know, because if you say, hey, I got this class, which is, like, receiving a notification from this other class and then I was doing some things.
- 7:27I mean, it's okay, but you could just say, hey, I'm using this observer class and then everyone kind of knows what you mean, you know?
- 7:33Yeah, I think that is one of the absolute really strong powers of design patterns.
- 7:39Obviously, one is to know the solutions, but the other is to have larger building blocks as you think about your application mentally.
- 7:46Yes, exactly.
- 7:47All right.
- 7:47Like you say, if I go to you and I describe, okay, what I'm going to do is I'm going to have a variable and the variable is going to be shared.
- 7:53There's only going to be one of them and it's going to be shared across all parts of the application.
- 7:57And so when one part changes it, the other part will see that same change, I could have this conversation or I could just say I'm using the singleton pattern.
- 8:05Yes, exactly.
- 8:06You know what I'm talking about.
- 8:07You know what the positive side of that is.
- 8:09You know what the negative side of that is.
- 8:11You know singleton pattern is a challenge for unit testing.
- 8:14And so is that a tradeoff you want to make?
- 8:16And it's not just helpful for working in a team.
- 8:19I can tell you that.
- 8:20You could tell me that.
- 8:21But I think it's also helpful just for you internally as you think about how your code is structured.
- 8:26Yeah, absolutely.
- 8:27We've all written a lot of code and we may or may have not given it good attention.
- 8:32We may have written it bad.
- 8:34We may have written it well.
- 8:36We might have been in a hurry.
- 8:37And so, or, you know, much more common, we've gotten it from somebody else.
- 8:41And we have to deal with it.
- 8:43And I think understanding these design patterns is really interesting, but also understanding when they're needed.
- 8:50And so there's this related idea of this concept called code smells.
- 8:55I think it was introduced by Martin Fowler, but I'm not entirely sure.
- 8:59Are you familiar with it?
- 8:59Yes, it's possible.
- 9:00Yeah.
- 9:01I'm not 100% sure.
- 9:02I mean, design patterns has been introduced by this gang of four in 1994, but I'm not sure where the term code smells comes from.
- 9:10Yeah, certainly Martin Fowler wrote a lot about it.
- 9:12And I think it's a very interesting way to think of things.
- 9:16So I'll put a link to some code smells in the show notes.
- 9:20And I think kind of what your code does is it sort of looks for code smells.
- 9:24It says you're doing these things that are not necessarily going to make your code not run, but it's not the best way to do it.
- 9:30Yes, exactly.
- 9:31I mean, what a static code analyzer does is basically that it looks for patterns in the code that can either produce bugs or are quality problems of the code.
- 9:43And so for us, like code smell can be categorized by how severe it is.
- 9:49So it might make your code crash or it might just be an annoyance to another programmer who takes longer to understand the code.
- 9:57And also by various categories like does it affect the performance?
- 10:01Does it affect the security of the code?
- 10:03Does it affect the maintainability?
- 10:05So there are lots of lots of different types of code smells in that sense.
- 10:10Yeah, and if you think back, you know, just thinking of the code smells that I can remember, there's some good ones like duplicated code.
- 10:18Oh, yeah.
- 10:20That one's common, right?
- 10:22Yes.
- 10:23Or feature envy or, you know, long method.
- 10:27Or there's a bunch of, you know, too many parameters.
- 10:32Yes.
- 10:32I mean, this we're seeing a lot in the code we analyze.
- 10:35Very complex functions, a lot of parameters that have too many responsibilities and are really hard to understand.
- 10:43So, I mean, I think that's really the number one performance or maintainability killer today in software complexity.
- 10:51I agree.
- 10:51One of the really interesting code smells, I think, are comments.
- 10:57And so you think, you know, comments, I'm supposed to comment my code.
- 11:00That's a good thing.
- 11:01A lot of times programmers will use comments where they should have just written better code.
- 11:06Yes, exactly.
- 11:07They'll write a function that has a bad name and it's not well written.
- 11:11So it's hard to understand.
- 11:12And it'll put a nice little comment to describe what it does because it's not well written, you know, where the real fix would be to write a well, to refactor, to have a proper descriptive name and to be cleaner in smaller parts, right?
- 11:27Yeah, absolutely.
- 11:28Yeah.
- 11:29Comments are kind of like deodorant for code smells, right?
- 11:32That's a good conversion, yeah.
- 11:35Okay, so that brings us to how I kind of got connected to you guys or learned about you guys is through this thing you wrote called The Little Book of Python Anti-Patterns.
- 11:45Yes.
- 11:46Can you tell us about that?
- 11:47Yeah, sure.
- 11:48So this was a project we started because we wanted to get an overview of what problems you would find in Python code.
- 11:57And so we started collecting problem types and code smells and issues from different sources, for example, lintes like PyLint or PyFlakes that already have like a collection of a few hundred of those anti-patterns.
- 12:12But also from sources such as Stack Overflow and articles, blog posts, and also from people and from code that we were reading.
- 12:20And so we like collected all this together and then we worked with a freelancer in the beginning who helped us like write a large part of the initial text.
- 12:28And we bundled this whole collection of anti-patterns together into a book so that people could just like learn about different things they should avoid in their Python code.
- 12:40So it's not like learning from good code, but learning from bad code in this sense.
- 12:44Yeah.
- 12:45You know, it's related to that code smells idea.
- 12:47Like I see all these problems and if I know to avoid them, well, either you know the right design pattern and the right style or you just know you need to go do some research and don't do it this way.
- 12:58Yeah, exactly.
- 13:00Yeah.
- 13:00Yeah.
- 13:00In your book, you do have sort of recommendations for most of the anti-patterns, right?
- 13:06Yes.
- 13:07So we always, if we describe the problem, we always try to like give at least one or two solutions to it.
- 13:13I mean, sometimes there is no really unique solutions or people would kind of like disagree if it is a problem at all or not.
- 13:24So, but we are always pretty opinionated and we think like a good programmer should be a bit opinionated.
- 13:31And so we try to like give our solution that we would actually solve the problem with.
- 13:35Yeah, that's great.
- 13:37And let me just tell everybody that the book is at docs.quantifiedcode.com slash python dash code dash patterns.
- 13:44And of course, I'll put that in the show notes as well.
- 13:46Yes, that would be great.
- 13:47I mean, we also have like a GitHub repository and the whole book is open source released under a Creative Commons license.
- 13:53So we're always happy to see contributions.
- 13:56And like, yeah, so if people are interested, they would be very welcome to like contribute.
- 14:03Yeah, that's excellent.
- 14:04Let me pull this up here.
- 14:06So in your book, you've broken it into quick math, six sections.
- 14:11You have, you sort of categorize the problems that people run into.
- 14:15One of them is correctness.
- 14:16One is maintainability.
- 14:18Maintainability is one that catches up with you later.
- 14:21Readability, security, performance, and then you have a special section on Django.
- 14:27Yes.
- 14:28So yeah, I mean, as I said before, we tried to like just categorize the issues or anti-patterns so that people could like go through individual sections.
- 14:39And for example, see what type of problems really affect the correctness of their program and what type of anti-patterns only affect, for example, the readability or the performance.
- 14:50So let's maybe look at a couple of them.
- 14:53I'll just sort of scan through here and just tell people the kind of stuff that's in there.
- 14:57Sure.
- 14:58One that I think is somewhat common when you're new to programming in Python is this one you call bad accept clauses order.
- 15:08Can you talk about that?
- 15:09Yes.
- 15:10A bad accept clauses order basically means that you put an accept statement, that you have a sequence of multiple accept statements,
- 15:20where the first one of those statements is already more generic or general than the second one.
- 15:27So basically your second accept clause will never get executed.
- 15:31And that's kind of really easy to get wrong.
- 15:33It also happens to me a lot of the time.
- 15:36Yeah.
- 15:37Yeah.
- 15:37It's not obvious, especially if you're going back and adding these additional accept clauses.
- 15:41So maybe you say try, you're going to do some code and then you say accept exception as X and then accept type error as TE.
- 15:51Right?
- 15:52That type error will never get run because the exception is the base class of type error.
- 15:56Even worse than this, a pattern is probably an empty accept plug without also an exception type.
- 16:02I think that's kind of the most evil Python anti-pattern that exists.
- 16:06This episode is brought to you by CodeShip.
- 16:24CodeShip has launched organizations, create teams, set permissions for specific team members, and improve collaboration in your continuous delivery workflow.
- 16:32Maintain centralized control over your organization's projects and teams with CodeShip's new organizations plan.
- 16:38And as Talk Python listeners, you can save 20% off any premium plan for the next three months.
- 16:44Just use the code TALKPython, all caps, no spaces.
- 16:47Check them out at CodeShip.com and tell them thanks for supporting the show on Twitter where they're at CodeShip.
- 16:53I have to admit, sometimes I use it, but it's really bad because it swallows up all your exceptions and it doesn't give you any useful information about what happens in your code.
- 17:08Yeah, maybe if you're lucky, the empty accept clause at least has like a logging statement or something.
- 17:15Yeah, that's already good, yeah.
- 17:15Yeah, but if it has accept pass, that's pretty bad.
- 17:20That's really easy, yeah.
- 17:21I spend many hours debugging stuff like this, yeah.
- 17:24Most of the time I wrote that myself.
- 17:27Yeah, I like to think of that one as the intern clause.
- 17:32Yes.
- 17:33Because they're like, oh, this isn't working.
- 17:35Oh, I can just put this in here and my code is now reliable.
- 17:38I'll be out of here in three months.
- 17:40Let's see.
- 17:42There's some other interesting ones.
- 17:44Under future import is not the first non-doc string statement.
- 17:49What's the problem with that?
- 17:50Yeah, so this is just a convention of the Python interpreter because you can use these so-called future imports to import functionality that is usually not available in your Python version.
- 18:03For example, one thing that is often used are absolute imports.
- 18:05And the thing with the future imports is that they have to be the first statement in your file.
- 18:11So you cannot have auto code and then afterwards a future import because the Python interpreter needs to load these ones right at the beginning.
- 18:19So, I mean, this is kind of an easy pattern to catch, but still, sometimes it happens that you have this in your code.
- 18:26I mean, it's also something that you would notice really easily because your program wouldn't run even.
- 18:31Yeah, for sure.
- 18:32Another one that you can tell that people are coming from somewhere else and they haven't quite fully gotten the Pythonic idiomatic style is implementing Java style or C++ style getters and setters.
- 18:44Oh, yeah.
- 18:44Yeah, I like this one a lot.
- 18:45We also see it often in our analysis.
- 18:48And so this is basically when you write set something and get something function in a class, which you would do in Java.
- 18:56Whereas in Python, the appropriate or the correct way to do this would be to use so-called class properties.
- 19:02We can just have just access an attribute of a class using a function, which looks like an attribute access, so to say.
- 19:13Right.
- 19:13Just use the app property decorator.
- 19:15Yeah.
- 19:16Yes, exactly.
- 19:16Yeah.
- 19:17Yeah.
- 19:18Then there's others that are less obvious to me.
- 19:21One of them is if I'm using, I'm not using the default dict.
- 19:25What's the story of that one?
- 19:27Oh, so this is more a recommendation than a real antipodent.
- 19:30Python has this great collections library, which contains many data structures that can help you to make your code easier to understand.
- 19:39And the default dict is one of them.
- 19:41And it's one of my favorite ones and the one I probably use most in my code.
- 19:46And what it is, is a dictionary that basically initializes the value of a given key to something that you can pass in as a function.
- 19:56So the typical use case for this would be that you have a dictionary with a list inside.
- 20:03And so if you want to create an entry for some key that you haven't seen before, normally what you would need to do is to check if the key has been set in the dictionary before.
- 20:14And if not, create a list, an empty list in that place.
- 20:18And then append the element to the list.
- 20:20Yeah, so it's like three or four lines of code, right?
- 20:22But the default dictionary basically does, yes, so that's four lines of code.
- 20:25And the default dictionary basically does that for you because you can just say default dict of list.
- 20:30And then whenever you would like access a given value for a key in the dictionary, it will have created an empty list if there has been no value in it already.
- 20:40So really, really useful.
- 20:42And I did not know this when I started writing Python, but it really makes your code much more readable.
- 20:50Right.
- 20:50Does that come from the collections module?
- 20:52Default dict?
- 20:54Yes, exactly.
- 20:55Yeah.
- 20:55Yeah.
- 20:56Yeah.
- 20:56So it's not immediately imported, but it's easy to get to.
- 20:59Yes.
- 21:00It's in the standard library.
- 21:01Yeah.
- 21:02So another one that you have, let's move on to the next section of maintainability.
- 21:06And one of them I think is probably pretty common is you should not be using wildcard imports.
- 21:13So from math import star.
- 21:16It's so much easier to type.
- 21:19Why shouldn't I just do that all the time?
- 21:20Yeah.
- 21:21It's also a mistake which I made like a lot in the beginning when I started writing Python code.
- 21:27And it's pretty insidious because normally it doesn't break anything.
- 21:34But in some circumstances, you can overwrite functions that you have imported before.
- 21:39And so the behavior of your program changes in like really unexpected ways, I would say.
- 21:45And so today I always try to use only qualified imports.
- 21:50So either import the module and then use it as a name when I access the variable or explicitly name the things that I import from a module.
- 21:58Yeah.
- 21:59I'm with you on that.
- 22:00I try to, if it's not too much writing, try to use the name so it's really clear where the type is coming from.
- 22:05But sometimes, you know, it's like, well.
- 22:07Yeah.
- 22:08I know.
- 22:09I do it sometimes too.
- 22:10But, you know, more as like an explicit import.
- 22:12Yeah.
- 22:13So another area that you guys did a lot of work on was readability.
- 22:17And I think one of the ones that's, I don't know how easy this is for your system to check automatically.
- 22:26But certainly if you come from a language like Java or C# or C++ is not in your way of thinking.
- 22:34Right.
- 22:34Even though it's one of the core tenets of Python.
- 22:38And that's ask for permission instead of for forgiveness.
- 22:42Asking for permission is the anti-pattern rather than for forgiveness.
- 22:45Yes, exactly.
- 22:46Yeah.
- 22:46Can you tell people about that?
- 22:47So usually when you write Python code, the preferred way is to like assume that everything will go as you expect and not perform any checks.
- 22:59But rather catch an exception if some part of your program chose one.
- 23:05And like handle that exception.
- 23:07And then react to like behavior which you didn't anticipate, so to say.
- 23:12A good example is always like when you're opening a file.
- 23:16You could think of like checking if the path exists and then open the file at the path.
- 23:23But I mean between the two calls, like check of existence of the file and the opening, that could actually happen and stuff.
- 23:29And so your program could still crash and you will have an exception handler for that then.
- 23:35So the preferred way here is to just assume that the file is there and try to open it.
- 23:39And if you get an error because the file does not exist or something else is wrong, then handle that exception, so to say.
- 23:46Yeah.
- 23:46I think that's pretty key.
- 23:48Another example is just, you know, you're trying to think of all the use cases of how something could go bad.
- 23:53And you end up with like five or six if tests.
- 23:57And then finally you're going to actually perform the thing.
- 24:00And you may have forgotten one of those.
- 24:02Or there may be something else.
- 24:03So imagine I'm going to call a web service.
- 24:04Yeah, exactly.
- 24:05I've got to make sure the network is on.
- 24:07I've got to make sure that the address is well formed.
- 24:11And then, you know, I've got to make sure my data, I'm going to send it as well for them.
- 24:15And then it still may be the case that the DNS name doesn't look up.
- 24:17I mean, there's so many things that could go wrong.
- 24:19Just try it and somewhere higher up where it makes sense, catch it and handle it, right?
- 24:24Yeah.
- 24:24That's what you're recommending there.
- 24:26Another thing which I did quite often in the beginning when I learned Python was to use, for example, the non variable to like indicate like a special that something happened inside a function, for example,
- 24:39that something didn't exist and function couldn't do what it was supposed to do.
- 24:42And then basically check for this non value in the code that called the function.
- 24:47And this, of course, makes it also very complicated to like handle these kind of errors because now the function does not return only one type of value, but two different ones, like non, for example, and like a numerical value or something.
- 25:02And that's also a very good case where you could like ask for forgiveness instead of permission by just like having a function that throws an exception when something goes wrong and then catch that exception in the calling block of the function instead of like looking for a magic non value or something.
- 25:19And the thing about that is that complexity that you're adding by having a return of none or a real value or some sentinel value that is indicating the wrong value means you propagate that complexity up through the callers and then it propagates up higher.
- 25:32Exactly.
- 25:33It's like you forced this style onto everybody up the chain.
- 25:38That's bad.
- 25:38So another one that you have that I think is pretty common for people who are new to Python, but quickly they get over it, is using an un-Pythonic loop or a non-Pythonic loop.
- 25:50What's a non-Pythonic loop for the listeners?
- 25:53So, I mean, many people, me included, that come, for example, from like a C background, they're used to writing loops where they initialize some loop variable like X.
- 26:05They set them to zero.
- 26:06Then they increase the value of that variable to, for example, the length of an array.
- 26:13And inside the loop, they then fetch the value of the array element using the index operator, so to say.
- 26:23And in Python, of course, you can just like iterate directly over a list, for example, and not use a range to like first get the indices of the list and then only retrieve the value of the inside the loop.
- 26:36I mean, this is only a small thing, but it also makes your code much more readable and it like helps you to avoid errors because it involves less variables and less things that can go wrong, so to say.
- 26:47Yeah, that makes a lot of sense.
- 26:49So the next section, security, is actually pretty simple.
- 26:53Yes, I have to say we have to work on a security section because right now we only have, I think, one entry, which is the use of exec.
- 27:02And in our analysis tool, we flagged that by default because it's not always an anti-pattern or something that you should avoid.
- 27:10But it's, of course, a huge, huge opening in your system and it can like be used easily to like execute like untrusted code.
- 27:21So I think it's not generally an anti-pattern, but if you use it, you should definitely be really careful with it and like think a lot about which code or which things you're passing into exec.
- 27:33Yeah, absolutely.
- 27:35Because unfortunately, Python does not have a real sandbox as well, a safe sandbox.
- 27:39So, I mean, there are some things that you can do to make it a bit more safe to use exec, but there's nothing that can prove like 100% security in that case.
- 27:49Yeah, to a large degree, it comes down to how much do you trust that string input that you're passing there, right?
- 27:55Yes, exactly.
- 27:56Yeah.
- 27:56And if it's entered into a community forum, it probably shouldn't be exec.
- 28:02Yes.
- 28:03It would be basically the Python equivalent of a SQL injection attack.
- 28:09Yes.
- 28:10Something to that effect.
- 28:11Okay.
- 28:12Some other ones that were interesting in your performance area.
- 28:16You have one using key and list to check if a key is contained in a list.
- 28:21What's the story of that one?
- 28:22Yeah.
- 28:23I had to say the performance section is also pretty small right now, but this pattern has to do with the complexity or the computational complexity of an operation.
- 28:33Because if you, for example, try to find out if a certain value is in a long list, in our example, we have, for example, a list of several numbers, and you want to find out if, for example, a tree is in that list, then the code that checks this will have a linear runtime.
- 28:51So it will take longer, the longer the list is, so to say.
- 28:55And if you would use a dict instead, where you have just a mapping between the values that you have in a list and a true value, for example, then you could perform the same check in constant time.
- 29:08So order of one.
- 29:11I mean, it's also like a small thing, but if you have really long, large data structures, which, for example, I had in my PhD when analyzing data, this can make a huge difference in the wrong time.
- 29:20You're right.
- 29:21It can make a tremendous difference.
- 29:23I'll tell you a real world story where this completely changed my perspective on this.
- 29:39This episode is brought to you by Hired.
- 29:42Hired is a two-sided, curated marketplace that connects the world's knowledge workers to the best opportunities.
- 29:48Each offer you receive has salary and equity presented right up front, and you can view the offers to accept or reject them before you even talk to the company.
- 29:57Typically, candidates receive five or more offers in just the first week, and there are no obligations ever.
- 30:04Sounds pretty awesome, doesn't it?
- 30:06Well, did I mention there's a signing bonus?
- 30:08Everyone who accepts a job from Hired gets a $2,000 signing bonus, and as Talk Python listeners, it gets way sweeter.
- 30:16Use the link Hired.com slash Talk Python To Me, and Hired will double the signing bonus to $4,000.
- 30:24Opportunity's knocking.
- 30:26Visit Hired.com slash Talk Python To Me and answer the call.
- 30:29I was working on this project involving eye tracking.
- 30:43Like, not the letter I, but, you know, where you're looking.
- 30:46And this eye tracking system was collecting data at 250 hertz, so 250 times a second.
- 30:52And we were trying to do real-time analysis on this, which meant we had four milliseconds between the times that sample came in.
- 30:58It had to, like, really be quick and efficient.
- 31:02And we were doing some stuff that we had ported over from MATLAB.
- 31:06And we were doing basically what you said.
- 31:08We had kind of this, like, running buffer of data.
- 31:11And we would have to seek back into it and find certain elements.
- 31:14And then we would apply that forward over a certain amount of time.
- 31:18And we ran it, and the code was too slow.
- 31:20We're like, oh, no, it's too slow.
- 31:22If we can't make it go four milliseconds, it's not going to be real-time.
- 31:27It's just that simple, right?
- 31:28And so there was just tons of complicated math.
- 31:32And, like, we really don't want to try to optimize that.
- 31:36We're like, it's like wavelet decomposition.
- 31:38And so it was, like, on the very edge of even understanding the math.
- 31:42And so optimizing it was a bad idea.
- 31:45And after running some performance analysis, profiling stuff on the code,
- 31:49it turned out 80% of the time was spent looking for an item in the list.
- 31:53Oh, I see.
- 31:54So we got it to go, like, five times faster by switching to a dictionary.
- 31:59And it was, like, basic.
- 32:01That's great.
- 32:02That's a quick win, yeah.
- 32:03It was so little work.
- 32:04And we got to avoid optimizing wavelet decomposition math, which is fantastic.
- 32:08That's awesome, yeah.
- 32:10Yeah, so it sounds really simple.
- 32:12Yeah, no, that's a perfect example.
- 32:14I mean, yeah, small things can make a big difference, yeah.
- 32:16Yeah, they sure can.
- 32:17That's cool.
- 32:18The last section is Django.
- 32:20And I'm more of a micro-framework guy.
- 32:22I haven't done a lot of Django.
- 32:23Probably have a show on that coming up at some point.
- 32:24But maybe you could talk about some of the highlights there for us.
- 32:27Yeah, so we added a Django section so that a lot of people were using a project.
- 32:31I mean, a lot of the Python projects out there are web projects using Django or Flask or something else.
- 32:38And so we wanted to help people also not only writing better Python code in general, but also getting more proficient at libraries like Django or Flask or anything else.
- 32:49And so the Django section is basically organized in the same way as the main section.
- 32:54So we have maintainability, security, correctness, etc.
- 32:57And the only difference is that we are only talking about stuff that is really specific to the Django framework.
- 33:05So we're telling you how to import modules, how to use certain fields and models, which variables you should have in your config and which variables should not have in your config under some conditions.
- 33:17And also something that is really helpful to many people is we show you which things you would need to change actually in your code if, for example, you wanted to migrate from Django 1.6 to Django 1.8.
- 33:29Right. Oh, that's really helpful.
- 33:31There's a lot of stuff in there that's not obvious.
- 33:33And I think a lot of people learn Django by going to the tutorial and going through it and go, now I know Django.
- 33:38Yeah, exactly.
- 33:39I mean, Django is changing really fast.
- 33:41I mean, the community is really awesome and they're putting out a lot of new features and new versions.
- 33:48And I think it's good to have like this resource where you can see what has changed and how you would need to change your code in order to make it work with the latest version.
- 33:58And I mean, normally you want to be on the latest version because it's more secure and more features and probably better than the precedent one.
- 34:06Yeah, definitely.
- 34:08When you're running a website, you definitely want to stay on the latest version of your framework.
- 34:11Exactly.
- 34:12Okay.
- 34:13So this project, the book you said is on GitHub and your general GitHub is just, it's github.com/quantified code, all lowercase.
- 34:23Yeah.
- 34:23Exactly.
- 34:24Yeah.
- 34:25So something I found up there when I went to check out your anti-patterns project on GitHub was you also have this thing called a repository called xMachina.
- 34:37Ah, yeah.
- 34:38Okay.
- 34:38That's pretty cool.
- 34:39Can you tell just people really briefly what that is?
- 34:41It looks awesome.
- 34:42I have to say this is something that my co-founder, Christoph, did because in the movie xMachina, there's a snippet of Python code that you can see on the screen.
- 34:52And he just wrote down the code because he was interested in what it would do.
- 34:57And it actually prints out an ISBN number of a book that is mentioned in the movie.
- 35:03And he also ran it through our static analysis software and he found that actually it's not like PEP 8 compliant and there's some other things in it.
- 35:09So it probably shouldn't be security code, which it was in the movie.
- 35:13Sure.
- 35:14Well, you know, the fact that you can take code from a movie and it even runs is already a good deal.
- 35:20Yeah, it's great.
- 35:21I mean, it's really good to see.
- 35:23Awesome to see that Python is used in movies now.
- 35:26Yeah, yeah.
- 35:26XMachina looks really cool.
- 35:28I have not seen it yet.
- 35:29Yeah, great movie.
- 35:30I would like to talk about it, but I don't want to say any spoilers.
- 35:33Oh, yeah, yeah.
- 35:34I'll put a link to the trailer on YouTube in the show notes for everyone.
- 35:38They can check it out and go see it for themselves.
- 35:40Awesome.
- 35:41So another thing that you guys have up there is something called Code is Beautiful.
- 35:45What's the story with that?
- 35:48Exactly.
- 35:49So in Quantified Code, we don't only do code analysis, but we're also thinking about new or better ways to visualize code.
- 35:58Because for us, dealing with a large code base always involves getting an overview of the whole thing and seeing where, for example, problems are or where work would need to be done.
- 36:11And so visualizing the code is a great way to do that.
- 36:14And we always liked things like, for example, Cityscape visualizations, but we didn't find anything that was open source or available to the public, so to say.
- 36:24And that's why we said, hey, let's develop some cool visualizations of Python code and give them for free to the community.
- 36:33So we started this code, this open source project.
- 36:36And we have currently three different visualizations of source code.
- 36:39One is like a Cityscape visualization, which is like a 3D version of your code where you can, so to say, zoom in and see at a glance where, for example, the most complex part of your code are.
- 36:50That's really cool.
- 36:51And you should point out to people that it's live.
- 36:54You can rotate it and hover over these little buildings or skyscrapers, and it'll tell you what segment of your code that is.
- 37:00What do the colors mean in your visualizations?
- 37:03So in the visualization, currently, the color encodes the complexity on the code.
- 37:08So it's basically the cyclomatic complexity divided by the number of lines.
- 37:13That is fantastic.
- 37:14So the cyclomatic complexity is just the number of branches in the code, so to say.
- 37:21Roughly, you can think of it like the number of unit tests you would need to test a given piece of code.
- 37:27Right.
- 37:27How many decision points are there in a given function in a class or something like that.
- 37:31So I can create this cool cityscape, and the red buildings are more bad.
- 37:37And if I'm going to look at somebody's code, I guess maybe looking at the height and width of a building also tells me how much is monolithic.
- 37:47Yeah, exactly.
- 37:47So the area is the number of lines in a given file, and the height is just the total complexity of the file.
- 37:57Yeah, okay.
- 37:58Very cool.
- 37:59And then you also have two other visualizations that are, I would say, more standard.
- 38:04Yes, so they're like 2D, a bit more down-to-earth visualizations.
- 38:08And, yeah, we have them because many browsers still don't support WebGL, and we wanted to have something that, like, everyone can use, so to say.
- 38:18Right.
- 38:19And they're all interactive with D3, right?
- 38:21So you can sort of explore them and so on.
- 38:23Yeah, exactly.
- 38:25And they're also open source, so you can download them in GitHub, use them in your own projects, and modify them.
- 38:30Excellent.
- 38:31Yeah, you have a live demo that people can go check out, right?
- 38:33And what are you analyzing?
- 38:34Are you analyzing Django there, or what are you analyzing?
- 38:37Oh, we have a lot of projects on our platform, about, like, 10,000 public GitHub projects that we constantly analyze.
- 38:45And you can basically go there and see if your project has any problems or if you could improve anything.
- 38:50And you can also get visualizations of all the popular Python projects there.
- 38:54Oh, that's awesome.
- 38:56Is there a way to hook that visualization into a continuous integration story?
- 39:00So, like, every time I push to a branch, a build is kicked off, and then an analysis is kicked off and saved.
- 39:05Have you guys done this?
- 39:07Oh, yeah, you can do that.
- 39:07Really?
- 39:07You can just sign up for free on our website, and you can add your GitHub project.
- 39:11And every time you will make a commit, we will analyze your project, and then you can get the visualizations on our website.
- 39:17And it's, of course, free for open-source projects.
- 39:19Yeah, yeah, that's really cool.
- 39:21So, that's quantifiedcode.com.
- 39:22Yeah, very nice.
- 39:23Yeah, I really like what you guys are doing there with that one.
- 39:27Thanks.
- 39:28Yeah.
- 39:28Yeah.
- 39:29So, I have a few more questions before we call it a show.
- 39:32One is, what are the worst anti-patterns that you see?
- 39:37Like, we went through a couple of them, but are there some that you go, oh, my gosh, if you do this, then it's really bad?
- 39:42So, I mean, as I said before, the empty try-accept statement is one of the worst things to debug, I think.
- 39:50And apart from that, I think it's not the simple anti-patterns that are, like, killing software projects.
- 39:58It's mostly, like, complexity and, like, changing code too fast and without using, having a solid idea of what you're doing and without, like, writing unit tests.
- 40:11So, I think there's no really easy recipe to say, okay, if you avoid this and this and this, then you will write very good code.
- 40:19But I think it's, like, mostly about, like, controlling your complexity and, like, constantly thinking about your code structure and thinking how you can improve it and also discussing it with other people and doing manual code reviews and automated code reviews.
- 40:35So, yeah, for me, it's like, as a software development, our daily job is to fight complexity.
- 40:41Yeah, I think you're right.
- 40:43One of the problems that people end up with is they end up with complex, highly cyclomatic, you know, high cyclomatic complexity and all that given to them.
- 40:52And then they're like, well, if I had written this, I would be beautiful.
- 40:56But, of course, I didn't.
- 40:57And so now what do I do?
- 40:58I'm kind of lost.
- 40:59So, two books, other than just using your tools to understand it, two books I'd like to recommend to people.
- 41:04One is called Refactoring the Patterns by Joshua Kershavisky.
- 41:09Probably destroyed his name.
- 41:10Sorry about that, Joshua.
- 41:11But that's a really cool book of how I take, like, monolithic ugly code and, like, make it better.
- 41:16And the other one is a book by…
- 41:18I can really recommend it.
- 41:19Yeah.
- 41:19Do you know the book?
- 41:20Yeah, I know.
- 41:22Yeah.
- 41:22It's a good one.
- 41:23The other one is Working Effectively with Legacy Code by Michael Feathers, which shows you how to take huge monolithic code and slowly break it into more maintainable pieces without breaking it, you know, while keeping it running, basically.
- 41:36So, those are some good things to check out if you're interested.
- 41:39There's also Clean Code.
- 41:41I just don't remember the name of the order right now.
- 41:43I don't know.
- 41:44Do you remember?
- 41:44That would be Robert C. Martin.
- 41:47Yes.
- 41:48Okay.
- 41:48Uncle Bob.
- 41:49Sometimes he goes that.
- 41:50Yeah.
- 41:51So, that's also a great resource, not specifically in refactoring, but more generally in how to write good code.
- 41:56And it's mostly written for Java, but it contains so much wisdom.
- 42:01I think I read it, like, five times already.
- 42:03So, every time I've done something new.
- 42:05Yeah.
- 42:05He did a really good book there.
- 42:08And I believe he might have written that with his son, Micah, as well.
- 42:10So, there might be two authors.
- 42:11But, yeah, it's very cool.
- 42:12All right, Andreas.
- 42:14This has been a really interesting conversation.
- 42:17Before I let you off the book…
- 42:19Yeah, thanks for the conversation.
- 42:19You bet.
- 42:20Before I let you off the hook, there's a couple of thoughts and questions I have for you.
- 42:26Sure.
- 42:26One, what's your…
- 42:27You know, there's a bunch of PyPI packages and libraries out there in the world.
- 42:32Do you have any favorites that you'd like to recommend to people?
- 42:36Mm-hmm.
- 42:36So, right now, I use SQLAlchemy a lot.
- 42:40And I think it's really, really a great Python package.
- 42:43It's perfectly documented.
- 42:46And it allows you to do so much magic with SQL.
- 42:51And it has allowed…
- 42:53It has saved us so much time.
- 42:54So, it's really, really a great project.
- 42:56And if you're working with SQL databases, you should have a look at that, definitely.
- 43:02I totally agree.
- 43:03You know, if I'm working with a relational database, I wouldn't use anything else, basically.
- 43:08That's how I feel about it, right?
- 43:11We actually had Mike Bayer on show five to talk about SQL as well.
- 43:15And so, if people are listening now and they want to go get more info on it, we've got a whole show there.
- 43:19That's awesome.
- 43:20Yeah.
- 43:20And what's your favorite editor?
- 43:22I mostly use Sublime Text, I have to say.
- 43:25But I'm not even a power user, so I'm pretty old-fashioned.
- 43:28And sometimes I also use Vim to edit my code.
- 43:31But I really don't use many key combinations or so.
- 43:35So, I'm really…
- 43:35When it comes to developing, writing code, I'm…
- 43:39Yeah.
- 43:39Yeah.
- 43:40Cool.
- 43:40Okay.
- 43:41Excellent.
- 43:41Yeah.
- 43:41Sublime is great.
- 43:42So, have you thought about selling an e-book version of your little book of anti-patterns?
- 43:48Oh, I mean, right now we really don't see this as a commercial project.
- 43:53And we just want to make this available to as many people as possible.
- 43:56That's why we licensed it using a Creative Commons license.
- 44:01So, basically, you can download it, modify it, and do whatever you want with it.
- 44:05So, and right now I think we don't have the time to make an e-book version.
- 44:09But if somebody would want to do it, he or she could do it.
- 44:13Excellent.
- 44:14Yeah.
- 44:15Seems like you could write a cool script that would generate a little e-pub or something that you could throw at your kiddo.
- 44:21I think the documentation swings also supports e-book and mobi outputs.
- 44:29So, I think it would be pretty easy to do.
- 44:31Oh, yeah.
- 44:32Cool.
- 44:32It's another good package, which I could recommend, by the way, if you're writing documentation.
- 44:35Yeah.
- 44:35Yeah.
- 44:36That's what's powering your docs and your book.
- 44:38Great.
- 44:38Yeah.
- 44:39Okay.
- 44:40And then why don't you just tell us a little bit about what you guys do at Quantified Code so people can check out whatever you're up to in your day job.
- 44:48So, I mean, at Quantified Code, what we're doing now is mostly static code analysis.
- 44:53But what our big vision is, so to say, is to change the way that people write software.
- 44:59Because we think that today we're still using tools that are very similar to the ones that we used like 30 years ago.
- 45:07We're still entering text using editors, and we're thinking of code as text files, and we're not making use of all the progress in software development and computer hardware as well that has been made in the last 30 years.
- 45:22So, our mission is kind of like to think about new ways to interact with code, to analyze code, and to transform it, and to also make use of all the available data that is out there.
- 45:34Because today, most of us develop software using version control systems like Git, et cetera.
- 45:39And those systems, they generate an amazing amount of data about every interaction we have with a code base.
- 45:45And we can make use of that and basically use that data to find out how you can become a better programmer, how you can be more effective at your job, how you can be more happy maybe, and how you can write code that is more robust.
- 46:00So, this is basically what we're trying to do.
- 46:03And the first step on this way is, so to say, to develop a new way to do code analysis and help people to avoid some of the most problematic things in their code.
- 46:16That's really a great vision and a mission, and you guys are off to a good start.
- 46:21Thanks.
- 46:21Cool.
- 46:22Of course.
- 46:23It will take a while until we get there, I guess.
- 46:25But, I mean, yeah, somebody should do it.
- 46:28Yeah, if you're going to change the world and no one else is doing it, maybe that's you, right?
- 46:32That's awesome.
- 46:33Okay, well, Andreas, thank you so much for being on the show.
- 46:36It's been great to talk.
- 46:37Yeah, thanks for having me.
- 46:38It was really great.
- 46:39Yeah, you bet.
- 46:40It's been great to talk about bad code with you.
- 46:42Thanks.
- 46:43All right.
- 46:44See you later.
- 46:45See you.
- 46:46Bye-bye.
- 46:48This has been another episode of Talk Python To Me.
- 46:50Today's guest was Andreas Duvez, and this episode has been sponsored by CodeShip and Hired.
- 46:56Thank you guys for supporting the show.
- 46:58Check out CodeShip at CodeShip.com and thank them on Twitter via at CodeShip.
- 47:03Don't forget the discount for listeners.
- 47:05It's easy.
- 47:05Talk Python, all caps, no spaces.
- 47:08Hired wants to help you find your next big thing.
- 47:11Visit Hired.com slash Talk Python To Me to get five or more offers with salary and equity
- 47:16presented right up front and a special listener signing bonus of $4,000.
- 47:20You can find the links from today's show at Talk Python To Me.com slash episodes slash show slash 18.
- 47:29Be sure to subscribe to the show.
- 47:30Open your favorite podcatcher and search for Python.
- 47:33We should be right at the top.
- 47:35You can also find the iTunes and direct RSS feeds in the footer of the website.
- 47:40Our theme music is the song Developers, Developers, Developers by Corey Smith, who goes by Smix.
- 47:46You can hear the entire song on our website.
- 47:48This is your host, Michael Kennedy.
- 47:50Thanks for listening.
- 47:50Smix, take us out of here.
- 48:10Developers, Developers, Developers, Developers, Developers, Developers, Developers, Developers.
- 48:15you