Loading video player…

Have a First Look at the Code

00:00 Welcome to another Code Conversation. My name is Philipp from Real Python, and joining me today is Martin. Hi, everyone. We got a question in our office hours session, and we will tackle the question itself at some point, but we thought at first it might make sense to tackle the topic of how to ask questions online and what’s a good way to change your code and update your code to actually get answers that you want to get.

00:28 So we will kind of transition through this course by first talk about the code itself, then the question, and then go into the refactoring, and in the end finally answer answer a question.

00:41 We thought it might be interesting to take you along with this ride and not just present your refactoring, but also talk a little bit about our first impression.

00:51 And this would be actually my segue into the conversation with you, Martin. So what’s your first impression of this code? When you saw this code the first time, what are your thoughts about it?

01:02 When I look at this at the first time, it was a little confusing. There seems to be quite a bit of unnecessary code and then also maybe not exactly the necessary code. Also, when connecting it to the question, which we’re going to talk about in a bit, it was a little hard to understand how the code that we see here is related to the question, I guess. Another thing is that the code currently actually wouldn’t run, and I can see that right away by just looking at these squiggly red lines here. Pylance is telling me that there’s a problem here. Predictions or "preds" is not defined.

01:41 So, the code doesn’t look very accessible to me. There’s a lot going on. It seems like maybe not the essentials are happening, and I can’t just put it into my interpreter and run it to see what the output is, which in an ideal case would help me better understand what is the person asking with their question and what is the issue that they’re running into. So yeah, it’s not an ideal thing to get a code like this.

02:06 And I think what we are going to explore in this code conversation too is like what’s the work that needs to be done on the side of someone answering this question, if they get a code example that isn’t somewhat prepared to be answered, you know? Exactly. Yeah.

02:21 Because you want to make it as easy as possible for a person to understand your code and give you an answer, and all the obstacles that a possible future helper might do to help you might be an obstacle too much, and the person is like, “Okay, I don’t want to even, like, understand the code first to give an answer.” So yeah, to make it as easy as possible, you want to remove the obstacles. And basically, we already did one thing.

02:48 So, imagine like this code that you’re seeing there is part of a bigger codebase, and by taking this code out and posting it into a new file here in VS Code, we can already see that there is a squiggly line in line 25, where your editor is saying like, “Hey, something, something is not okay here.” And if you post this code then into Slack, for example, in our help community, or if you post your question on Stack Overflow, maybe the person who helps you will copy and paste the code the same way.

03:22 And if you then see, well actually there are some errors here, this might be one of those obstacles we were talking about where the person is like, “Yeah, I’m not fixing your code first to actually get to your question”—Unless of course the specific error that you’re getting is the question that you have, you know, then there would be an error in your code that makes sense of course.

03:41 But in this case, this error here of the variable not being defined on line 25, that isn’t actually related to the question. So like Philipp just said, you’d have to dig through something to get to the actual problem, solve some problems to get to the actual problem that the question is about.

03:56 Exactly. One thought I had about the codebase, like you described, it was clearly part of some larger code base, and something that the person here did well is to already abstract away some of the details that were probably part of the original code. And you can see that in line 5 and line 12, they’re using the the ellipsis and here on line 24 again to just indicate that there’s some code in there, but it’s not really relevant to the question.

04:25 So they cut it out and just replaced it with an ellipsis. And that’s a good idea. It might not be necessary to show that there’s something left out if you actually refactor your code to a degree that it’s a minimal reproducible example, which we’re going to aim for in the course of this course.

04:45 But yeah, so this is a good way to reduce visual clutter if you want to do something quick and just cut out something, but you’ve got to take care that it’s then still running, which with the undefined variable in line 25 isn’t the case here.

04:58 Yeah, and I think you already had a good segue into the next part—unless this is part of your question to fix this, to need a solution for it, it’s good to not have errors in your code.

05:11 And I would say like, yeah, let’s hop into the next part where we actually have a look at the question now that we had a look at the code. Let’s see what the question is actually about.

05:21 Great.

Become a Member to join the conversation.