• Advertisement

Archived

This topic is now archived and is closed to further replies.

is this function too long?

This topic is 5996 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

now i heard it is bad programming practice to have functions that are way tooo long now i have a function in my game that is exactly 512 lines ( that is like soo much to me) is it too long? should i make it shorter some how? heh that function has more lines then i ever coded before thx

Share this post


Link to post
Share on other sites
Advertisement
Yeah, that''s a bit too long. I can''t remember what the ''optimal'' function length is according to Code Complete, but I know it''s much shorter than 512 lines .

~~~~~~~~~~
Martee

Share this post


Link to post
Share on other sites
lol i havent seen you in a while Martee

yea i thinks its quite long also but with the way the function is set up i can''t think of a way to shorten it without screwing it up

Share this post


Link to post
Share on other sites
If theres a way to make your function smaller and just as accurate, whether or not it is standard, do it. It will be much easier to check for errors.

Share this post


Link to post
Share on other sites
i honestly can''t think of a way to shorten it

you want me to postt it here and see if any of you guys can think of a way

Share this post


Link to post
Share on other sites
Martee:

If functions cannot have more than X amount of lines of code... int main() is a function, your saying that it can only have a certain amount of code?

Just a thought, theres probably some exception rule...

Share this post


Link to post
Share on other sites
maybe i should explain what the role of the function is

its role is to let the user decide how many skill points to put in the 12 skills

i would have to split up the huge switch statement( the function is basically a switch statement)

Share this post


Link to post
Share on other sites
Could you make a class rather than a function, one class for each skill, or a class called monster and have it have each skill as a variable?

Making it as a class or struct would be a good idea.

Then you can call

cout<<"What is your HP?";
cin>> myMonster.hitPoints;

In a function called checkStats()

Share this post


Link to post
Share on other sites
lol everything is in a class

i dont use variables unless they are in classes

i have variables for the attack skills and the defense skills


plz do keep in mind that im not a newbie and i do know how to use C++

Share this post


Link to post
Share on other sites
uhhh the way i use comments is not exactly standard..

instead of putting them on the side i put them on top

heres a portion of the code so you can see what i did...

