Live data from Hacker News

Ask HN: How do you deal with 2000 line functions?

news.ycombinator.com

1–10 of 72 posts

Ask HN: How do you deal with 2000 line functions?

#1
I'm working with an archaic code base and I'm stuck at trying to add new business logic into a 2000 line function that is absolutely crappily written with nothing but if() conditions in it. Given such a scenario, how do I go about adding a new functionality to this mess? Should I tell my manager that the function needs to be refactored and take time to refactor it?

Edit: Removed offensive word

Re: Ask HN: How do you deal with 2000 line functions?

#5

Yes, just don't use the word "shit". The obvious refactoring is to split the function up into smaller pieces, even if you can split it into two 1500 line functions that is a win.

Thanks, that came out of frustration from looking at similar long-sized functions for an extended period of time.

Re: Ask HN: How do you deal with 2000 line functions?

#6
post #4

You test the shit out of it, then re-write it ensuring tests pass, then add your new code. It's probably easier to do that than to muck around in the 2000-line version hoping you don't screw something else up.

Makes sense. So step #1, get 100% coverage for that thing.

Re: Ask HN: How do you deal with 2000 line functions?

#8
post #4

You test the shit out of it, then re-write it ensuring tests pass, then add your new code. It's probably easier to do that than to muck around in the 2000-line version hoping you don't screw something else up.

Yep, this is the only way to handle these beasts. Don't touch anything. Make a ton of tests. Spend some time validating those tests. Get someone to write off that those tests are sufficient. Start refactoring!
Post reply on HN