變數命名的善意


這是一小段泡沫排序法的程式碼,可是在說什麼呢?你可以試著閱讀他,但千萬不要勉強自己。

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

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

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

    return 0;
}

如果你跟我一樣,看到n1、n2那邊就受不了的,我能了解你的感受,但是目前的資訊量還不夠給這兩個變數一個合適的命名,往下看也許可以找到答案。

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

這段程式碼有排序前的陣列,四個之後會用到的變數i、j、n1、n2以及印出arr陣列內所有的值,可能一開始只是想展示arr有什麼東西吧。


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

喔~這邊就明白多了,提示使用者輸入seat和number,代表可能會在陣列中嘗試放進一個值,這裡告訴我們n1代表插入陣列的位置,n2代表插入的值,我們可以用seat代表n1,number代表n2。

    int arr[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 ", arr[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);

修改過後總算有點理解他想表達的事情了,首先先印出排序前陣列的所有值,再來提示使用者輸入插入的位置和數值,但還有一個地方我不太滿意的就是arr這個陣列的命名,既然都用seat當作位置的索引了,我想用seats(一堆位置)來取代arr可以更明確表達意圖,也許你有其他想法也可以提供,但目前我想先用seats來重新修改一下全部的程式碼。


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

這樣舒服多了,不用再去想n1代表索引、n2代表數字,程式碼直接表達出他的意圖,讓我們可以更輕鬆閱讀他,小細節的修改帶來的便利性是會積累的,也能讓閱讀的人感受到你的善意,下一篇我們會往while迴圈內探索(老實說一開始我沒看懂while內在做什麼XD),感謝你陪我到這裡,我們下次見^ ^

#重構 #程式碼 #變數







你可能感興趣的文章

MR-ROBOT: 1 Walkthrough

MR-ROBOT: 1 Walkthrough

網域申請及網站部署遠端伺服器

網域申請及網站部署遠端伺服器

JQ總務處|選擇器與方法|深入淺出jQuery

JQ總務處|選擇器與方法|深入淺出jQuery






留言討論