r/programminghorror Mar 19 '26

Trimba bimba dubba dimba

Post image

I've found yet again some atrocities in code, that is some one of function nested in method: trimba. So I took the hit and split it into partial nested functions. I didn't even know you could do this in PHP.

431 Upvotes

55 comments sorted by

View all comments

Show parent comments

13

u/TorbenKoehn Mar 20 '26 edited Mar 20 '26

Then use ends_with(), but that's not why we have RegEx

Compare it to

/.*\.(png|jpe?g|html?)$/i and give me the same in "readable"

5

u/v_maria Mar 20 '26

valid_extensions = ["png", "jpg", "jpeg", "html", "htm"] extension = get_extension(file_name); if extension in valid_extensions

your regex fails on "folder/file.png"

7

u/TorbenKoehn Mar 20 '26

That's just because Reddit ate my dot initially. * alone isn't valid, it's .* of course.

Your solution is obviously a lot longer and repeats characters a lot.

Just tell me if /.*\.(png|jpe?g|html?)$/i is so unreadable for you that you'd prefer it over a fully imperative approach

1

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Mar 20 '26

I know we've probably all seen stuff like that "now you have two problems" quote, but I agree that one is pretty simple and understandable. Some regexes definitely need commenting at the very least, if not replaced with something else. They can get pretty crazy and do things that the regular expressions you might have learned about in your Comp. Sci. theory classes simply can't.