用函式來傳達你的心意> 0 <


前情提要一下,上次在變數命名的善意那篇中我們把arr換成seats代表一堆位置,n1換成seat代表位置索引,n2換成number代表要放入seats內的值,這次我想帶你探索while迴圈內想表達什麼,如何才能讓別人一眼就看出他想說的事。

int main()
{
    int seats[10] = {99, 0, 10, 31, 0, 42, 70, 67, 0, 0};
    int i, j, seat, number;
    printf("*seating*\n");
    for(int i = 0; i < 10; ++i){
        printf("%d ", seats[i]);
    }

    printf("\n");
    printf("***************\n");
    printf("Please input the seat (1~10) and 2-digit number(0 to end game)\n");
    scanf("%d %d", &seat, &number);
    while(number != 0){
        if(seats [seat - 1] == 0){
            seats[seat - 1] = number;
            printf("*seating*\n");
            for(int i = 0; i < 10; ++i){
                printf("%d ", seats[i]);
            }
            printf("\n");
            printf("***************\n");
            printf("Please input the seat (1~10) and 2-digit number(0 to end game)\n");
            scanf("%d %d", &seat, &number);
        }
        else{
            printf("Sorry, seat is taken.\n");
            scanf("%d %d", &seat, &number);
        }
    }

    printf("*seating*\n");
    bubble_sort(seats, 10);
    for(int i = 0; i < 10; ++i){
        printf("%d ", seats[i]);
    }
    printf("\n");
    printf("***************\n");

    return 0;
}

當number != 0時做以下的事,這是我第一眼看見程式碼得到最直接的資訊,其實在上幾行印出來的訊息中有提到

printf("Please input the seat (1~10) and 2-digit number(0 to end game)\n");

所以我可以得知說繼續執行whlie做的事是由使用者輸入的數值決定的。

while(number != 0){
    if(seats [seat - 1] == 0){
        seats[seat - 1] = number;
        printf("*seating*\n");
        for(int i = 0; i < 10; ++i){
            printf("%d ", seats[i]);
        }
        printf("\n");
        printf("***************\n");
        printf("Please input the seat (1~10) and 2-digit number(0 to end game)\n");
        scanf("%d %d", &seat, &number);
    }
    else{
        printf("Sorry, seat is taken.\n");
        scanf("%d %d", &seat, &number);
    }
}

那往下看if(seats [seat - 1] == 0)裡面又做了一堆事,但我們可以先看else的部分(因為感覺內容不多),else裡面是告訴你位置被拿走了,然後又重新輸入一次seat和number,回頭再看if內的判斷式,可以得知0這個數字代表的意思是位置是空的,重新再詮釋一遍的話就是當位置是空的時候做下面的事否則重新再輸入一次位置和數值。

    if(seats [seat - 1] == 0){
        seats[seat - 1] = number;
        printf("*seating*\n");
        for(int i = 0; i < 10; ++i){
            printf("%d ", seats[i]);
        }
        printf("\n");
        printf("***************\n");
        printf("Please input the seat (1~10) and 2-digit number(0 to end game)\n");
        scanf("%d %d", &seat, &number);
    }
    else{
        printf("Sorry, seat is taken.\n");
        scanf("%d %d", &seat, &number);
    }

那if內到底在做什麼呢? seats[seat - 1] = number這裡很直覺是指把輸入的值塞到seat-1這個空的座位裡,-1的原因是陣列起始索引是從0開始的,接下來到for迴圈裡把所有的值印出來,最後再輸入一次新的位置和數值。

        seats[seat - 1] = number;
        printf("*seating*\n");
        for(int i = 0; i < 10; ++i){
            printf("%d ", seats[i]);
        }
        printf("\n");
        printf("***************\n");
        printf("Please input the seat (1~10) and 2-digit number(0 to end game)\n");
        scanf("%d %d", &seat, &number);

可以請你仔細想一下這三件事情其實是各自獨立的,但他們全部連在一起的時候會混淆他們之間的關係,增加閱讀的困難,偷偷跟你分享個小技巧,加幾個enter會更容易區分他們。

        seats[seat - 1] = number;

        printf("*seating*\n");
        for(int i = 0; i < 10; ++i){
            printf("%d ", seats[i]);
        }

        printf("\n");
        printf("***************\n");
        printf("Please input the seat (1~10) and 2-digit number(0 to end game)\n");
        scanf("%d %d", &seat, &number);

終於把整個while內在做的事搞懂了,再回頭看完整的程式碼後,不知道你有沒有感覺到有些片段好像重複出現過了像是印出陣列內的所有值請使用者輸入seat和number。

    printf("*seating*\n");
    for(int i = 0; i < 10; ++i){
        printf("%d ", seats[i]);
    }
    printf("\n");
    printf("***************\n");
    printf("Please input the seat (1~10) and 2-digit number(0 to end game)\n");
    scanf("%d %d", &seat, &number);

當出現這種情形,代表我們可以新增函式來取代重複的事情,新函式的名稱我覺得用showSeats()跟getUserInput()這樣最直接明白,當然你也可以有自己的想法,但切記要讓別人一看就懂,不要用只有自己看得懂的意思,接下來我們來改改看。

void showSeats(int seats[], int length)
{
    printf("*seating*\n");
    for(int i = 0; i < length; ++i){
        printf("%d ", seats[i]);
    }
}

void getUserInput(int& seat, int& number){
    printf("\n");
    printf("***************\n");
    printf("Please input the seat (0~9) and number(-1 to end game)\n");
    scanf("%d %d", &seat, &number);
}

int main()
{
    int seats[10] = {99, 0, 10, 31, 0, 42, 70, 67, 0, 0};
    int i, j, seat, number;
    showSeats(seats, 10);

    getUserInput(seat, number);

    while(number != 0){
        if(seats [seat - 1] == 0){
            seats[seat - 1] = number;

            showSeats(seats, 10);

            getUserInput(seat, number);
        }
        else{
            printf("Sorry, seat is taken.\n");
            scanf("%d %d", &seat, &number);
        }
    }

    bubble_sort(seats, 10);

    showSeats(seats, 10);

    printf("\n");
    printf("***************\n");

    return 0;
}

哇!!! 看看我們改完後的結果多麼令人舒暢,while迴圈內是不是一看就知道他想做什麼了,雖然還有些地方可以表達得更清楚但已經比一開始看不出個毛來好太多了,而且這樣做的好處有很多,除了增加閱讀性之外,bug也方便查找,如果之後想要更換顯示陣列的方式為從尾巴開始,只需要在showSeats裡修改就可以了,最重要的是你幫助別人省去看程式碼的時間,因為你已經寫在函式上了,相信看你程式碼的人會感受到你的心意。

下一篇我會帶你把整個main裡可以優化的地方重新組織一下,讓整個輸入數值到陣列的前置作業更清楚,感謝你陪我到這裡,相信這段過程會對你有幫助,我們下次見^ ^

#重構 #程式碼 #變數







你可能感興趣的文章

[Day 03] 觀察者模式,命令模式,轉接器模式,表象模式

[Day 03] 觀察者模式,命令模式,轉接器模式,表象模式

Day03  自製收費廣告版位 - 使用 react-intersection-observer

Day03 自製收費廣告版位 - 使用 react-intersection-observer

Day 93

Day 93






留言討論