//This function is called when the user wants to distribute skill points
void Monster::DSPnts()
{
//This holds the users choice
short unsigned choice;

//This holds how many points the user wants to put in a category
short unsigned points;

//Ask the user which category to distribute points into
cout <<"\n\nPlease choose the category you would to put points into:" <
//Write all 12 categories to the screen
cout <<"1)Fire Attack" < cout <<"2)Water Attack" < cout <<"3)Electricity Attack" < cout <<"4)Air Attack" < cout <<"5)Psycic Attack" < cout <<"6)Omni Attack" < cout <<"7)Fire Defense" < cout <<"8)Water Defense" < cout <<"9)Electricity Defense" < cout <<"10)Air Defense" < cout <<"11)Psycic Defense" < cout <<"12)Omni Defense" <
//Ask the user to input a number
cout <<"Please chose a number- ";
cin >>choice;

//Use a switch statement to check what category the user picked
switch(choice)
{
//If the user picks number 1
case FireA:
{
//Ask the user how many points he/she would like to put into fire attack
cout <<"\n\nHow many points will you put into fire attacks?- ";
cin >> points;

//If the number is less then or equal to the current amount of points and more then 0
//add it to fire attack
if (points <= SPnts && points > 0)
{
//Add the user defined points to fire attack
FDmg+=points;

//Subtract the points from the current amount of skill points
SPnts-=points;

//Break from the switch
break;
}
else
{
//If the user picked a number too high, tell him/her, then break
if (choice > 12)
{
cout <<"\n\nThe number you picked is too high." < system("PAUSE");
break;
}

//If the user picked a number too low, tell him/her, then break
if (choice < 0)
{
cout <<"\n\nThe number you picked is too low." < system("PAUSE");
break;
}
}
}

//If the user picks number 2
case WaterA:
{
//Ask the user how many points he/she would like to put into fire attack
cout <<"\n\nHow many points will you put into water attacks?- ";
cin >> points;

//If the number is less then or equal to the current amount of points and more then 0
//add it to water attack
if (points <= SPnts && points > 0)
{
//Add the user defined points to water attack
WDmg+=points;

//Subtract the points from the current amount of skill points
SPnts-=points;

//Break from the switch
break;
}
else
{
//If the user picked a number too high, tell him/her, then break
if (choice > 12)
{
cout <<"\n\nThe number you picked is too high." < system("PAUSE");
break;
}

//If the user picked a number too low, tell him/her, then break
if (choice < 0)
{
cout <<"\n\nThe number you picked is too low." < system("PAUSE");
break;
}
}
}

//If the user picks number 3
case ElectricityA:
{
//Ask the user how many points he/she would like to put into fire attack
cout <<"\n\nHow many points will you put into electricity attacks?- ";
cin >> points;

//If the number is less then or equal to the current amount of points and more then 0
//add it to electricity attack
if (points <= SPnts && points > 0)
{
//Add the user defined points to electricity attack
EDmg+=points;

//Subtract the points from the current amount of skill points
SPnts-=points;

//Break from the switch
break;
}
else
{
//If the user picked a number too high, tell him/her, then break
if (choice > 12)
{
cout <<"\n\nThe number you picked is too high." < system("PAUSE");
break;
}

//If the user picked a number too low, tell him/her, then break
if (choice < 0)
{
cout <<"\n\nThe number you picked is too low." < system("PAUSE");
break;
}
}
}

//If the user picks number 4
case AirA:
{
//Ask the user how many points he/she would like to put into fire attack
cout <<"\n\nHow many points will you put into air attacks?- ";
cin >> points;

//If the number is less then or equal to the current amount of points and more then 0
//add it to air attack
if (points <= SPnts && points > 0)
{
//Add the user defined points to air attack
ADmg+=points;

//Subtract the points from the current amount of skill points
SPnts-=points;

//Break from the switch
break;
}
else
{
//If the user picked a number too high, tell him/her, then break
if (choice > 12)
{
cout <<"\n\nThe number you picked is too high." < system("PAUSE");
break;
}

//If the user picked a number too low, tell him/her, then break
if (choice < 0)
{
cout <<"\n\nThe number you picked is too low." < system("PAUSE");
break;
}
}
}

//If the user picks number 5
case PsycicA:
{
//Ask the user how many points he/she would like to put into psycic attack
cout <<"\n\nHow many points will you put into psycic attacks?- ";
cin >> points;

//If the number is less then or equal to the current amount of points and more then 0
//add it to psycic attack
if (points <= SPnts && points > 0)
{
//Add the user defined points to psycic attack
PDmg+=points;

//Subtract the points from the current amount of skill points
SPnts-=points;

//Break from the switch
break;
}
else
{
//If the user picked a number too high, tell him/her, then break
if (choice > 12)
{
cout <<"\n\nThe number you picked is too high." < system("PAUSE");
break;
}

//If the user picked a number too low, tell him/her, then break
if (choice < 0)
{
cout <<"\n\nThe number you picked is too low." < system("PAUSE");
break;
}
}
}

Share this post


Link to post
Share on other sites
hmm the ''endl'' dont show up

hope it showed up now..

my code is much more cleaner then wat the dam post made it look like

Share this post


Link to post
Share on other sites
If you put the comments on the side there wont be so many lines, for starters. Ill see if I can find a way to shorten it.

"I''ve sparred with creatures from the nine hells themselves... I barely plan on breaking a sweat here, today."~Drizzt Do''Urden

Share this post


Link to post
Share on other sites
heh i hate putting the comments on the side, it annoys me

i should really thank you guys for helping me after all the posts i make and the stupidity i show

Share this post


Link to post
Share on other sites
I put the comments on top, too, but I don''t have 500 line functions

I think the right thing to do is this:

int SetFireAttack()
{
//Ask the user how many points he/she would like to put into fire attack
cout <<"\n\nHow many points will you put into fire attacks?- ";
cin >> points;

//If the number is less then or equal to the current amount of points and more then 0
//add it to fire attack
if (points <= SPnts && points > 0)
{
//Add the user defined points to fire attack
FDmg+=points;

//Subtract the points from the current amount of skill points
SPnts-=points;
}

SO each attack has its own small function to call in the case statements so:

case FireA:
{
SetFireAttack();
break;
}




"I''ve sparred with creatures from the nine hells themselves... I barely plan on breaking a sweat here, today."~Drizzt Do''Urden

Share this post


Link to post
Share on other sites
If you want to make it really neat, yu can make a header file that holds every SetAttack function..



"I''ve sparred with creatures from the nine hells themselves... I barely plan on breaking a sweat here, today."~Drizzt Do''Urden

Share this post


Link to post
Share on other sites
wow that really shows how stupid i am

dam drugs, i knew they would mess me up...

thx dude, i love u lol

Share this post


Link to post
Share on other sites
i would do that but where would i include them?

in the top of the main header file( where the class i declared is)?

Share this post


Link to post
Share on other sites
Actually, you just add the .h file to your project and call the function.. it recognizes it as if it was part of the .cpp. You are using MSVC++ right? I am pretty sure thats how it works..


You just type them any where in the .h file

"I've sparred with creatures from the nine hells themselves... I barely plan on breaking a sweat here, today."~Drizzt Do'Urden

Edited by - Drizzt DoUrden on September 23, 2001 8:31:27 PM

Share this post


Link to post
Share on other sites
I strongly advise that you break up that function into ''sections'', where each case statement would have its very own, private function Also Drizzt DoUrden, the idea of placing comments on the side of code, is really not such a good idea (in my opinion) I really dont want to type anymore to explain why, as of me being tired, so tata

Share this post


Link to post
Share on other sites
I strongly advise that you break up that function into ''sections'', where each case statement would have its very own, private function Also Drizzt DoUrden, the idea of placing comments on the side of code, is really not such a good idea (in my opinion) I really dont want to type anymore to explain why, as of me being tired, so tata

Share this post


Link to post
Share on other sites

  • Advertisement