Welcome to Dream.In.Code
Getting Help is Easy!

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!




Good Programming Practices

 
Reply to this topicStart new topic

Good Programming Practices

ShotokanDeity
30 Nov, 2007 - 01:43 PM
Post #1

D.I.C Head
Group Icon

Joined: 13 Sep, 2007
Posts: 79


My Contributions
Hey everyone!

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;

    menu();
}

void dailyreport(void)
{
    item = &item01;
    printf("DAILY REPORT \nItem    ID    Monthly Sales\n");
    for (loop = 0; loop < 20; loop++)
    {
        printf("%s    %d    %d    \n", item->name, item->ID, item->total_month_sales);
        item = item + 1;
    }
    printf("\n");
    printf("Press 'ENTER' to return to menu.\n");
    fflush(stdout);
    (void)getchar();

    menu();
}

void weeklycheck(void)
{
    item = &item01;
    printf("Weekly Check\n--------------\n");
    printf("Restock the following items:\n");
    printf("Item    ID#    Amount to Buy\n\n");
    for(loop = 0; loop < 20; loop++)
    {
        item->restock = (item->wanted_stock)/10;
        amount_to_buy = (item->wanted_stock) - (item->actual_stock);
        if(item->actual_stock <= item->restock)
        {
            printf("%s    %d    %d\n", item->name, item->ID, amount_to_buy);
        }
        item = item + 1;
    }

    menu();
}

void monthlyupdate(void)
{
    item = &item01;
    printf("Monthly Update\n-------------\n\n");
    month = 0;
    while(month < 1 || month > 12)
    {
        printf("Enter the current month number(e.g. enter 1 for January): ");
        scanf("%d", &month);
        gets(a);
    }
    for(loop = 0; loop < 20; loop++)
    {
        item->curr_month = month;
        item = item + 1;
    }
    item = &item01;
    printf("Now please enter the actual in-stock inventory for each item.\n");
    printf("Item    ID#\n");
    for(loop = 0; loop < 20; loop++)
    {
        stock = 0;
        printf("\n%s    %d    Inventory (max %d): ", item->name, item->ID, item->wanted_stock);
        scanf("%d", &stock);
        gets(a);
        while(stock < 1 || stock > item->wanted_stock)
        {
            printf("Please enter the amount of inventory (between 1 and %d.): ", item->wanted_stock);
            scanf("%d", &stock);
            gets(a);
        }
        item->actual_stock = stock;
        item = item + 1;
    }
    item = &item01;
    for(loop = 0; loop < 20; loop++)
    {
        item->total_month_sales = 0;
        item = item + 1;
    }
    
    menu();
}

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();
}


Thanks all!
bananaman.gif
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Good Programming Practices
30 Nov, 2007 - 02:01 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,173



Thanked: 208 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
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. smile.gif

This post has been edited by Martyr2: 30 Nov, 2007 - 02:03 PM
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Good Programming Practices
30 Nov, 2007 - 02:04 PM
Post #3

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,433



Thanked: 64 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(Martyr2 @ 30 Nov, 2007 - 04:01 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".



Maybe there is a menu for saving the file.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Good Programming Practices
30 Nov, 2007 - 02:09 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,173



Thanked: 208 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
I was talking about if the only job of the routine was to print the menu choices. smile.gif
User is offlineProfile CardPM
+Quote Post

ShotokanDeity
RE: Good Programming Practices
30 Nov, 2007 - 02:27 PM
Post #5

D.I.C Head
Group Icon

Joined: 13 Sep, 2007
Posts: 79


My Contributions
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!
bananaman.gif

edit: misspelled the BananaMan command!

This post has been edited by ShotokanDeity: 30 Nov, 2007 - 02:28 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Good Programming Practices
30 Nov, 2007 - 02:40 PM
Post #6

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,173



Thanked: 208 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
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. smile.gif
User is offlineProfile CardPM
+Quote Post

ShotokanDeity
RE: Good Programming Practices
3 Dec, 2007 - 08:49 AM
Post #7

D.I.C Head
Group Icon

Joined: 13 Sep, 2007
Posts: 79


My Contributions
I know I'm saying this a little late, but thanks for the reply Martyr! Your little example cleared up what you were trying to say earlier for me.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 08:30AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month