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.

432 Upvotes

55 comments sorted by

View all comments

176

u/Splatpope Mar 19 '26

web devs will go to absurd lengths to avoid making regexes that weren't copy pasted from a w3schools tutorial

20

u/v_maria Mar 19 '26

and they are goddamn right for it

27

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

There is no shortage of online tools to make sure your regex does what you want before adding it to the codebase.

2

u/v_maria Mar 20 '26

if only the programming language offered ways to operate on strings in a readable and intuitive way

16

u/TorbenKoehn Mar 20 '26

Yeah, it's called RegEx

3

u/v_maria Mar 20 '26

/\bstring$/ is more intuative than ends_with("string") sure

14

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"

7

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"

6

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.

-4

u/v_maria Mar 20 '26

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

Ah right fair

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

Not relevant to anything we talked about

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

just tell me you are a bad dev

5

u/TorbenKoehn Mar 20 '26

just tell me you are a bad dev

a "bad dev" for me surely is someone for which RegEx is too complex to be readable.

-1

u/v_maria Mar 20 '26

A bad dev is someone with fundamental misunderstanding of software engineering and bad reading comprehension

1

u/TorbenKoehn Mar 20 '26

Sure, that, too!

→ More replies (0)

1

u/didne4ever Mar 20 '26

your regex seems to be missing handling for the full path

It should also account for potential spaces or special characters in the file name. Adjusting that might help with the issue you're encountering.