Paul Graham: Essays 2024年11月25日
Holding a Program in One's Head
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文探讨了优秀程序员的工作方式,强调了将代码“装入脑海”的重要性,以及保持专注、连续工作、使用简洁语言、重构代码等技巧。文章指出,优秀程序员往往能通过一些看似偶然的方式达到最佳工作状态,而大型组织的运作方式却往往与之相悖。作者认为,组织应认识到优秀程序员的工作方式并非任性,而是源于编程工作的特殊性,并尝试建立一种新的组织形式,鼓励和支持个体创造力,而非将其视为可替换的零件。

🤔 **将代码“装入脑海”是优秀程序员的关键能力:**就像数学家能够在脑海中构建和操作数学问题一样,优秀的程序员能够将整个程序“装入脑海”,从而灵活地修改和调整代码,并深刻理解其背后的问题。

🚧 **避免干扰和保持专注是提升编程效率的关键:**干扰会打乱程序员的思维,导致代码“卸载”,因此避免干扰,特别是那些需要长时间思考和集中注意力的任务,才能保持思维的连续性,提高编程效率。

⏱️ **连续工作和分段休息可以提高编程效率:**每次开始工作都需要重新“加载”程序,因此连续工作可以减少加载成本,提高效率。但长时间工作也会导致疲劳,因此需要合理安排休息,甚至利用休息时间让潜意识发挥作用。

🗣️ **使用简洁的编程语言和自顶向下的编程风格可以简化程序结构:**简洁的语言和结构能够帮助程序员更容易地“装入”和“操作”代码,而自顶向下的编程风格则可以将复杂问题分解成更易于理解的模块,降低认知负担。

🧑‍🤝‍🧑 **小团队协作和代码所有权可以促进项目快速迭代:**团队规模过大或多人编辑同一部分代码,都会影响程序员对代码的理解和掌控,不利于快速迭代和创新。因此,在项目初期,最好由少数人甚至一个人负责开发,并将其分解成独立的模块进行开发。

🌱 **从小项目开始,逐步扩展项目规模:**从一个小问题或子集开始,逐步扩展项目规模,可以帮助程序员更容易地理解和掌握整个程序,避免一开始就面对过于复杂的问题而陷入困境。

