#πŸ”’ Efficiency and Coding Principles?

33 messages Β· Page 1 of 1 (latest)

worldly perch
#

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.

twin shaleBOT
#

@worldly perch

Python help channel opened

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.

worldly perch
boreal shard
#

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

worldly perch
#

They are defined in the code but that is provided to us

boreal shard
#

What you just pasted is provided by your prof?

worldly perch
#

Everything besides def parse_program(token_list):

#

everything in that function is mine

boreal shard
#

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?

worldly perch
#

Yes, that makes perfect sense

boreal shard
#

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

worldly perch
#

I have to head out now but this makes a lot of sense- seems better to use, I'll definetly try incorporate such

boreal shard
#

The example is a bit nitpicky, but the idea is to use helper functions

unkempt oasis
#

a lot could be done. separation of logic, simplifying procedures, and extracting repeated lines (like stdio.writef("ERROR ...") ) .

twin shaleBOT
#
Python help channel closed for inactivity

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.