转载:写更少的代码

转载自: http://justjavac.com/other/2012/07/20/write-less-code.html

 

软件开发的一个基本事实是:你需要写代码。 但是一个最大的概念误区是“写代码就是我的工作”。 当我开始我的软件开发者生涯时,我就陷入了这个误区。

代码是强大的,它能让你做许多事情,你觉得自己是有效率的,觉得自己完成了工作。 然后,这些年来我学到了事情的真实的本质:一个程序员的工作的本质是写尽可能少的代码

不要误解我的意思。我不是说我们应该压缩代码,以尽量少敲键盘为目的,从而让写出来的代码根本无法理解。 当你想到,作为一个程序员,你的绝大部分时间将用来维护你写的可怕的代码这一事实的时候, 你就会觉得“写更少的代码,不要让自己写的代码给自己更来更大的工作量”这一原则是一个非常不错的主意。

为什么

想想你的工作。想想你使用的工具,看看你喜欢用来开发的框架。 你做的每件事,你使用的每件工具,都是为了让你写更少的代码。 你的工具能让你犯更少的错误,并且把那些可能会给你带来麻烦的底层细节都抽象出来。

那么,为什么你需要写更少的代码?前文中我说:你的工作就是写更少的代码。 其实我没有说出真相。你的真正的工作是思考。 你的工作是思考要解决的问题,设计一个优雅的解决方案,然后把这个方案变成软件。 而开发软体的一个副产品是,你需要通过写代码来解决问题

代码是副产品

代码并没有那样重要。 之所以我们喜欢让自己那样认为,是因为当我们开始干活、把解决方案变成代码时,写代码看来就好像既是手段,又是目的。 我们谈论自己使用的编辑器,谈论我们使用的编程语言和我们的编程环境。 我这样做过,你这样做过,我们都这样做过。 诚然,享受你的工作带给你的快乐是无可厚非的,但是你要意识到,代码是很棒的, 但它同样是你的敌人:写代码花掉你大量的时间,它可能有各种bug,它可能很难理解,或者不够优雅

这就是我为什么坚持“写更少的代码”的原因。 当我觉得一些东西变得超出需要的烦琐、或者比它原本的样子更复杂时,我告诉自己“写更少的代码”。 当我写了太多的代码以至于很难在上下文中理解它们时,我就重构、清理、坚持想着“写更少的代码”。

虽然让我很痛苦,但不得不说:有时写大量的代码是无法避免的。 但是,当你不能保持简洁(concise)时,则保持清晰(clear)。 “写更少的代码”是就写干净(clean)的代码,写清晰(clear)的代码。 当你写的代码足够简练时,你的程序的目的就变得更加清楚。

那么,这一课的经验是什么呢?我认为最重要的是“代码只是一个副产品”,它是你在你工作的过程中不可避免要创造出来的东西。 因此,多思考,多重构,去掉一些老旧的代码,写更少的新的代码,给自己一个机会,从今天开始。

英文原文:Write Less Code

 

Write Less Code

One of the fundamental truths of software development is that you have to write code, but one of the biggest fallacies is the idea that writing code is your job. When I first started out as a software developer, I fell into that trap, writing code is a powerful thing, its empowering, you feel like you are productive and you are accomplishing things. However, what I have learned over the years is the real truth of the matter. The truth that the job of a software developer is to write as little code as possible.

Now, don't get me wrong, we should not be getting ourselves into code golf situations where we are compacting code and saving keystrokes to the point where the code you produce is no longer understandable. When you think about it though the fact that most of your time will be maintaining the terrible code you wrote, writing less code and not creating more work for yourself will start to look like an exceptionally good idea.

Why?

Look around, look at your tools, look at the framework you likely use to produce that thing you work on. Everything you do, all the tools you use, everything is trying make you write less code. Your tools are trying to make you less error prone, and abstracting away the things that will cause you trouble down the road.

So why should you write less code? When I said earlier that your job was to write less code, I was fibbing a little bit. Really your job is to think, your job is to think about the problem at hand, devise an elegant solution and then turn that solution into software. One of the byproducts of creating software is that you have to write code to solve problems.

the thinker

Code is a by-product

Code isn't that important. We love to convince ourselves that it is because as we actually start executing and turning our solutions into software, it seems like writing code is both a means and an end unto itself. We talk about our editors, we talk about our language of choice and we talk about our environments. I do it, you do it, we all do it. There is no shame in enjoying the craft of the work you do. The trick is to stay focused. Code is awesome, but its also the enemy, it takes time to write, it can be fragile, it can be unclear and not particularly robust.

This is why I stick with the mantra "Write less code". Whenever I feel like something is getting bigger than it should be or more complicated than it should be, I say "Write less code". Whenever I write so much code that it gets hard to keep it in context, I refactor, I clean it up, all the while, thinking about "Write less code".

As much as it pains me to say, sometimes there is no way to get around writing a bunch of code, but if you can't be concise, be clear. Writing less code is writing clean code. Writing less code is also about writing clear code. When you are economical with the code you write the purpose of your code becomes clear.

So, whats the lesson here? Most importantly I think the lesson here is that code is a by-product, its an unavoidable thing that you generate in the process of doing your job. So think more,refactor more, remove some old code and write less new code, do yourself a favor and start this today.

posted @ 2014-02-15 10:43  kira2will  阅读(190)  评论(0编辑  收藏  举报