变数命名的善意

这是一小段泡沫排序法的程序码,可是在说什麽呢?你可以试着阅读他,但千万不要勉强自己。

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

/images/emoticon/emoticon06.gif如果你跟我一样,看到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;
}

/images/emoticon/emoticon34.gif这样舒服多了,不用再去想n1代表索引、n2代表数字,程序码直接表达出他的意图,让我们可以更轻松阅读他,小细节的修改带来的便利性是会积累的,也能让阅读的人感受到你的善意,下一篇我们会往while回圈内探索(老实说一开始我没看懂while内在做什麽XD),感谢你陪我到这里,我们下次见^ ^

下一篇传送门


<<:  CMoney菁英软件工程师战斗营前端课程_Week 11

>>:  云服务器安全吗?云服务器有哪些优势?

关於 Scratch 3

关於 Scratch 3 教学原文参考:关於 Scratch 3 Scratch 是由美国麻省理工学...

Day 19 网页分析 - Web Application Analysis (Wapiti)

工具介绍 今天要介绍的工具叫做挖鼻涕Wapiti,是用来扫描网页的安全性,以黑盒的方式,也就是在不了...

[Angular] Day26. Reactive forms (二)

上一篇中介绍了如何使用 FormControl 建立单个表单控制元件,也介绍了如何使用 FormGr...

【Day 09】- 今天来创造 Ghost Process(基於断链隐藏 Process 的手法)

Agenda 资安宣言 测试环境与工具 学习目标 技术原理与程序码 References 下期预告 ...