I have some homework assigned to me in which I must parse a certain 'made up' language into Python. This code works perfectly for what the homework assignment is on and passes all tests but it would be highly beneficial if I am able to understand it more through the assistance of you guys. I have had a few mates assist me with this and therefore I would like help learning where I can make this code more efficient and if it's following basic coding principles. I am a first year computer science student.
#π Efficiency and Coding Principles?
33 messages Β· Page 1 of 1 (latest)
@worldly perch
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
There are quite some functions that are not included in this module you pasted
Are those defined somewhere, or did you get an explanation of what they do? @worldly perch
Like expect_token
They are defined in the code but that is provided to us
What you just pasted is provided by your prof?
Everything besides def parse_program(token_list):
everything in that function is mine
Alrighty
Well first of all, and this is out of your control, the provided functions and this module does some stuff that is a bit smelly
For example, there are global variables which are used throughout the functions. Global constants are OK, global variables are kinda bad
And then also, the values you provide to these functions as arguments are modified in the function body
Thus, they are not "pure functions"
The global variable I'm talking about is the cur_tok_index
One example of why this is bad, is let's say you want to run parse_program twice, the cur_tok_index will already be modified, so it will be out of index the second run.
Does this all make sense what I'm saying?
Yes, that makes perfect sense
Using global vars and mutating provided arguments in function bodies makes it very hard to read code
Because I cannot just look at 1 function, I will have to inspect all the rest of the code as well, as they are all interconnected now
Instead of properly separating the responsibilities, such that each function is responsible for doing 1 thing
The second more general feedback is that you wrote all the logic of the function directly into the main "parse_program" function
You could have made a function f.e.
def validate_header(token_list):
expect_token("ROBOT", token_list)
expect_token("IDENTIFIER", token_list)
expect_token("COLON", token_list)
expect_token("BEGIN", token_list)
And then replace those lines in parse_program with validate_header
I have to head out now but this makes a lot of sense- seems better to use, I'll definetly try incorporate such
The example is a bit nitpicky, but the idea is to use helper functions
a lot could be done. separation of logic, simplifying procedures, and extracting repeated lines (like stdio.writef("ERROR ...") ) .
@worldly perch a nice video, from a previous message of mine-
I'm a Never Nester and you should too.
Access to code examples, discord, song names and more at https://www.patreon.com/codeaesthetic
Correction: At 2:20 the inversion should be "less than or equal", not "less than"
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.