TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: Functions: how long is too long?

4 pointsby blintsonover 15 years ago
First I was going to ask how long you think a function should be, but there are lots of cases where using short functions is convenient.<p>Instead I'd like to ask what's the longest you'll let a function get in a given language before you break it up? Why?<p>I have very little experience coding commercially, but I try and never let any function get &#62;1 screenfull, which is 50 lines for me.

8 comments

jacquesmover 15 years ago
Too long is when you need more than once sentence to describe what a function does or when the function name gets too long.<p>That's a pretty good indicator because it shows that you could 'abstract' something out and make it an individual routine.<p>Err on the side of caution, better to have smaller functions than larger ones, unless you have a very good reason not to.<p>And if performance is your worry and you work in 'C' then inline is your friend.
cpercivaover 15 years ago
I don't find that it's useful to have any limits on the <i>length</i> of functions; I have code with functions (even main!) are hundreds of lines long.<p>I do, however, find that it is essential to limit the <i>width</i> of functions, and if my code doesn't comfortably fit into 80 columns when using 8-column tabs, there's a clear sign that it needs to be broken up.<p>In general, nested loops are far more of a problem than simple "do X, then do Y, then do Z" code.
评论 #1026971 未加载
Groxxover 15 years ago
A general rule I hear is screen +~50%. Once you go beyond that, a study I read a while ago (apologies, no links) showed a huge leap in error, they believed stemmed from our difficulty remembering complex / random things. With my own testing, I'd say it fits pretty close. On-screen is pretty limiting, but 2x screen gets harder to be <i>certain</i> about <i>everything</i> in it, all the time. My error ratio is definitely best when I keep my functions below 2x screen height.<p>That, and once you get into the hundred+ lines of code, you are probably doing something that's more easily summed up as a couple pieces. Using multiple pieces also has the advantage of it being very easy to rapidly change / experiment with your code.
yannisover 15 years ago
It is probably too long at 50 lines. Turn the question around, what must the function return and start from there. The function should do no more than necessary to achieve the goal stated as the return of the function.
评论 #1026948 未加载
chaosprophetover 15 years ago
Personally, I tend to restrict functions to 1 screen length. This is not based on any good programming practices list, it's just that I like being able to see the entire function in one view.
kristianpover 15 years ago
I'm not saying this is right, but at my place of work, our c# coding standard says a maximum of 25 lines. It also says maximum line length of 110 spaces (including 4 character tabs).
bemmuover 15 years ago
If the function starts to feel difficult to work with, then it's too long.
wendroidover 15 years ago
I still aim for the old school<p>7 +- 2<p>not including declaratons