View Full Version : c++: general question about functions
Karim
04-18-2005, 06:23 PM
I've been told that functions should not be >30 lines. Ideally, they should accomplish one well-defined task and should fit onto one screen.
Are there exceptions?
Of course, my design skills bite and I inevitably can only see a way that is too complicated. I'm just hoping to pass my last assignment and this course and maybe eventually get better at streamlining/efficiency with more experience.
//rant on
Then again, it's hard to compare yourself with guys who are in industry coming back to class for a degree or guys who are taking it for the second or third time...
//rant off
sabotai
04-18-2005, 06:30 PM
I've been told that functions should not be >30 lines. Ideally, they should accomplish one well-defined task and should fit onto one screen.
Wishful thinking. There's a point where elegence can be detrimental to a program, and people who force their functions to be small and just do one thing and only one thing go way passed that point.
Of course, you've told us about your professor before. If he wants them to all be less then 30 lines, then you better do it. You shouldn't be coding anything for a class that would need large functions anyway.
But the real world doesn't work like that.
EDIT: Just to clarify. MOST of the functions I code are probably less than 30 lines, or around that point. And a good chunk of those are probably less than 10. But yeah, there are plenty of issues you need to deal with that will probably take functions larger than 30 lines. Enough that I would not call them "exceptions". Enough that I don't follow any rule on function size.
Mr. Wednesday
04-18-2005, 07:01 PM
Guys who are coming back from industry are unlikely to do very well adhering to academically-imposed guidelines. :p I've never been very good at keeping the length of my functions down (in any language).
In a number of the things that I've coded, saying that the functions would all have to be less than thirty lines would be absurd. When you're doing something complex, there's only so much reduction of complexity that can be done; a process with more than thirty steps is going to take a function with more than thirty lines to get it done.
Greyroofoo
04-18-2005, 08:24 PM
yes, a function should do one defined job. However use as many lines as you need to make it work AND to make it UNDERSTANDABLE
Radii
04-18-2005, 10:20 PM
Spacing, comments and readability all trump line length and being on one page. Yes, keep your function to one task, but if that task is complicated you're sitll going to have some damn long functions.
But if your professor told you that, then for that class, he's right. ;)
Marc Vaughan
04-19-2005, 08:14 AM
I've been told that functions should not be >30 lines. Ideally, they should accomplish one well-defined task and should fit onto one screen.
Are there exceptions?
Yes numerous ones - bear in mind however that these comments are biased by my own experience and preference:
Where possible functions should be kept succinct and reasonably short (and most importantly focused on the task involved), however if there is a sensible reason for having a lengthy function and it isn't sensible to cut it down into sub-functions (either because there isn't any percieved sub-functionality or for optimisation reasons) then having a function over 30 lines long is perfectly acceptable and indeed in practice you'll often find this is the case.
For instance in FM there are numerous state engines which have around or over 20 states in them, just that alone will bounce any function which maintains these states out of this rule-set.
Also imho keeping functions short for the sake of short can often lead to unreadable code, especially if someone is trying to 'force' a function to be short by splitting it into arbitary sub-functions.
(just my tuppence worth)
Marc
vBulletin v3.6.0, Copyright ©2000-2026, Jelsoft Enterprises Ltd.