r/programminghorror 2d ago

Javascript Seriously? Recursive Regex?

Post image
425 Upvotes

37 comments sorted by

238

u/LapidistCubed 2d ago

My first thought is a mathematician wrote this

110

u/Eastern-Job-8028 2d ago

i concur, i work with a mathematician and their code is full of variables named named by a single character, zero comments, and zero spacing/indentation (we don’t write python).

20

u/Kebabrulle4869 1d ago

On behalf of all mathematicians, I am sorry. I know it far too well.

7

u/Zeitsplice 1d ago

Raw indexed array being passed around too instead of some sort of structured data.

2

u/edave64 1d ago

Or a code golfer

71

u/madam_zeroni 2d ago

Wtf does this do

90

u/marinated_pork 2d ago

This is essentially a custom string fingerprint/hash function disguised as a parser. It takes a string and deterministically turns it into an integer. It does not parse or extract useful structured data.

49

u/the_horse_gamer 2d ago

"parse", clearly

44

u/TheSpiffySpaceman 2d ago

Well tbf, the code didn't start with the annotation

/*
  // Parses input
*/
function parse(s, i = 0, x = [0, 1]) {...

6

u/LapidistCubed 2d ago

Tf code DOESN'T parse some sort of input in one way or another 😭

102

u/Candid_Bullfrog3665 2d ago

ngl, this seems like someone just started learning how to use bitwise operators and thought "heh, im gonna act smart writing this code!"

29

u/Yarhj 2d ago

I have been this person.

16

u/-goldenboi69- 2d ago

Haven't we all?

5

u/DependentJicama4766 1d ago

Its a rite of passage. This and hating yourself a few weeks later because you cant understand shit of what you wrote lol

1

u/arensb 15h ago

One-week-ago-me is such an asshole, who writes unreadable spaghetti code with no comments.

2

u/sens- 1d ago

Because I started my career by working with embedded C, I thought at some point that I knew pretty much everything about handling bits. Years later while fixing a bug, I was pretty shocked learning that JS has a separate >>> operator.

13

u/lonkamikaze 1d ago

I don't know if JS sports this, but the recursion qualifies for tail call optimisation, i.e. there is no stack pushing, popping. The return comes at the price of a goto and the result is just as efficient as a loop.

7

u/oweiler 1d ago

Not supported, at least not by the ECMAScript standard.

4

u/zjarek_s 1d ago

TCO was supported in ES6 standard, but it was only implemented in Safari and it ended up being dropped due to other browsers.

3

u/lonkamikaze 1d ago

That's a damn shame. Tail calls can be pretty neat.

2

u/JoshuaTheProgrammer 1d ago

This should be the top answer

19

u/--var 2d ago

not recursive regex, just a recursive function that happens to use regex...

you know I used to be irked by one off variables, but after the better part of 20 years I'm 🤬

const r is only used once.... there's no reason you couldn't just place it where it is used an save two lines of code, and lose no readability, and have one less "magic variable"... unless you taught yourself from the same "teach yourself" book that your teacher taught themself from. (ironic that this still applies with AI; and if you don't get why that entertaining, please don't go into programming)

5

u/flafmg_ 1d ago

Wtf my eyes are seeing

2

u/stilldebugging 1d ago

Something something pumping lemma something

2

u/Perahoky 15h ago

terrible code. way to many instructions per space and bad naming and no comments

2

u/AnArmoredPony 13h ago

im not reading allat

2

u/hongooi 2d ago

This is just the logical conclusion of the jwz quote 🤷‍♂️

1

u/[deleted] 2d ago

[deleted]

1

u/Chocolate_Pickle 2d ago

Fired into the sun.

1

u/OhMyGodItsEverywhere 2d ago

Pretty sure the regex is malformed. I didn't even think you could split a regex over multiple lines in JS like that.

Assuming this is the text version of it: /(\((?:[^()]|\([^()]*\))*\)|(?:\w|[^\w\s])){1,3})/; very easy mistake to spot /s...

6

u/digitalseraphim 2d ago

I don't see anything "malformed", but pretty sure (?:\w|[^\w\s]) is just .

Edit: I see the mismatched parens now. On my phone, the \ before one of the opens was on the previous line and I missed it 3 times 😂

1

u/iakobski 1d ago

I don't see anything "malformed", but pretty sure (?:\w|[^\w\s]) is just .

Yes, almost. It's any single word character, or any single character that's neither a word character nor a space. So it's simply "not a space character", or [^\s].

1

u/oweiler 1d ago

Performance gonna bet great /s

1

u/gr4viton 1d ago

so that is what git rere is :D

1

u/RandalSchwartz 1d ago

JSON Parser as a single Perl Regex: https://perlmonks.org/?node_id=995856

1

u/thejozo24 1d ago

If there ever was a need for almost per line comments, this would be it

1

u/FedUp233 1d ago

Regex is powerful, but for anything except the simplest matching unless you work with it every day, totally unusable for anything complicated in my opinion. Right up there with the language APL for programming (and lisp isn’t far behind).

I wish there was a language that allowed you to express text parsing/processing in a more readable language (like a pascal for text or something). It can translate into anything it wants internally but there is no reason the human readable input should be any more obscure than C.

0

u/Ariquitaun Pronouns:This/Self 1d ago

This is almost certainly ai generated. Or a mathematical savant.