⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 http:^^www.cs.princeton.edu^courses^cs217^etc^pike.html

📁 This data set contains WWW-pages collected from computer science departments of various universities
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<h2>Procedure names</h2>Procedure names should reflect what they do; function names shouldreflect what they<em>return</em>.Functions are used in expressions, often in things likeif's,so they need to read appropriately.<pre>if(checksize(x))</pre>is unhelpful because we can't deduce whether <tt>checksize</tt> returns true onerror or non-error; instead<pre>if(validsize(x))</pre>makes the point clear and makes a future mistake in using the routineless likely.<p><h2>Comments</h2>A delicate matter, requiring taste and judgement.I tend to err on the side of eliminating comments, for several reasons.First, if the code is clear, and uses good type names and variable names,it should explain itself.Second, comments aren't checked by the compiler,so there is no guarantee they're right,especially after the code is modified.A misleading comment can be very confusing.Third, the issue of typography: comments clutter code.<p>But I do comment sometimes.Almost exclusively, I use them as an introduction to what follows.Examples: explaining the use of global variables and types(the one thing I always comment in large programs);as an introduction to an unusual or critical procedure;or to mark off sections of a large computation.<p>There is a famously bad comment style:<pre>i=i+1;        /* Add one to i */</pre>and there are worse ways to do it:<pre>/********************************** *                                * *          Add one to i          * *                                * **********************************/               i=i+1;</pre>Don't laugh now, wait until you see it in real life.<p>Avoid cute typography in comments,avoid big blocks of comments except perhaps before vitalsections like the declaration of the central data structure(comments on data are usually much more helpful thanon algorithms);basically, avoid comments.If your code needs a comment to be understood, it would be betterto rewrite it so it's easier to understand.Which brings us to<p><h2>Complexity</h2>Most programs are too complicated - that is,more complex than they need to be to solve their problems efficiently.Why?Mostly it's because of bad design,but I will skip that issue here because it's a big one.But programs are often complicatedat the microscopic level,and that is something I can address here.<p><dl><dt>Rule 1.You can't tell where a program is going to spend its time.Bottlenecks occur in surprising places, so don'ttry to second guess and put in a speed hack until you'veproven that's where the bottleneck is.<p><dt>Rule 2.Measure.Don't tune for speed until you've measured,and even then don't unless one part of the code<em>overwhelms</em>the rest.<p><dt>Rule 3.Fancy algorithms are slow when<em>n</em>is small, and<em>n</em>is usually small.Fancy algorithms have big constants.Until you know that<em>n</em>is frequently going to be big,don't get fancy.(Even if<em>n</em>does get big, use Rule 2 first.)For example, binary trees are always faster than splay trees forworkaday problems.<p><dt>Rule 4.Fancy algorithms are buggier than simple ones,and they're much harder to implement.Use simple algorithms as well as simple data structures.<p>The following data structures are a complete list for almostall practical programs:<dl><dt>array<dt>linked list<dt>hash table<dt>binary tree</dl>Of course, you must also be prepared to collect these into compounddata structures.For instance, a symbol table might be implemented as a hash tablecontaining linked lists of arrays of characters.<p><dt>Rule 5.Data dominates.If you've chosen the right data structures and organized things well,the algorithms will almost always be self-evident.Data structures, not algorithms, are central to programming.(See Brooks p. 102.)<p><dt>Rule 6.There is no Rule 6.<p></dl><h2>Programming with data</h2>Algorithms, or details of algorithms,can often be encoded compactly, efficiently and expressively as datarather than, say, as lots of<tt>if</tt>statements.The reason is that the<em>complexity</em>of the job at hand, if it is due to a combination ofindependent details,<em>can be encoded</em>.A classic example of this is parsing tables,which encode the grammar of a programming languagein a form interpretable by a fixed, fairly simplepiece of code.Finite state machines are particularly amenable to thisform of attack, but almost any program that involvesthe `parsing' of some abstract sort of input into a sequenceof some independent `actions' can be constructed profitablyas a data-driven algorithm.<p>Perhaps the most intriguing aspect of this kind of designis that the tables can sometimes be generated by anotherprogram - a parser generator, in the classical case.As a more earthy example, if an operating system is drivenby a set of tables that connect I/O requests to the appropriatedevice drivers, the system may be `configured'by a program that reads a description of the particulardevices connected to the machine in question and printsthe corresponding tables.<p>One of the reasons data-driven programs are not common, at leastamong beginners, is the tyranny of Pascal.Pascal, like its creator, believes firmly in the separationof code and data.It therefore (at least in its original form) has no ability tocreate initialized data.This flies in the face of the theories of Turing and von Neumann,which define the basic principles of the stored-program computer.Code and data<em>are</em>the same, or at least they can be.How else can you explain how a compiler works?(Functional languages have a similar problem with I/O.)<p><h2>Function pointers</h2>Another result of the tyranny of Pascal is that beginners don't usefunction pointers.(You can't have function-valued variables in Pascal.)Using function pointers to encode complexity has some interestingproperties.<p>Some of the complexity is passed to the routine pointed to.The routine must obey some standard protocol - it's one of a setof routines invoked identically - but beyond that, what itdoes is its business alone.The complexity is<em>distributed</em>.<p>There is this idea of a protocol, in that all functions used similarlymust behave similarly. This makes for easy documentation, testing,growth and even making the program run distributed over a network -the protocol can be encoded as remote procedure calls.<p>I argue that clear use of function pointers is the heart of object-orientedprogramming. Given a set of operations you want to perform on data,and a set of data types you want to respond to those operations,the easiest way to put the program together is with a group of functionpointers for each type.This, in a nutshell, defines class and method.The O-O languages give you more of course - prettier syntax,derived types and so on - but conceptually they provide little extra.<p>Combining data-driven programs with function pointers leads to anastonishingly expressive way of working, a way that, in my experience,has often led to pleasant surprises. Even without a special O-Olanguage, you can get 90% of the benefit for no extra work and bemore in control of the result.I cannot recommend an implementation style more highly.All the programs I have organized this way have survived comfortablyafter much development - far better than with less disciplinedapproaches.Maybe that's it: the discipline it forces pays off handsomely in the long run.<p><h2>Include files</h2>Simple rule: include files should never include include files.If instead they state (in comments or implicitly) what files they need to haveincluded first, the problem of deciding which files to includeis pushed to the user (programmer) but in a way that's easy to handleand that, by construction, avoids multiple inclusions.Multiple inclusions are a bane of systems programming.It's not rare to have files included five or more times tocompile a single C source file.The Unix<tt>/usr/include/sys</tt>stuff is terrible this way.<p>There's a little dance involving<tt>#ifdef</tt>'sthat can prevent a file being read twice, but it's usually donewrong in practice - the<tt>#ifdef</tt>'sare in the file itself, not the file that includes it.The result is often thousands of needless lines of codepassing through the lexical analyzer, which is (in good compilers)the most expensive phase.<p>Just follow the simple rule.<p><hr><address>Rob Pike / rob@research.att.com / Sep. 1987</address></BODY>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -