`

Fix That Code Immediately

阅读更多

本文是从 Fix That Code Immediately! 这篇文章翻译而来。

  你们正在开发一个新项目,你在一个地方看到一段有问题的代码。错误的处理方式是,“啊,别人写的,我最好别碰它”,“我没有时间去改它——我有自己的事要做”,“如果我修改它,肯定会改出问题”。
  问题是——有问题的代码会越积越多。即使是很小的一段程序,经过一段时间的累计,你很快就能看到它成为一个“由一些菜鸟写的、没人愿意去维护的巨大的历史遗留项目”。有人曾说,超过6个月的项目全是“历史遗留”项目,因为里面都会积累大量的有问题的代码,或用另外一个词——技术债务。
  这就是为什么你要马上修改它们的原因。当你看到一些有问题的代码,或一些不是好的写法的东西——改掉它。立即。否则,当你再次注意到它时就已经太晚了,因为其它的代码就开始依赖它,新的代码会模仿这种编写风格(也许是拷贝/粘贴而来),修改这些东西将会变成你的噩梦。让我们把上面错误的做法纠正:
“啊,别人写的代码,我最好别动它”——什么?你的项目团队的一员,你有“权力”去修改它。如果有人把代码写的很糟糕,他可能并没有意识到自己的代码很烂——所以,他们不会改正它。不要认为改正这些代码会冒犯他们。他们也许会没面子,但不是因为你。
“我没有时间去修改它——我有自己的事要做”——这就是你的事。你可以在你的缺陷跟踪里添加上一条任务,写上“重构X”,写上花费的时间。你也可以把它推迟到下一个sprint(如果是敏捷开发)。管理层坚持认为开发新东西比修改旧程序重要吗?告诉他们去读读《重构》这本书或Spolsky的文章..或本文。(也许不管用,但不妨试一试)
“如果我修改它,肯定会改出问题”——也许。但是,等一下,你们有单元测试用例,不是吗?还有集成测试,确认测试。如果没有——先把这些补齐了。这样你就不用担心把程序改坏了。
  代码审查是避免这样的代码很重要的方法。如果提交的代码都经过了代码审查,未被察觉的有问题的代码会大幅度的减少。仍然会有,但会少的多。
  对于这样的做法唯一的问题是——如何确定一段代码是有问题、需要改进的?这就需要经验了,需要你熟悉好的开发方法和模式。对这个问题我不能给出一个秘诀。但你需要在团队里有一群能明辨是非的程序员。如果没有——读一读《代码大全(Code Complete)》(以及《Effective Java》,如果你们使用的语言是Java。)
  所以——请马上修改。这会省下你的时间,免去你的头疼,让你对这个项目更有自豪感,而不是“这烂项目是一些菜鸟写的,我只是做了一些辅助的工作。”你不能这样说——如果项目很烂,你难辞其咎。
分享到:
评论

相关推荐

    acpi控制笔记本风扇转速

    GPEs are now immediately disabled to prevent the waking GPE from firing again and to prevent other wake GPEs from interrupting the wake process. Added the AcpiGpeCount global that tracks the number ...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - FIX: Support for the Import/Export modules that can't work with Streams. Added TFlexFileFormat.StreamSupport property (default True), which difines, is it necessary to open file stream or give the ...

    DbfDotNet_version_1.0_Source

    In Dbf.Net you don't have to write any code, if you don't want to wait for the garbage collector to collect your individual you can call SaveChanges. Dbf.Net ADO.Net Collapse Copy Codevoid ...

    Kwin Scripts

    * 1.6 (unreleased): Make resizing layout-agnostic, a manual tiling mode, code cleanup, fix tiling when compositing is disabled * 1.6.1: Respect min/maxSizes, multiple (or zero) master support in ...

    DTrace Dynamic Tracing In Oracle Solaris, Mac OS X & FreeBSD PDF

    非扫描、纯英文、PDF格式、带目录。...This book will help you make the most of DTrace to solve problems more quickly and efficiently, and build systems that work faster and more reliably.

    LCTF软件备份VariSpec™ Liquid Crystal Tunable Filters

    c) Place a copy of vsdrvr.dll in either the folder that includes the executable code for the program being developed; or, preferably, in the windows system folder. Failures in step a) will lead to ...

    Android代码-Aceso

    Aceso is a Android HotFix by optimizing the AOSP's "InstantRun HotSwap" solution, it is used to fix online bugs without a new APK publish. wiki 中文说明 Features Support 4.x to 7.0 Android OS Perfect...

    BobBuilder_app

    Splitting a page in b+tree has to fix parent nodes and children so effectively will lock the tree for the duration, so parallel updates are very very difficult and have spawned a lot of research ...

    CSS Mastery

     It begins with a brief recap of CSS fundamentals such as the importance of meaningful markup, how to structure and maintain your code, and how the CSS layout model really works.  With the basics ...

    android-viewflow

    Although possible, you should not call setSelection(...) immediately after calling setAdapter(myAdapter) as that might load unnecessary views giving you a decrease in performance. Listen on screen ...

    2009 达内Unix学习笔记

    集合了 所有的 Unix命令大全 ...telnet 192.168.0.23 自己帐号 sd08077-you0 ftp工具 192.168.0.202 tools-toolss ... 各个 shell 可互相切换 ksh:$ sh:$ csh:guangzhou% bash:bash-3.00$ ... 命令和参数之间必需用空格隔...

    S7A驱动720版本

    Please make sure that slot number of the CPU is entered and not of the Profibus-CP!! - The driver now supports the different date and time data types of the S7 PLC. See Topic "Hardware Options ...

    联想旭日420AXP系统芯片组驱动

    components in order to ensure that the following features function properly: - Core PCI and ISAPNP Services - PCIe Support - IDE/ATA33/ATA66/ATA100 Storage Support - SATA Storage Support - USB ...

    8-07-14_MegaCLI for linux_windows

    SCGCQ00337213 Defect FW reports exit code 0x3d (Unsupported HWR) when getting bbu capacity info for Supercap SCGCQ00337810 Defect 37 drives shown as unconfigured good in MegaCLI when "enable JBOD=0" ...

    unigui0.83.5.820

    - 0000769: UniDBGrid: Row position is ignored if row is immediately changed after a call to Open() - 0000673: UniDBGrid: OnDrawColumnCell event - 0000768: Better "ext\" folder translation - 0000766:...

Global site tag (gtag.js) - Google Analytics