Join 135,935 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,662 people online right now. Registration is fast and FREE... Join Now!
So I have a relatively basic question: with regard to functions, what is considered good programming practices? More specifically, is it good/bad/indifferent to heavily function out your program? For example:
CODE
/* That is my entire main() */
int main(void) { ShotoBomb(); welcome(); menu(); /* <-------- This calls to 4 other functions */ bye(); return 0; }
CODE
/* and here is the rest of my program */
void ShotoBomb(void) { printf("This program is a creation of ShotoBomb.\n"); printf(" a.k.a. Kevin VanUs\n\n\n"); }
void welcome(void) /* <------------- This is different for every program I write. Sometimes it says a lot more. */ { printf("Welcome to Inventory Manager."); }
void menu(void) { choice = 5; printf ("\n--------------------------\n"); printf ("0: Exit\n"); printf ("1: Item Sold\n"); printf ("2: Daily Report\n"); printf ("3: Weekly Check\n"); printf ("4: Monthly Update\n\n"); printf ("Please enter the number of the menu item you desire: "); scanf ("%d", &choice); gets(a); switch (choice) { case 0: break; case 1: puts("\n\n"); itemsold(); break; case 2: puts("\n\n"); dailyreport(); break; case 3: puts("\n\n"); weeklycheck(); break; case 4: puts("\n\n"); monthlyupdate(); break; default: puts("\nPlease enter a menu choice 0 - 4.\n\n"); menu(); break; } }
void itemsold(void) { item = &item01; printf("Item ID#\n\n"); for (loop = 0; loop < 20; loop++) { printf("%s %d\n", item->name, item->ID); item = item + 1; } printf("\nEnter the ID number of the item sold: "); scanf("%d", &inputID); gets(a); printf("\n"); while(inputID < 1 || inputID > 20) { printf("Please enter a correct ID number for the item sold: "); scanf("%d", &inputID); gets(a); printf("\n\n"); } item = &item01; while (inputID != item->ID) { item = item + 1; } if(item->actual_stock < 1) { printf("%s is out of stock!", item->name); } else { printf("Number of %s(s) sold: ", item->name); scanf("%d", &sold); gets(a); while(sold < 0 || sold > item->actual_stock) { printf("\nPlease enter the number sold (between 0 and %d): ", item->actual_stock); scanf("%d", &sold); gets(a); } item->actual_stock = item->actual_stock - sold; item->total_month_sales = (item->total_month_sales) + sold; printf("%s, ID# %d SOLD(x%d) @ $%lg each.\n",item->name, item->ID, sold, item->price); due = item->price * sold; printf("Amount Due: $%lg\n\n", due); } inputID = 0;
void bye(void) { puts("\n\n\n\nThank you for using Inventory Manager by ShotoBomb.\n\n\n"); puts("Normal Termination!\n"); printf("Press 'ENTER' to quit.\n"); fflush(stdout); (void)getchar(); }
Like everything else in life, the goal is moderation. I am largely indifferent to operating out of the main function as long as it makes sense to do so in the problem domain. I have seen some programmers call a single function from main and largely operate from there. What good is main for then? If you have nothing but single function calls in main, that is fine as long as they are well named and you can see the order of things. If menu() calls other functions it is expected by most programmers that those functions help complete the singular task that menu() is designed for. I would not like to see a call to "saveFile()" in something that is suppose to be showing me a menu (and only showing me a menu).
So in short, design the program as you would logically solve the problem the program addresses. Keep things simple and easy to read while keeping all functions related together and systems separate (look up the term decoupling in relation to system design for more about this topic).
I should never have to see a program and ask myself "why on earth are they saving a file in a routine that is suppose to be showing a menu?" or "Why am I accessing a database when I am suppose to be sorting an already made array?" Instead I should be saying "Oh I see why they are asking for a file, the procedure asks for a file so that it can open it".
Hope that makes sense.
This post has been edited by Martyr2: 30 Nov, 2007 - 02:03 PM
I should never have to see a program and ask myself "why on earth are they saving a file in a routine that is suppose to be showing a menu?" or "Why am I accessing a database when I am suppose to be sorting an already made array?" Instead I should be saying "Oh I see why they are asking for a file, the procedure asks for a file so that it can open it".
OK thanks for the reply. Since the menu() was pointed out, let me ask about that specifically: my menu() prints out the menu, and then asks the user to enter a choice. From there it calls the desired function (via a switch statement). The chosen function executes, then calls back to menu() to continue the program. Menu() is only fully exited when the user prompts for the exit command, in which case the switch statement breaks, the program returns to main(), then bye() is called (basically sayings "thanks, normal termination, bye") and then goes back to main() for the return 0; statement. And the program exits.
So...is that "expected" menu() functionality, or should the other function calls be placed elsewhere? (To be honest, I can't think of where else to put them though...although if I could figure something out if needed.)
Anyhow, thanks again for the responses!
edit: misspelled the BananaMan command!
This post has been edited by ShotokanDeity: 30 Nov, 2007 - 02:28 PM
Yeah that would be perfectly acceptable because you are obviously expecting the user to empty back to the menu after each task. This would be a great setup and even allow you to plug in other menus later via main and after menu() was called. Maybe you would have instead of bye() being the next statement, you would call "launchAnotherMenu()".
This is what I was talking about earlier of showing a reasonable series of steps in program execution. You can see that you call menu and then you go on to showing another series of menu choices with this launchAnotherMenu() call. You are also not doing something like....
CODE
int main() { runmyprogram(); return 0; }
Which essentially makes main() useless since runmyprogram() is the one handling everything.