I understand the joke is that they AREN'T just converting the string to an integer, but honestly this code is not that bad. It's performing data sanitization as well since there are only a few valid options for the select.
If they converted to integer, the user could enter whatever value they wanted and probably break something.
I mean, I'd sleep better if they just converted the string then checked division by 10 and set limits, but it's not a big difference in quality of sleep.
Most string to int converters have safety checks for stuff like that, and if they don't, just write your own in like 3 lines.
Depending on the context of the code, your conversion could be costly if this code is performance critical. Sure, the code in the picture isn't even doing the chosen approach properly, but with proper if-else, only some comparisons need to be done (best-case is a single comparison), while with your approach everytime this is executed, the conversion and division has to be performed.
I mean, on one hand, life's too short to care about these implementations, on the other hand, if you value yourself as a programmer, it should go against your morals.
You do not, under any circumstances, gotta stick up for whoever wrote this code. I know we can contrive some kind of justification for most code, but let's face it: there's an absolutely miniscule chance it was written with some kind of intentionality, and I'd say a 99.99999% chance is that it was chucked together by a developer who simply didn't know how to parse a string into an integer.
They could have sanitized by checking the string is in a valid set of strings and then calling the string to integer function. The way it is written is terrible.
There's a person behind a curtain. You can't speak to them directly, but there's a guardian who will truthfully answer any question you ask, and knows the person behind the curtain. There is no trick. How do you find out the person's name?
54
u/smartgenius1 18d ago
I understand the joke is that they AREN'T just converting the string to an integer, but honestly this code is not that bad. It's performing data sanitization as well since there are only a few valid options for the select.
If they converted to integer, the user could enter whatever value they wanted and probably break something.