Rename Your Symbols
00:00 Okay, now with having cleaned up the code a bit more and actually answered the question, let’s do a little bit of more refactoring because there are a few instances in this code where we can optimize the code even a bit more.
00:14 It might not be particularly needed to get an answer to your question, but it’s still nice to end up with a code example that might be a bit more best practice. So Martin, what’s something that catches your eye right away where you would do a little bit of refactoring?
The first thing that’s been bugging me since I’ve wrote it here is this
X. This is definitely not a very useful name for this minimal code example that we have now. So I will just call it
user_input. Sure. Yeah, I think
user_input is fine.
user_input. We don’t really know what what it does.
So if I would name this here
input, then I’m actually overriding this. So you definitely don’t want to do that. Yeah. So the code would still work, but if you know that
input is a built-in function, it’s good to adjust a little bit, for example, to
user_input to not override the built in function.
And so what I can do here, actually I can just go and use something that the code editor provides, and most of code editors do that. I can, in VS Code, right-click after highlighting the variable that I want to change and click on Rename symbol, and then I can get this pop-up here and can write
And then you see, like, it didn’t refactor it in here because here we’re in a different scope, in the
main() function. This is the parameter that’s passed to
main(), so we want to refactor that too.
main() function takes
user_input, and then the
load_model() function takes two values from the
"s" and one
"t", currently. But I’m also not very happy with those. And here, luckily we have some context here—it’s
"target". It’s a little more descriptive. Okay, so now you have a dictionary with a descriptive name, and inside of the dictionary you have two keys—
"target"—that before were
"t", and now it actually has a verbose name where it’s easier to understand what the value should be of them. So currently in line 13,
"source" has a value
"target" has a value
0. Right, and that means for us that this is going to throw the error, so let’s look at the error next.
Become a Member to join the conversation.