Live data from Hacker News

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

news.ycombinator.com

61–70 of 72 posts

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

#63
post #59
post #58

Earlier quoted context omitted.

> and in Ruby any function over 10 lines is frowned upon Citation required.

Sorry you're right. It's 5 lines: https://robots.thoughtbot.com/sandi-metz-rules-for-developer...

For certain web apps, maybe that can work. Not for anything slightly more complex though.

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

#64
post #63
post #59

Earlier quoted context omitted.

Sorry you're right. It's 5 lines: https://robots.thoughtbot.com/sandi-metz-rules-for-developer...

For certain web apps, maybe that can work. Not for anything slightly more complex though.

Needing to be longer than five lines? What's your limit?

I can see some view functions being a bit more, but that's boilerplate.

A function should do one thing. It's really, really hard to do one thing in more than a few lines.

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

#65
post #29

Something I found to be really useful is to remove all statements inside the code, so you'll be left with large skeleton structures. Look at it from a distance, (small font), see some patterns. Can you split it up? getting a feel for the structure of the function helps me understand it. Ex: function someFunction () { if () { for () { if () { } else { } } if () { for () { } } } }

Ah I do this with if() block folding in Vim. In this case, the developer actually has return statements inside some if blocks. A general point that has stood out from all comments is to first understand how the function achieves what the function is trying to achieve.

A return point inside an if statement is a great place to do a refactor. Refactor anything inside that if into its own function, write a test, and see what happens :)

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

#68
post #20

Earlier quoted context omitted.

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

Er... am I misunderstanding or is everyone else ? Tell your manager that you want to refactor it. Just don't tell your manager "it's shit". The "don't say it's shit" was when speaking to the manager... not using it on your thread. Unless I'm the one misunderstanding the statement...

Both, but more to the boss. I can't stand the whole "Get Shit Done" thing, the way that word gets used on HN is like a universal qualifier, i.e.

ForAll(x): Shit(x)

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

#69
post #63

Earlier quoted context omitted.

For certain web apps, maybe that can work. Not for anything slightly more complex though.

Needing to be longer than five lines? What's your limit? I can see some view functions being a bit more, but that's boilerplate. A function should do one thing. It's really, really hard to do one thing in more than a few lines.

Just take a look at any OS/graphics/machine learning/etc. codebase.
Post reply on HN