中年如何高效阅读代码?Linux大神拍了拍你并教给你这三个步骤( 三 )
这是一个长达106行的函数 , 下面我们开始精简!
1、根据“三部曲”精简原则 , 我们首先去掉其中的注释 。 还剩下92行:
int main(int argc, char *argv[]) { const int constant_yes = 1 const int constant_true = 1 const int constant_no = 0 const int constant_false = 0 struct emul **emuls char **diskimages = NULL int n_diskimages = 0 int n_emuls int i progname = argv[0] console_init() cpu_init() device_init() machine_init() timer_init() useremul_init() emuls = malloc(sizeof(struct emul *)) if (emuls == NULL) { fprintf(stderr, "out of memory") exit(1) } n_emuls = 1 emuls[0] = emul_new(NULL) if (emuls[0] == NULL) { fprintf(stderr, "out of memory") exit(1) } get_cmd_args(argc, argv, emuls[0], &diskimages, &n_diskimages) if (!skip_srandom_call) { struct timeval tv gettimeofday(&tv, NULL) srandom(tv.tv_sec ^ getpid() ^ tv.tv_usec) } debug("GXemul") debug(" Copyright (C) 2003-2006 Anders Gavare") debug("Read the source code and/or documentation for other Copyright messages.") if (emuls[0]->machines[0]->machine_type == MACHINE_NONE) { n_emuls -- } else { for (i=0 i<n_diskimages i++) diskimage_add(emuls[0]->machines[0], diskimages[i]) } if (n_emuls > 0) { for (i=1 i<argc i++) if (argv[i][0] == "@") { fprintf(stderr, "You can either start one " "emulation with one machine directly from " "the commandline, or start one or more " "emulations using configuration files." " Not both.") exit(1) } emul_simple_init(emuls[0]) } for (i=1 i<argc i++) { if (argv[i][0] == "@") { char tmpstr[50] char *s = argv[i] + 1 if (strlen(s) == 0 && i+1 < argc && argv[i+1][0] != "@") { i++ s = argv[i] } n_emuls ++ emuls = realloc(emuls, sizeof(struct emul *) * n_emuls) if (emuls == NULL) { fprintf(stderr, "out of memory") exit(1) } console_allow_slaves(1) if (n_emuls == 1) { emul_destroy(emuls[0]) } emuls[n_emuls - 1] = emul_create_from_configfile(s) snprintf(tmpstr, sizeof(tmpstr), "emul[%i]", n_emuls-1) } } if (n_emuls == 0) { fprintf(stderr, "No emulations defined. Maybe you forgot to " "use -E xx and/or -e yy, to specifythe machine type." " For example: %s -e 3max -d disk.img" "to boot an emulated DECstation 5000/200 with a disk " "image.", progname) exit(1) } device_set_exit_on_error(0) console_warn_if_slaves_are_needed(1) emul_run(emuls, n_emuls) console_deinit() for (i=0 i<n_emuls i++) emul_destroy(emuls[i]) return 0}
2、删除注释以后仍然很长 , 所以我们开始第二次精简 , 去掉变量声明和简单的赋值语句 。 还剩下78行:
int main(int argc, char *argv[]) { console_init() cpu_init() device_init() machine_init() timer_init() useremul_init() emuls = malloc(sizeof(struct emul *)) if (emuls == NULL) { fprintf(stderr, "out of memory") exit(1) } emuls[0] = emul_new(NULL) if (emuls[0] == NULL) { fprintf(stderr, "out of memory") exit(1) } get_cmd_args(argc, argv, emuls[0], &diskimages, &n_diskimages) if (!skip_srandom_call) { gettimeofday(&tv, NULL) srandom(tv.tv_sec ^ getpid() ^ tv.tv_usec) } debug("GXemul") debug(" Copyright (C) 2003-2006 Anders Gavare") debug("Read the source code and/or documentation for other Copyright messages.") if (emuls[0]->machines[0]->machine_type == MACHINE_NONE) { n_emuls -- } else { for (i=0 i<n_diskimages i++) diskimage_add(emuls[0]->machines[0], diskimages[i]) } if (n_emuls > 0) { for (i=1 i<argc i++) if (argv[i][0] == "@") { fprintf(stderr, "You can either start one " "emulation with one machine directly from " "the commandline, or start one or more " "emulations using configuration files." " Not both.") exit(1) } emul_simple_init(emuls[0]) } for (i=1 i<argc i++) { if (argv[i][0] == "@") { if (strlen(s) == 0 && i+1 < argc && argv[i+1][0] != "@") { i++ s = argv[i] } n_emuls ++ emuls = realloc(emuls, sizeof(struct emul *) * n_emuls) if (emuls == NULL) { fprintf(stderr, "out of memory") exit(1) } console_allow_slaves(1) if (n_emuls == 1) { emul_destroy(emuls[0]) } emuls[n_emuls - 1] = emul_create_from_configfile(s) snprintf(tmpstr, sizeof(tmpstr), "emul[%i]", n_emuls-1) } } if (n_emuls == 0) { fprintf(stderr, "No emulations defined. Maybe you forgot to " "use -E xx and/or -e yy, to specifythe machine type." " For example: %s -e 3max -d disk.img" "to boot an emulated DECstation 5000/200 with a disk " "image.", progname) exit(1) } device_set_exit_on_error(0) console_warn_if_slaves_are_needed(1) emul_run(emuls, n_emuls) console_deinit() for (i=0 i<n_emuls i++) emul_destroy(emuls[i]) return 0}
推荐阅读
- 湖人队|扎心?一张图看湖人如何应对马赛克三分雨,神龟5米范围没人理他
- 搜狐新闻|准大学生数码三件套如何选?这些元气好物值得种草
- 想好好做医生的胖子|五十岁女性经常有周身无力和疼痛的困扰!该如何应对?医生告诉您
- 儿童饮食|秋冬季如何让宝宝爱上吃水果?稍微蒸一下美味翻倍,营养价值更高
- 队员|北极科考中“遭遇”北极熊,看科考队员如何与它们和谐相处
- 中国历史发展过程|中国历史发展过程!中国游戏外挂发展史:“辅助工具”是如何成长为“苍
- 金色年华录|都是男扮女装谈恋爱,《少女大人》比《女世子》如何?
- 上海市嘉定区人民政府网站|每年消耗3350吨蔬菜150吨肉,嘉定这家中央厨房如何践行“光盘行动”?
- 上海嘉定|如何让《民法典》走进群众心里?嘉定这些街镇和单位这样做
- 没蜡笔的小新|“整容脸”应该被鄙视吗?医生告诉你千篇一律的好看皮囊如何而来
