中年如何高效阅读代码?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], &ampdiskimages, &ampn_diskimages) if (!skip_srandom_call) { struct timeval tv gettimeofday(&amptv, 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]-&gtmachines[0]-&gtmachine_type == MACHINE_NONE) { n_emuls -- } else { for (i=0 i&ltn_diskimages i++) diskimage_add(emuls[0]-&gtmachines[0], diskimages[i]) } if (n_emuls &gt 0) { for (i=1 i&ltargc 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&ltargc i++) { if (argv[i][0] == "@") { char tmpstr[50] char *s = argv[i] + 1 if (strlen(s) == 0 &amp&amp i+1 &lt argc &amp&amp 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&ltn_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], &ampdiskimages, &ampn_diskimages) if (!skip_srandom_call) { gettimeofday(&amptv, 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]-&gtmachines[0]-&gtmachine_type == MACHINE_NONE) { n_emuls -- } else { for (i=0 i&ltn_diskimages i++) diskimage_add(emuls[0]-&gtmachines[0], diskimages[i]) } if (n_emuls &gt 0) { for (i=1 i&ltargc 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&ltargc i++) { if (argv[i][0] == "@") { if (strlen(s) == 0 &amp&amp i+1 &lt argc &amp&amp 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&ltn_emuls i++) emul_destroy(emuls[i]) return 0}


推荐阅读