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