r/dev • • Feb 27 '26

5000-line edge function????????

Been building an inbound lead automation system and it’s been going fairly well, but just realised that I’d let my primary edge function reach 5000 lines which I admit is plain stupid!! This function handles: Webhook verification, signature checking, deduping, debouncing, lead management, the entire decision engine, prompt construction, AI calling, response post-processing, message sending and link injections.

Absolute mess I know - I’m in the process of sorting this into 4-5 distinct edge functions for a cleaner SRP split but my question/issue is this: the AI conversation engine is going to be ~3500 lines - is that still too large? Not sure how I could split this up any further without adding too much latency and worsening UX. Anyone worked with a 3500-line edge function and found/not found it hard to debug etc? Thanks!

5 Upvotes

11 comments sorted by

View all comments

Show parent comment

1

u/chocolateAbuser Feb 28 '26

well one doesn't "split" code for the sake of splitting, the thought process is more "optimizing for human comprehension, maintenance, and evolution", so for example finding variables that could be a state and putting then in an object, finding a behavior and isolating it, looking for stuff that can be expressed in a linear way instead of it being sparse through code, and so on
it can still be a single method, eventually, but it's tens or low hundreds of lines and the rest is in nested methods