August 2007A good programmer working intensively on his own code can hold itin his mind the way a mathematician holds a problem he's workingon. Mathematicians don't answer questions by working them out onpaper the way schoolchildren are taught to. They do more in theirheads: they try to understand a problem space well enough that theycan walk around it the way you can walk around the memory of thehouse you grew up in. At its best programming is the same. Youhold the whole program in your head, and you can manipulate it atwill.That's particularly valuable at the start of a project, becauseinitially the most important thing is to be able to change whatyou're doing. Not just to solve the problem in a different way,but to change the problem you're solving.Your code is your understanding of the problem you're exploring.So it's only when you have your code in your head that you reallyunderstand the problem.It's not easy to get a program into your head. If you leave aproject for a few months, it can take days to really understand itagain when you return to it. Even when you're actively working ona program it can take half an hour to load into your head when youstart work each day. And that's in the best case. Ordinaryprogrammers working in typical office conditions never enter thismode. Or to put it more dramatically, ordinary programmers workingin typical office conditions never really understand the problemsthey're solving.Even the best programmers don't always have the whole program they'reworking on loaded into their heads. But there are things you cando to help: Avoid distractions. Distractions are bad for many types of work, but especially bad for programming, because programmers tend to operate at the limit of the detail they can handle.The danger of a distraction depends not on how long it is, but on how much it scrambles your brain. A programmer can leave the office and go and get a sandwich without losing the code in his head. But the wrong kind of interruption can wipe your brain in 30 seconds.Oddly enough, scheduled distractions may be worse than unscheduled ones. If you know you have a meeting in an hour, you don't even start working on something hard. Work in long stretches. Since there's a fixed cost each time you start working on a program, it's more efficient to work in a few long sessions than many short ones. There will of course come a point where you get stupid because you're tired. This varies from person to person. I've heard of people hacking for 36 hours straight, but the most I've ever been able to manage is about 18, and I work best in chunks of no more than 12.The optimum is not the limit you can physically endure. There's an advantage as well as a cost of breaking up a project. Sometimes when you return to a problem after a rest, you find your unconscious mind has left an answer waiting for you. Use succinct languages. More powerful programming languages make programs shorter. And programmers seem to think of programs at least partially in the language they're using to write them. The more succinct the language, the shorter the program, and the easier it is to load and keep in your head.You can magnify the effect of a powerful language by using a style called bottom-up programming, where you write programs in multiple layers, the lower ones acting as programming languages for those above. If you do this right, you only have to keep the topmost layer in your head. Keep rewriting your program. Rewriting a program often yields a cleaner design. But it would have advantages even if it didn't: you have to understand a program completely to rewrite it, so there is no better way to get one loaded into your head. Write rereadable code. All programmers know it's good to write readable code. But you yourself are the most important reader. Especially in the beginning; a prototype is a conversation with yourself. And when writing for yourself you have different priorities. If you're writing for other people, you may not want to make code too dense. Some parts of a program may be easiest to read if you spread things out, like an introductory textbook. Whereas if you're writing code to make it easy to reload into your head, it may be best to go for brevity. Work in small groups. When you manipulate a program in your head, your vision tends to stop at the edge of the code you own. Other parts you don't understand as well, and more importantly, can't take liberties with. So the smaller the number of programmers, the more completely a project can mutate. If there's just one programmer, as there often is at first, you can do all-encompassing redesigns. Don't have multiple people editing the same piece of code. You never understand other people's code as well as your own. No matter how thoroughly you've read it, you've only read it, not written it. So if a piece of code is written by multiple authors, none of them understand it as well as a single author would.And of course you can't safely redesign something other people are working on. It's not just that you'd have to ask permission. You don't even let yourself think of such things. Redesigning code with several authors is like changing laws; redesigning code you alone control is like seeing the other interpretation of an ambiguous image.If you want to put several people to work on a project, divide it into components and give each to one person. Start small. A program gets easier to hold in your head as you become familiar with it. You can start to treat parts as black boxes once you feel confident you've fully explored them. But when you first start working on a project, you're forced to see everything. If you start with too big a problem, you may never quite be able to encompass it. So if you need to write a big, complex program, the best way to begin may not be to write a spec for it, but to write a prototype that solves a subset of the problem. Whatever the advantages of planning, they're often outweighed by the advantages of being able to keep a program in your head.It's striking how often programmers manage to hit all eight pointsby accident. Someone has an idea for a new project, but becauseit's not officially sanctioned, he has to do it in off hours—whichturn out to be more productive because there are no distractions.Driven by his enthusiasm for the new project he works on it formany hours at a stretch. Because it's initially just anexperiment, instead of a "production" language he uses a mere"scripting" language—which is in fact far more powerful. Hecompletely rewrites the program several times; that wouldn't bejustifiable for an official project, but this is a labor of loveand he wants it to be perfect. And since no one is going to seeit except him, he omits any comments except the note-to-self variety.He works in a small group perforce, because he either hasn't toldanyone else about the idea yet, or it seems so unpromising that noone else is allowed to work on it. Even if there is a group, theycouldn't have multiple people editing the same code, because itchanges too fast for that to be possible. And the project startssmall because the idea is small at first; he just has some coolhack he wants to try out.Even more striking are the number of officially sanctioned projectsthat manage to do all eight things wrong. In fact, if you look atthe way software gets written in most organizations, it's almostas if they were deliberately trying to do things wrong. In a sense,they are. One of the defining qualities of organizations sincethere have been such a thing is to treat individuals as interchangeableparts. This works well for more parallelizable tasks, like fightingwars. For most of history a well-drilled army of professionalsoldiers could be counted on to beat an army of individual warriors,no matter how valorous. But having ideas is not very parallelizable.And that's what programs are: ideas.It's not merely true that organizations dislike the idea of dependingon individual genius, it's a tautology. It's part of the definitionof an organization not to. Of our current concept of an organization,at least.Maybe we could define a new kind of organization that combined theefforts of individuals without requiring them to be interchangeable.Arguably a market is such a form of organization, though it may bemore accurate to describe a market as a degenerate case—as whatyou get by default when organization isn't possible.Probably the best we'll do is some kind of hack, like making theprogramming parts of an organization work differently from the rest.Perhaps the optimal solution is for big companies not even to tryto develop ideas in house, but simply to buy them. But regardlessof what the solution turns out to be, the first step is to realizethere's a problem. There is a contradiction in the very phrase"software company." The two words are pulling in opposite directions.Any good programmer in a large organization is going to be at oddswith it, because organizations are designed to prevent whatprogrammers strive for.Good programmers manage to get a lot done anyway. But often itrequires practically an act of rebellion against the organizationsthat employ them. Perhaps it will help if more people understand that the wayprogrammers behave is driven by the demands of the work they do.It's not because they're irresponsible that they work in long bingesduring which they blow off all other obligations, plunge straight intoprogramming instead of writing specs first, and rewrite code thatalready works. It's not because they're unfriendly that they preferto work alone, or growl at people who pop their head in the doorto say hello. This apparently random collection of annoying habitshas a single explanation: the power of holding a program in one'shead.Whether or not understanding this can help large organizations, itcan certainly help their competitors. The weakest point in bigcompanies is that they don't let individual programmers do greatwork. So if you're a little startup, this is the place to attackthem. Take on the kind of problems that have to be solved in onebig brain.Thanks to Sam Altman, David Greenspan, Aaron Iba, Jessica Livingston,Robert Morris, Peter Norvig, Lisa Randall, Emmett Shear, Sergei Tsarev,and Stephen Wolfram for reading drafts of this.

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

程序员 编程 代码 组织 创造力
相关文章