如何打造精品软件一直是软件行业讨论的热点话题。初入职场的软件开发人员,多数都只是在学会了某种编程语言后就直接进行编程工作。但由于他们并未掌握编程中的一些有效方法,导致其开发的软件问题累累且工作效率不高。码农修行 编写优雅代码的32条法则针对这类人群提出了一些改善编程方法的建议,涉及具体编程工作的诸多方面,偏重于实践。书中大部分示例都是笔者实际编程工作中碰到过的真实案例,具有较高的参考价值。码农修行 编写优雅代码的32条法则从代码的可读性、可靠性、效率、可维护性、可扩展性5个方面,提出了编写代码的32 条法则。此外还公开了笔者自主编写的一款Android 小游戏源代码,希望能对读者有所帮助。
码农修行 编写优雅代码的32条法则适用于有一定编程基础、且想进一步提升个人编程能力的读者阅读,也可作为大中专院校计算机专业师生的教学参考用书。
自序
前言
引子 ···································································································· 1
第1 章:可读性 ························································································· 4
法则01:准确命名 ·············································································· 6
法则02:设置缩进 ············································································ 12
法则03:保留个性 ············································································ 16
法则04:语法潜台词 ········································································ 21
法则05:补充注释 ············································································ 26
第2 章:可靠性 ······················································································· 37
法则06:增强健壮性 ········································································ 39
法则07:避免过度防御 ···································································· 43
法则08:防止不确定性 ···································································· 48
法则09:善始善终 ············································································ 51
法则10:异常处理 ············································································ 59
法则11:留意编译告警 ···································································· 65
法则12:尽早暴露问题 ···································································· 67
法则13:规避短板 ············································································ 70
第3 章:效率 ··························································································· 75
法则14:关注性能热点 ···································································· 77
法则15:留意非热点代码 ································································· 84
法则16:采用缓存 ············································································ 90
法则17:引入并发 ············································································ 99
第4 章:可维护性 ················································································· 102
法则18:记录日志 ·········································································· 104
法则19:明确错误提示 ·································································· 109
法则20:代码看护 ·········································································· 112
法则21:消除冗余代码 ·································································· 117
法则22:掌握逻辑运算 ·································································· 123
法则23:控制函数规模 ·································································· 128
法则24:控制文件规模 ·································································· 140
第5 章:可扩展性 ················································································· 143
法则25:正确理解面向对象设计 ···················································· 145
法则26:控制接口规模 ·································································· 155
法则27:写可重用的模块 ······························································· 160
法则28:写可重入的函数 ······························································· 164
法则29:避免循环依赖 ·································································· 169
法则30:保障平稳升级 ·································································· 176
法则31:灵活注入对象 ·································································· 182
法则32:正确运用依赖注入 ··························································· 194
第6 章:代码资源 ················································································· 200
日志框架 ························································································· 201
进程委托调用框架··········································································· 208
花样泡泡龙 ····················································································· 215
尾声 ······································································································· 267