Live data from Hacker News

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

news.ycombinator.com

21–30 of 72 posts

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

#21
Don't tell you manager anything, just do it. If someone tells you to add functionality to a 2000 line function, it takes extra time. If you want the company to benefit a little bit more from the time you spend on grokking it (by saving the next poor soul some time) by splitting it up.

Go to the part that you suspect you need to modify, identify a block of code that together forms something you can give a name, i.e. "initialize_doodad_struct", "validate_widget_params", pull it out put it in a function. Now look at all identifiers that are referenced or declared in your block of code, do a ack or ctrl+f through the 2000-N line function to 100% verify that they're not referenced anywhere. If they aren't you can safely encapsulate them in your new function (if you're working in a static language you probably could just right-mouse-click-extract to do all this).

Then immediately verify that the code still works. Run it, ideally in unit tests but most certainly also in real world(-like) scenarios. Do this for every small step. If your codebase takes 2 hours to compile, instead make a commit. Make a commit for every single 3-10 line extraction you do. (do it anyway, it'll look good and everyone will love you)

And then repeat until either the part you wanted to modify is clear to you, or there's nothing more to extract ;)

By the way, I'm a Ruby programmer mainly, and in Ruby any function over 10 lines is frowned upon. I know that in some programming language communities (looking at you C and C++!) a 200 line function doesn't raise any eyebrows, and they'll sometimes even scold you for extracting a function if it doesn't 'add' anything. I think this is because in C every function pollutes the global namespace, so there's a little more cost associated with it.

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

#23

1. Unit tests. Otherwise modifying old code is full of potentially unforeseen landmines. 2. Read the function a few times to get a sense of what it's doing. 3. Break the function into blocks of statements that are performing a related task. 4. Factor those out into smaller functions. 5. Recursively repeat 3-5 until you're satisfied with the new functions' line count. > Should I tell my manager that the function needs…

Unit tests might be hard on a function like this and you might have to admit that your tests are more integrationey than you'd like.

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

#24

1. Unit tests. Otherwise modifying old code is full of potentially unforeseen landmines. 2. Read the function a few times to get a sense of what it's doing. 3. Break the function into blocks of statements that are performing a related task. 4. Factor those out into smaller functions. 5. Recursively repeat 3-5 until you're satisfied with the new functions' line count. > Should I tell my manager that the function needs…

I would add assertions in as well as you iterate. It will validate whether you understand the flow of information through the function.

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

#26
I would actually approach this differently. I'd go back to whomever the authority is with regards to what the function is supposed to do, or to the spec, and then rewrite it from scratch. This won't ensure that it behaves as it does now but with a little bit added - it will ensure that it behaves as per the specification / user requirement. If the function is that huge, with just if's, it's very likely that it's buggy and that those bugs haven't been addressed. They won't be addressed if you duplicate the functionality in a refactor unless you can get a handle on the purpose of the thing to begin with. Scrapping it and rewriting it is not a bad thing - frequently, you'll make something much tighter if you do that.

(note: would be happy for you to have left the "offensive" word in - 2,000 lines of code for a single function screams for a refactor, and says that the original dev had no clue how to write good code. if the code makes you want to cuss, well....)

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

#28
post #7

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.

Is swearing on HN banned now?

No, but a large part of the community is in the US where many people (not all) take offense to even mild swear words (in contrast to for example the UK where even politicians won't hesitate to call someone a wazzock).

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

#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 () {
            
            }
        }
    }

}
Post reply on HN