Order Your Functions
I guess one more thing we can do here is we can move the
main() function to the top, after the class definition of the custom exception. That gives, if someone goes to read the code, that also gives them a quicker idea of what’s happening.
This, of course, would make more sense now if we had more functions, but I see this more as a little hint for maybe the original codebase, where you would want to give a quick overview of what’s happening. And in this case, the
main() function packages everything together that happens here.
00:31 And so it’s nice to have it up top so that it’s quick and easy to read.
And maybe this is also an interesting thing to talk about because people might not want to put the
main() function on the top because it’s calling the
validate() function. And in Python, the entry point of a script is always on the top.
So you might expect that you get a problem that
validate() isn’t defined yet, but when Python runs the script, it just defines the functions and it doesn’t run any code in here before you actually call any of those functions.
So there’s no issue here because we are running the
main() function only in line 17, and at that point, the
validate() function on line 12 is already defined.
main() knows what to do when this piece of code on line 9 finally runs.
01:18 Okay, so to verify this, you may run this again. Of course. So it’s not just your word.
Still going to run into the same error:
SourceEqualsTargetError. Okay. Things working the same.
Become a Member to join the conversation.