薛蟠为什么怕夏金桂:想写程序吗?远离你的计算机!

来源:百度文库 编辑:中财网 时间:2024/04/28 05:51:43
.pcb{margin-right:0}

 想写程序吗?远离你的计算机! 

      DeadFire 发表于 2011-3-31 15:15
        我最近想明白了一些事情。最好的写程序的地方不是在你的计算机前,不是使用你的编译器、IDE 或其它一些工具。这最好的地方是一个远离这些工具的场所 —— 是某个能让你认真的思考的地方。对于一个你很熟悉的编程语言,你很容易把你脑子里已经构思好的程序转换成编译器/解释器可以编译/解释的程序—— 难就难在如何在脑子里先把程序编好。

  有一天我乘火车旅行。我带了笔记本,但没有网络。不幸的是,我使用的是一种商业编程语言(IDL,真不巧),这种语言需要使用我大学网站上的许可证。因为不能连上互联网,我拿不到许可证,于是我的编译器和IDE就跑不起来。你可能会喜欢使用一个需要昂贵的许可证的商业编程语言,但它的确使我不能在编辑器里写任何的代码。而你猜这么着 … 这让我开始思考!

  我想这篇博客正好印证了 Does Visual Studio rot the mind? 这篇博客的内容,它里面说道:

我从我的第一个老板那里学到的最大的一个教训是:“当你的程序运行异常时,不要使用 debug 工具,用你的大脑。”

  这就是远离电脑后会迫使你去做的事。通常你很容易进入这样一种编程习惯:

  • 写一些程序(乱糟糟的)
  • 编译,运行
  • 使用简单的测试用例进行测试
  • 发现有问题
  • 小改一下程序,也许能解决这个问题
  • 重复 …

  这当然会导致程序最终乱糟糟的,无法阅读理解,很有可能问题多多,没有很好的测试。

  远离计算机能迫使你在大脑里周全的思考所有的问题 —— 这跟你抱着计算机编译、运行你的程序相比可能会花更长的时间(至少对于小程序是这样)。可是,这样做后,你就不会去一点一点的改动程序、一遍遍试着运行它们了,你是真正的思考这些代码是做什么的。在那天、在火车上之前,我还从没有在纸上规划过任何的程序。

  而从此之后我努力在写程序之前会把思路画在纸上,思考它,一步一步的用代码实现,做出高质量的,高效的,没有问题的程序,多去思考,最后才在编译器上运行。程序是最后才从纸上拷贝到编译器里。

  任务完成的很好——我自认为,希望这是一个有用的建议。

原文:Want to write some code? Get away from your computer!
译文:http://www.aqee.net/2011/03/31/w ... from-your-computer/

March 19, 2011

Want to write some code? Get away from your computer!

I’ve recently realised something. The best place to write code isn’t in front of your computer, with your compiler, IDE and tools. The best place to write code is far, far away from any of these tools – somewhere where you can think properly. For a language with which you are fairly familiar, the mechanics of translating the program in your mind to a program that the compiler can compile (or the interpreter can interpret) is fairly easy – it’s coming up with that program in your mind which is hard.

The other day I was on a train journey. I had my laptop, but no internet. Unfortunately I was using a commercial programming language (IDL, as it happens) for which I need to use my university’s site license. As I didn’t have access to the internet, I couldn’t get hold of the site license, so couldn’t run the compiler and IDE. Say what you like about commercial programming languages which require expensive licenses, but it stopped me from actually writing code in my editor with the compiler. And…guess what…it actually made me think!

I guess this post is somewhat along the lines of Does Visual Studio rot the mind? and the following quote:

One of the best lessons I learnt from my first boss was: “when your code doesn’t behave as expected, don’t use the debugger, think.”

That is what being away from your compiler forces you to do. It’s very easy to slip into the mindset of:

  1. Write a bit of (fairly bad) code
  2. Compile and run
  3. Test with a poorly chosen test case
  4. Find it doesn’t work
  5. Make small change to the code on the off-chance that it might solve the problem
  6. Repeat…

Of course this leads to code in the end that is ill-understood by the programmer, probably fairly buggy and not well tested.

Being away from the computer forces you to run through all of the thoughts in your head – which tends to take longer than getting a computer to compile and run your code (for small code bases at least…). So you don’t tend to make tiny changes and re-run things, you tend to actually think about what the code is doing. Until I did this on the train the other day, I hadn’t actually run a piece of code on paper (that is, written down columns for each of the variables and worked out what each value will be at each stage in the program) since my Computing A-Level exam!

In the case of the code I was writing the other day, I managed to produce some high quality, fast, bug-free code by writing it in long-hand on a piece of paper, thinking about it, gradually typing up bits of it, thinking some more, and then after a long time trying it in the compiler. The code (which was some region-growing image segmentation code which involved lots of recursion) was eventually copied from my piece of paper to my IDE, compiled (with only one syntax error – impressive I think) and ran correctly first time (and completed all of the tests that I had also devised on paper).

Job well done, I think, and a useful piece of advice, I hope.