Thread: When does a func() become too large?

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    When does a func() become too large?

    I have a function that performs a lot of floating-point arithmetic. The function basically calculates coordinates on a fractal-type design of sorts. The function is called 3^n times where n is the number of iterations (consequently, the overall complexity of the fractal).

    Now I understand the merits of modularizing things as a programmer, but my question is, at what point should the function be split into smaller functions, if the number of calculations within the function as it is will never change?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    When it is bigger than one screen in hight
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by vart View Post
    When it is bigger than one screen in hight
    Ha, is that the rule of thumb? I was kind of hoping for a more insightful answer than that.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by dudeomanodude View Post
    Ha, is that the rule of thumb? I was kind of hoping for a more insightful answer than that.
    Then you should go and find yourself some good coding standard manual. And read it n the topic
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But you won't find much more than this rule of thumb. A function should be so large that it can still be easily understood, but how easily a function is understand depends heavily on what it contains.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I would also probably add the fact that a function shouldn't add functionality beyond that of which it was created for... (or some other form of this sentence. I'm sure there is a better way of saying it)
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would also probably add the fact that a function shouldn't add functionality beyond that of which it was created for... (or some other form of this sentence. I'm sure there is a better way of saying it)
    A function should do one thing and do it well.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    well like I said, it performs a lot floating-point arithmetic, it contains no loops, no if-else statements, it's just a procedural calculating function, that's it. I think it's easy enough to follow, especially since there are no control structures. Also, no calculations are repeated.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, you should consider: are there portions of this function that can be better expressed (and maybe reused) as a unit by moving them out to another function?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by laserlight View Post
    Well, you should consider: are there portions of this function that can be better expressed (and maybe reused) as a unit by moving them out to another function?
    OK, as a for instance, my function uses the distance formula, but only once. You might say that the distance function is a rather robust calculation. Is it robust enough to stick it in its own function, though?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dudeomanodude View Post
    OK, as a for instance, my function uses the distance formula, but only once. You might say that the distance function is a rather robust calculation. Is it robust enough to stick it in its own function, though?
    The advantage of putting it in its own function is that it can also be used elsewhere; do you need the distance formula elsewhere in your program? Do you need the distance formula elsewhere in some other program (written or yet unwritten)? Probably, yes; so it might not be a bad idea to have it in an easily movable state. (People tend to end up with files of functions like this that they can import as necessary.)

    Another advantage is that the code becomes self-commenting: when you see
    Code:
    d = distance(point_1, point_2);
    it's pretty obvious what's going on, more so than the actual calculation itself would be.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    Well, if you have a piece of code in the function that you think can be used independently, then cut it out. I think the distance function could be cut out (even if you don't use it in this specific program, you'll have it ready for others).
    I might not be a pro, but I'm usually right

  13. #13
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Hey thank you guys, I realize the subject of this thread may boil down to personal preference, and might also rely on a situational basis, but I think I might make better decisions now on how to modularize things.

    Always good to get the consensus of the experts here!
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Seeing as the compilers nowadays are pretty darn good at figuring out if something should be inlined or not [and even without an inline keyword], there's really no reason to NOT split things into simpler functions if there's any scope to do that.

    A long set of calculations, with or without conditoons and such, will be clearer if each SMALL part of that calculation is separated out into smaller functions.

    If you find later that this is REALLY time-critical calculations, and could do with optimizing, then you may consider re-writing the function in a more optimal way [assuming there is such a thing].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by matsp View Post
    If you find later that this is REALLY time-critical calculations, and could do with optimizing, then you may consider re-writing the function in a more optimal way [assuming there is such a thing].

    --
    Mats
    Funny you should say that, this is actually a school assignment, with the point being that after so many iterations, the computer can no longer efficiently handle the task. Right now I'm just trying to get it to work, but once that's done, I'm gonna have to go through and start counting operations to optimize the procedure.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [Large file][Value too large for defined data type]
    By salsan in forum Linux Programming
    Replies: 11
    Last Post: 02-05-2008, 04:18 AM
  2. Representing a Large Integer with an Array
    By random_accident in forum C Programming
    Replies: 3
    Last Post: 03-03-2005, 08:56 PM
  3. typedef a func
    By trekker in forum C Programming
    Replies: 4
    Last Post: 07-02-2002, 05:15 AM
  4. Delete & print func()
    By spentdome in forum C Programming
    Replies: 5
    Last Post: 05-29-2002, 09:02 AM