プログラム書法
ファイル:The Elements of Programming Style.jpg | |
著者 | Brian W. Kernighan and P. J. Plauger
木村泉(訳) |
---|---|
出版 | McGraw-Hill
共立出版(訳本) |
出版年
|
1978
1982(訳本) |
頁数 | 168 ページ |
ISBN | 978-0070342071 |
プログラミング・スタイルの要素(邦訳「プログラム書法」)は、プログラム書法の手引き書であり、コンピュータ・プログラムがコンパイラや個人のプログラミング「スタイル」を満足させるに留まらず、人間にとっての「読みやすさ」、特にソフトウェア保守エンジニア、プログラマー、テクニカルライターによる理解を重視すべきだという考え方を提唱した。原著初版は1974年に出版された。
本書はタイトルと文体に、ストランク&ホワイト著の『スタイルの要素』へのオマージュを表し、エドガー・ダイクストラの構造化プログラミングの議論の実践的な土台とみなされている。この本は大きな影響力を持ち、The Elements of C Programming Style, The Elements of C# Style, The Elements of Java(TM) Style, The Elements of MATLAB Style (いずれも邦訳なし) など、個々の言語に合わせた同様の教本を生み出している。
本書は、プログラミングの教科書に実際に掲載された短いプログラムを例題として取り上げており、その結果、抽象的で学術的な議論というよりは、実践的な手法となっている。本書の書き方はそつがなく、全般的に同情的な批評でありつつ、遠慮なく切り込んでいる。誤りのいくつかは自身の著書のものである(一つは第1版の誤りを第2版で取り上げた)。
格言[編集]
教訓は、各セクションの最後に「汚れ仕事は機械にやらせよう」といった簡潔な格言としてまとめられている。※現代語に一部置き換えたため、訳本の訳とは必ずしも一致しない。尚、functionは「関数」と訳されるが「機能」の意をもち、両義的に使われるため、直訳では原意を十分に伝えきれない。
- Write clearly – don't be too clever. わかりやすく書こう - うますぎるプログラムはいけない
- Say what you mean, simply and directly. いいたいことを単純素直にいおう
- Use library functions whenever feasible. ライブラリ関数を使おう
- Avoid too many temporary variables. 一時変数はなるべく使わないこと
- Write clearly – don't sacrifice clarity for efficiency. わかりやすく書こう - 効率のためにわかりやすさを犠牲にしてはいけない
- Let the machine do the dirty work. 汚れ仕事は機械にやらせよう
- Replace repetitive expressions by calls to common functions. 同じ表現の繰り返しは共通関数の呼び出しに変えよう
- Parenthesize to avoid ambiguity. 括弧を使って誤解を避けよう
- Choose variable names that won't be confused. 混同の恐れのない名前を使おう
- Avoid unnecessary branches. 無用な分岐は避けよう
- If a logical expression is hard to understand, try transforming it. 論理表現がわかりにくいときは表現を変えよう
- Choose a data representation that makes the program simple. プログラムが単純になるようデータ表現を選ぼう
- Write first in easy-to-understand pseudo language; then translate into whatever language you have to use. わかりやすい擬似コードで書いてから実際の言語に落とそう
- Modularize. Use procedures and functions. モジュール化し、手続きと関数を使おう
- Avoid gotos completely if you can keep the program readable. 読みやすさを保てる限り、GOTO文は避けよう
- Don't patch bad code – rewrite it. ダメなコードを直すのはやめて、書き直そう
- Write and test a big program in small pieces. 大きいプログラムは小さい部分に分けて書き、テストしよう
- Use recursive procedures for recursively-defined data structures. 再帰的に定義されたデータ構造には再起手続きを使おう
- Test input for plausibility and validity. 入力の妥当性と有効性をテストしよう
- Make sure input doesn't violate the limits of the program. 入力がプログラムの限界を超えていないことを確認しよう
- Terminate input by end-of-file marker, not by count. 入力はファイル終端で終了するようにし、回数指定に頼らない
- Identify bad input; recover if possible. ダメな入力を特定し、できれば回復しよう
- Make input easy to prepare and output self-explanatory. 入力データは準備しやすく、出力はわかりやすく
- Use uniform input formats. 共通の入力フォーマットを使おう
- Make input easy to proofread. 簡単に確認できる入力形式に設計しよう
- Use self-identifying input. Allow defaults. Echo both on output. 意味のわかりやすい入力を使おう。デフォルトを許容しよう。出力時に両方とも表示しよう。
- Make sure all variables are initialized before use. 全ての変数を使用前に初期化しよう
- Don't stop at one bug. バグをひとつ見つけたところで止まってはいけない
- Use debugging compilers. デバッグ機能付のコンパイラを使おう
- Watch out for off-by-one errors. 桁ずれのエラーに注意
- Take care to branch the right way on equality. 条件分岐時の等号を正しく扱おう
- Be careful if a loop exits to the same place from the middle and the bottom. ループの真ん中や最後から同じ所に抜けるときは気をつけよう
- Make sure your code does "nothing" gracefully. 何もしないはずのコードが余計なことをしないようにしよう
- Test programs at their boundary values. 境界値でテストしよう
- Check some answers by hand. 出力のいくつかを正解と比べて確認しよう
- 10.0 times 0.1 is hardly ever 1.0. 10.0×0.1が1.0になると考えてはいけない
- 7/8 is zero while 7.0/8.0 is not zero. 7/8はゼロだが7.0/8.0はゼロではない ※整数演算と小数演算に注意
- Don't compare floating point numbers solely for equality. 浮動小数点数が等しいかどうかだけで比較してはならない ※同値に複数の表現があり得る
- Make it right before you make it faster. 速くする前に正しくしよう
- Make it fail-safe before you make it faster. 速くする前にフェールセーフにしよう
- Make it clear before you make it faster. 速くする前にわかりやすくしよう
- Don't sacrifice clarity for small gains in efficiency. わずかな効率向上のためにわかりやすさを犠牲にしてはいけない
- Let your compiler do the simple optimizations. 簡単な最適化はコンパイラに任せよう
- Don't strain to re-use code; reorganize instead. コードを兼用しようとせず、組み直そう
- Make sure special cases are truly special. 特例が本当に特例かを確認しよう
- Keep it simple to make it faster. 速くする前に単純さを保とう
- Don't diddle code to make it faster – find a better algorithm. 速くするためにはコードをこねくり回すのでなく、よりよいアルゴリズムを探そう
- Instrument your programs. Measure before making efficiency changes. 効率改善の前にプログラムを計測しよう
- Make sure comments and code agree. コメントとコードを一致させよう
- Don't just echo the code with comments – make every comment count. コメントはコードのオウム返しではなく、意味あるものにしよう
- Don't comment bad code – rewrite it. ダメなコードにコメントするのはやめ、コードを書き直そう
- Use variable names that mean something. 意味のわかる変数名をつけよう
- Use statement labels that mean something. 意味のわかるステートメントラベルをつけよう
- Format a program to help the reader understand it. 読みやすいようにプログラムをレイアウトしよう
- Document your data layouts. データのレイアウトをドキュメント化しよう
- Don't over-comment. コメントしすぎないようにしよう
現代の読者は、この本の例題が、現在はあまり使われなくなった古い手続き型プログラミング言語(FortranやPL/I)を取り上げていることを欠点と感じるかもしれない。本書が書かれたときには、現在の言語はほとんどなかった。しかし、一般的にスタイルや構造的な問題に関わる本書の指摘の多くは、特定の言語の詳細を超越していると言って良い。
受容[編集]
キロボー・マイクロコンピューティングには、「他社が使うプログラムを書くなら、この本を読むべきだ。プロのプログラマーになるつもりなら、この本は必読だ」と述べられている。[1]
参考文献[編集]
- ^ Reeves, Jack W. (1980年1月). “The Elements of Programming Style”. Kilobaud Microcomputing: pp. 9 2014年6月23日閲覧。
- B. W. Kernighan and P. J. Plauger, The Elements of Programming Style, McGraw-Hill, New York, 1974. ISBN 0-07-034199-0ISBN 0-07-034199-0
- B. W. Kernighan and P. J. Plauger, The Elements of Programming Style 2nd Edition, McGraw Hill, New York, 1978. ISBN 0-07-034207-5ISBN 0-07-034207-5