So the bad code here is the "return True if a<= b else False", right? Okay. But what's the precedence variable and what does peek() do? Or is that not important for this post?
Around 12 years ago, I wrote a simple scripting language, and part of that scripting language involved an infix to postfix algorithm to convert the order of expressions.
Recently, I've been toying with the idea of inventing another programming language, so I've been brushing up on those topics. Infix to postfix was one of those topics I was brushing up on, and I discovered this code on this page. Check the Python section. There are a lot of things wrong with the code. Honestly, I think that the worst thing about the code is that everything that could have been in a function was placed in a class for some reason, so you have to create an object just to call the function on the class. It really makes no sense, but it came from geeksforgeeks, so you can't exactly expect it to be phenomenal code.
The try/except block is also ugly. Based on the contextual code, it would be an actual error for that KeyError to be encountered, and the fact that they choose to return False means that they are not properly handling that error.
I think the bad part is that obj.notGreater(3) should probably be more understandably written as obj.precedence[obj.peek] < obj.precedence[3] and the exception handled at a different level. That is keeping with the data model in the context of that program, but likely, something like the following could be written with a better data model: obj.precedence < precedence_level(3).
# Check if the precedence of operator is stricly
# less than top of stack or not
def notGreater(self, i):
try:
a = self.precedence[i]
b = self.precedence[self.peek()]
return True if a <= b else False
except KeyError:
return False
15
u/Phykin Dec 05 '21
So the bad code here is the "return True if a<= b else False", right? Okay. But what's the precedence variable and what does peek() do? Or is that not important for this post?
8
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Dec 05 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
5
u/RedditAcc-92975 Dec 05 '21
The real crime here is that comment.
1
u/patvdleer Dec 05 '21
You scared me there for a sec, Im a Python dev who used to worked at a company called Precedence
1
u/CaitaXD Dec 06 '21
W H Y ?
1
u/[deleted] Dec 17 '21
Oh no, 2 spaces before
<=!0
u/[deleted] Dec 06 '21
[deleted] — view deleted comment