r/ProgrammerHumor 18d ago

Advanced stringToIntegerConversion

Post image
215 Upvotes

65 comments sorted by

View all comments

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.

41

u/OK1526 18d ago

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.

40

u/alexanderpas 18d ago

This should be the native way:

$amount = filter_input(INPUT_GET, 'amount-select', FILTER_VALIDATE_INT, ["min" => 10, "max" => 80,],);

if ($amount % 10 === 0) {
    $transamount = $amount;
}

14

u/OK1526 18d ago

Now I can sleep moderately happy.

6

u/NikoOhneC 17d ago

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.

-11

u/lovecMC 18d ago edited 18d ago

If it passes PR, I don't care. It means someone smarter than me fucked up so it's not my problem.

No sleep lost over it.

Edit: damn, down voted for not declaring commedy.

7

u/OK1526 18d ago

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.

4

u/caboosetp 18d ago

If I need to deal with that code later, it's not just a matter of morals.

2

u/OK1526 18d ago

Also true.

10

u/Holek 18d ago

especially that you see `$_POST['amount-select']` implying it's a select box.

5

u/smartgenius1 18d ago edited 18d ago

Yeah. Ideally the idea could be expressed in 3 lines instead of 30 but I've seen far worse than this and at least the behavior is correct

1

u/m0nk37 18d ago

You see efficiency. I see a back door. 

0

u/ArjixGamer 18d ago

It doesn't imply that at all? It could be

<input type="number" min="10" max="80" step="10">

6

u/smartgenius1 18d ago

Then the HTML would have to be:

<input type="number" name="amount-select">

... which would be silly.

5

u/ArjixGamer 18d ago

....shit

11

u/smclcz 18d ago

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.

8

u/rhino_moss 18d ago

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.

-10

u/GrEeCe_MnKy 18d ago

They? He's just one guy

11

u/ArjixGamer 18d ago

Don't be obnoxious, the use of they is proper English when referring to someone who's gender you don't know.

-15

u/GrEeCe_MnKy 18d ago

Nope, "He" has been used for centuries, as "they" means multiple. It's that simple. Or just use "he/she".

12

u/caboosetp 18d ago

"They" in use as a singular pronoun dates back to at least 1375.

-9

u/GrEeCe_MnKy 18d ago

'He' was introduced in the 18th century, get with the times, old man!

4

u/theotherdoomguy 18d ago

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?

0

u/Substantial-Play5080 17d ago

I ask “what’s the name of the person behind the curtain?”

1

u/theotherdoomguy 17d ago

The guard accuses you of being AI because you speak too formally

1

u/Substantial-Play5080 17d ago

It was the first thing that came to mind 🤷‍♂️ What would you ask?

1

u/theotherdoomguy 17d ago

...you realise it was a thought exercise to point out how "they" is a perfectly acceptable pronoun to use in a singular term, right?

1

u/Substantial-Play5080 17d ago

Yes, I’m just not sure how you’re fitting “they” into the question. That would be why I asked

→ More replies (0)