![]() |
|
|
#1 | ||
|
College Starter
Join Date: Oct 2000
Location: Calgary
|
c++: general question about functions
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 |
||
|
|
|
|
|
#2 | |
|
General Manager
Join Date: Oct 2000
Location: The Satellite of Love
|
Quote:
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. Last edited by sabotai : 04-18-2005 at 06:34 PM. |
|
|
|
|
|
|
#3 |
|
Pro Starter
Join Date: Jul 2003
Location: South Bend, IN
|
Guys who are coming back from industry are unlikely to do very well adhering to academically-imposed guidelines.
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.
__________________
Hattrick - Brays Bayou FC (70854) / USA III.4 Hockey Arena - Houston Aeros / USA II.1 Thanks to my FOFC Hattrick supporters - Blackout, Brillig, kingfc22, RPI-fan, Rich1033, antbacker, One_to7, ur_land, KevinNU7, and TonyR (PM me if you support me and I've missed you) |
|
|
|
|
|
#4 |
|
Pro Starter
Join Date: Nov 2003
Location: Alabama
|
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
|
|
|
|
|
|
#5 |
|
Head Coach
Join Date: Jul 2001
|
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. ![]() |
|
|
|
|
|
#6 | |
|
SI Games
Join Date: Oct 2000
Location: Melbourne, FL
|
Quote:
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 Last edited by Marc Vaughan : 04-19-2005 at 08:15 AM. |
|
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|