中年如何高效阅读代码?Linux大神拍了拍你并教给你这三个步骤( 二 )
- 1.代码 vs. 注释
- 注释是非常有用的 , 可以帮助我们理解代码 , 但是注释在很大程度上是用于了解细节的 , 而在对主脉络的了解上面 , 用处不大 。 所以首先考虑的就是去掉注释 , 包括//…和/*…*/格式的直接注释 , 以及#if 0 … #endif格式的条件汇编 。
- 2.程序流程 vs. 变量声明
- 去掉注释以后 , 纯粹的代码会变得清爽很多 。 但是如果依旧十分复杂 , 影响阅读 , 那么就可以删除一些变量声明和简单的初始化赋值语句 , 留下重要的程序流程 。
- 3.功能语句 vs. 调试语句
- 如果到了这里主脉络依旧不是十分清晰 , 那么可以开始考虑去掉各种调试和打印语句 , 比如printf()、printk()、debug()等 。
- 4.正常流程 vs. 异常流程
- 异常处理是程序健壮性必不可少的部分 , 但是如果异常处理本身代码过于复杂 , 那它势必会影响可读性 。 因此在必要的时候 , 为了方便阅读 , 可以去掉返回值检查、try-catch中的catch子句等 。
- 5.常见路径 vs. 罕见路径
- 通常情况下 , 代码精简到这一步就已经比较容易理解了 , 但如果有必要 , 可以对switch-case、if-else等结构进行处理 , 只保留最常见的一种情况 。
【中年如何高效阅读代码?Linux大神拍了拍你并教给你这三个步骤】
int main(int argc, char *argv[]) { /* Setting constants: */ 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] /* Initialize all emulator subsystems: */ 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) } /* Allocate space for a simple emul setup: */ 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) } /* Print startup message: */ 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]) } /* Simple initialization, from command line arguments: */ if (n_emuls > 0) { /* Make sure that there are no configuration files as well: */ 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) } /* Initialize one emul: */ emul_simple_init(emuls[0]) } /* Initialize emulations from config files: */ 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) } /* Always allow slave xterms when using multiple emulations: */ console_allow_slaves(1) /* Destroy the temporary emuls[0], since it will be overwritten: */ 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) /* Run all emulations: */ emul_run(emuls, n_emuls) /* * Deinitialize everything: */ console_deinit() for (i=0 i<n_emuls i++) emul_destroy(emuls[i]) return 0}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 湖人队|扎心?一张图看湖人如何应对马赛克三分雨,神龟5米范围没人理他
- 搜狐新闻|准大学生数码三件套如何选?这些元气好物值得种草
- 想好好做医生的胖子|五十岁女性经常有周身无力和疼痛的困扰!该如何应对?医生告诉您
- 儿童饮食|秋冬季如何让宝宝爱上吃水果?稍微蒸一下美味翻倍,营养价值更高
- 队员|北极科考中“遭遇”北极熊,看科考队员如何与它们和谐相处
- 中国历史发展过程|中国历史发展过程!中国游戏外挂发展史:“辅助工具”是如何成长为“苍
- 金色年华录|都是男扮女装谈恋爱,《少女大人》比《女世子》如何?
- 上海市嘉定区人民政府网站|每年消耗3350吨蔬菜150吨肉,嘉定这家中央厨房如何践行“光盘行动”?
- 上海嘉定|如何让《民法典》走进群众心里?嘉定这些街镇和单位这样做
- 没蜡笔的小新|“整容脸”应该被鄙视吗?医生告诉你千篇一律的好看皮囊如何而来
