Read Code

We programmers are weird creatures. We love writing code. But when it comes to reading it we usually shy away. After all, writing code is so much more fun, and reading code is hard — sometimes almost impossible. Reading other people's code is particularly hard. Not necessarily because other people's code is bad, but because they probably think and solve problems in a different way to you. But did you ever consider that reading someone else's code could improve your own?

编程人员都是不可思议的生物。我们喜爱编程。但是当开始读代码的时候我们通常避开。毕竟,写代码有更多的乐趣,读代码是困难的--有时候几乎不可能。读其他人的代码特别的困难。未必是因为别人的代码很糟糕,但是他们可能在解决问题上对你来说是一个不同的方式。你曾经思考过度别人的代码能提高你自己的么?

The next time you read some code, stop and think for a moment. Is the code easy or hard to read? If it is hard to read, why is that? Is the formatting poor? Is naming inconsistent or illogical? Are several concerns mixed together in the same piece of code? Perhaps the choice of language prohibits the code from being readable? Try to learn from other people's mistakes, so that your code won't contain the same ones. You may receive a few surprises. For example, dependency-breaking techniques may be good for low coupling, but they can sometimes also make code harder to read. And what some people call elegant code, others call unreadable.

在你下次读别人的代码时,停下来认真的想想。这个代码是简单或者很难去读?如果很难读,为什么?是代码格式?是命名不一致或者不合逻辑吗?是几个概念混合在相同的代码块中?或许选择的语言阻止这个代码变得具有可读性?试着从其他人的错误上学习,以至于你的代码将不会再返错误。你可能得到些许惊喜。例如,依赖破坏技术可能对低耦合有好处,但是有时候使代码很难去读懂。有些人叫做优雅的代码,另一些人会叫做不可读的。

If the code is easy to read, stop to see if there is something useful you can learn from it. Maybe there's a design pattern in use that you don't know about, or had previously struggled to implement. Perhaps the methods are shorter and their names more expressive than yours. Some open source projects are full of good examples of how to write brilliant, readable code — while others serve as examples of the exact opposite! Check out some of their code and take a look.

如果代码很难读懂,停止去查看是否你能从这里学到一些有用的。可能这是一个设计模式你不知道的,或者有以前努力实现的。或许方法很短,他们的命名比你的更具有表现力。一些开源的项目充满了好的例子如何写明了的,可读的代码--然而其他人担当了恰恰相反的例子!检查其他人的代码然后看看。

Reading your own old code, from a project you are not currently working on, can also be an enlightening experience. Start with some of your oldest code and work your way forward to the present. You will probably find that it is not at all as easy to read as when you wrote it. Your early code may also have a certain embarrassing entertainment value, kind of in the same way as being reminded of all the things you said when you were drinking in the pub last night. Look at how you have developed your skills over the years — it can be truly motivating. Observe what areas of the code are hard to read, and consider whether you are still writing code in the same way today.

读你自己的老代码,从非你当前工作的项目中,也将是有启发的经验。开始一些你的老代码,按照你想要的方式去呈现。你将可能发现这是不容易的去读你写过代码。你早起的代码可能有某种令人尴尬的值,正如以同样的方式留下了你昨晚在酒吧喝醉时说过的所有事情。看看你如何提高你的技能在这些年里--它将真的有积极意义。观察哪部分的代码很难读懂,考虑是否你在今天一直用相同的方式写这些代码。

So the next time you feel the need to improve your programming skills, don't read another book. Read code.

posted @ 2013-01-16 23:56  sqtds  阅读(339)  评论(0编辑  收藏  举报