10-07-2012, 11:41 AM
The Practice of Programming
A.W.The.Practice.of.Programming.B.W.Kernighan.&.Rob.Pike.pdf (Size: 4.83 MB / Downloads: 160)
With the same insight and authority that made their book The Unix Programming
Environment a classic, Brian Kernighan and Rob Pike have written The Practice
of Programming to help make individual programmers more effective and
productive.
The practice of programming is more than just writing code. Programmers must
also assess tradeoffs, choose among design alternatives, debug and test, improve
performance, and maintain software written by themselves and others. At the
same time, they must be concerned with issues like compatibility, robustness,
and reliability, while meeting specifications.
The Practice of Programming covers all these topics, and more. This book is full
of practical advice and real-world examples in C, C++, lava, and a variety of
special-purpose languages. It includes chapters on:
debugging: finding bugs quickly and methodically
testing: guaranteeing that software works correctly and reliably
performance: making programs faster and more compact
portability: ensuring that programs run everywhere without change
design: balancing goals and constraints to decide which algorithms and data
structures are best
interfaces: using abstraction and information hiding to control the interactions
between components
style: writing code that works well and is a pleasure to read
notation: choosing languages and tools that let the machine do more of the
work
Kernighan and Pike have distilled years of experience writing programs,
teaching, and working with other programmers to create this book. Anyone who
writes software will profit from the principles and guidance in The Practice of
Programming.
Expressions and Statements
By analogy with choosing names to aid the reader's understanding, write expressions
and statements in a way that makes their meaning as transparent as possible.
Write the clearest code that does the job. Use spaces around operators to suggest
grouping; more generally, format to help readability. This is trivial but valuable, like
keeping a neat desk so you can find things. Unlike your desk, your programs are
likely to be examined by others.
Consistency and Idioms
Consistency leads to better programs. If formatting varies unpredictably, or a loop
over an array runs uphill this time and downhill the next, or strings are copied with
strcpy here and a f o r loop there, the variations make it harder to see what's really
going on. But if the same computation is done the same way every time it appears,
any variation suggests a genuine difference, one worth noting.
Use a consistent indentation and brace style. Indentation shows structure, but which
indentation style is best? Should the opening brace go on the same line as the i f or
on the next? Programmers have always argued about the layout of programs, but the
specific style is much less important than its consistent application. Pick one style,
preferably ours, use it consistently, and don't waste time arguing.
Should you include braces even when they are not needed? Like parentheses,
braces can resolve ambiguity and occasionally make the code clearer. For consistency,
many experienced programmers always put braces around loop or if bodies.
But if the body is a single statement they are unnecessary, so we tend to omit them. If
you also choose to leave them out, make sure you don't drop them when they are
needed to resolve the "dangling else" ambiguity exemplified by this excerpt:
Function Macros
There is a tendency among older C programmers to write macros instead of functions
for very short computations that will be executed frequently; 110 operations such
as getchar and character tests like i sdi gi t are officially sanctioned examples. The
reason is performance: a macro avoids the overhead of a function call. This argument
was weak even when C was first defined, a time of slow machines and expensive
function calls; today it is irrelevant. With modern machines and compilers, the drawbacks
of function macros outweigh their benefits.