How To Write Unmaintainable Code
How To Write Unmaintainable Code
メンテ不可能なコードの書き方
Ensure a job for life ;-)
Roedy Green
Canadian Mind Products
Introduction (はじめに)
Never ascribe to malice, that which can be explained by incompetence.
(無能で説明がつくことを陰謀のせいにしてはならない)
- Napoleon
訳注:この言葉は「ハンロンの剃刀」(Hanlon's razor - Wikipedia, the free encyclopedia)
といわれるもので、ナポレオンではなく
Robert J. Hanlon
の言葉というのが本当のようです。
ref: 神は細部に宿り給う
In the interests of creating employment opportunities in the Java programming field, I
am passing on these tips from the masters on how to write code that is so difficult to
maintain, that the people who come after you will take years to make even the simplest
changes. Further, if you follow all these rules religiously, you will even guarantee
yourself a lifetime of employment, since no one but you has a hope in hell of
maintaining the code. Then again, if you followed all these rules religiously, even
you wouldn't be able to maintain the code!
Javaプログラミングの分野での雇用機会の創出のために、わたしはメンテナンスを困難にするコ
ードの書き方についての単純極まる変更でさえもそれを行うのに何年も掛かってしまうような
tips を masters から授かりました。さらにこのルール全てにあなたが religiously に従えば
あなたは lifetime of employment を手に入れることになるでしょう。なぜなら、あなた以外の
誰であってもそのコードのメンテナンスをすることが絶望的だからです。そして繰り返しますが、
このルール全てにあなたが religiously に従えばあなた自身でさえもそのコードのメンテナン
スができなくなってしまうでしょう!
You don't want to overdo this. Your code should not look hopelessly unmaintainable,
just be that way. Otherwise it stands the risk of being rewritten or refactored.
あなたはこれをやりすぎることは望まないでしょう。あなたのコードは絶望的なほどメンテナ
ンス不可能 (hopelessly unmaintainable) なもののように見えるべきではありません。just be
that way. さもなければ、書き直されたりリファクタリングされる危険が残るでしょう。
General Principles (汎用的な原則)
Quidquid latine dictum sit, altum sonatur.
- Whatever is said in Latin sounds profound.
(ラテン語で言われたことは説得力ありげに聞こえる)
To foil the maintenance programmer, you have to understand how he thinks. He has your
giant program. He has no time to read it all, much less understand it. He wants to
rapidly find the place to make his change, make it and get out and have no unexpected
side effects from the change.
保守プログラマーを撃退するためには、彼がどのように思考するのかをあなたが理解する必要が
あります。彼はあなたの巨大プログラムを抱えています。彼にはそれを全部読む時間もないし、
理解する時間はもっとありません。彼は自分が行う変更の該当箇所をすばやく見つけて変更を
行って、それによって意図しない副作用が現れないことを望んでいています。
He views your code through a toilet paper tube. He can only see a tiny piece of your
program at a time. You want to make sure he can never get at the big picture from
doing that. You want to make it as hard as possible for him to find the code he is
looking for. But even more important, you want to make it as awkward as possible for
him to safely ignore anything.
彼はあなたのコードを toilet paper tube を通して見ています。彼はあなたのプログラムを、
一度にそのほんの一部 (tiny piece)しか見ることができません。あなたは彼が決して get at
the big picture from doing that なことを望んでいます。あなたは彼が探そうとしているコー
ドを見つけ出すことが可能な限り困難になるようにすることを望んでいます。しかしさらに重要
なのは、彼が安全にすべてを無視するように仕向けるために可能な限り彼を awkward にしてし
まいたいとあなたが望んでいるということです。
Programmers are lulled into complacency by conventions. By every once in a while, by
subtly violating convention, you force him to read every line of your code with a
magnifying glass.
プログラマーたちは規約によって complacency に lulled されています。あなた時折意図的に
規約を破ることによってあなたのコードの一行ごとを拡大鏡を使って読むことを彼に強制します。
You might get the idea that every language feature makes code unmaintainable -- not so,
only if properly misused.
あなたは、適切に誤用したときにだけコードをメンテナンス不能にしてしまう言語機能のすべて
についてのアイデアを得られるかもしれません。
Naming (命名規則)
"When I use a word," Humpty Dumpty said, in a rather scornful tone,
"it means just what I choose it to mean - neither more nor less."
- Lewis Carroll -- Through the Looking Glass, Chapter 6
Much of the skill in writing unmaintainable code is the art of naming variables and
methods. They don't matter at all to the compiler. That gives you huge latitude to use
them to befuddle the maintenance programmer.
メンテ不能なコードを書くスキルの大半は 「the art of naming variables and methods」
(変数やメソッドの命名規則の技法) です。それに従ってつけられた名前はコンパイラーにはま
ったく意味がありません。それはあなたに対して、保守を行うプログラマーを混乱に陥れる
ためのとても大きな latitude (自由度) をもたらします。
New Uses For Names For Baby
Buy a copy of a baby naming book and you'll never be at a loss for variable names.
Fred is a wonderful name, and easy to type. If you're looking for easy-to-type
variable names, try adsf or aoeu if you type with a DSK keyboard.
赤ちゃんに名前をつけるための本を購入すれば変数の名前に困るようなこととはなくな
ります。Fred というのはタイプしやすいのでとてもいい名前です。もしあなたがタイ
プしやすい変数名を探していて、DSK キーボードでタイプしているのなら asdf や
aoeu を試してみましょう。
Single Letter Variable Names (一文字の変数名)
If you call your variables a, b, c, then it will be impossible to search for
instances of them using a simple text editor. Further, nobody will be able
to guess what they are for. If anyone even hints at breaking the tradition
honoured since FORTRAN of using i, j, and k for indexing variables, namely
replacing them with ii, jj and kk, warn them about what the Spanish
Inquisition did to heretics.
もしあなたが変数の名前を a, b, c のようなものにしてしまったならテキストエデ
ィターを単純に使ってそれらの変数が使われている場所を検索することは不可能に
なるでしょう。さらに、それがどういった目的のためのものなのか推測できる人は
いなくなるでしょう。もし誰かがFORTRAN 以来の、添え字変数に i, j, k を使うと
いう tradition honoured を破る気配を見せるようななら、制御変数の名前を
ii, jj, kk のようなものに変更してしまって Spanish Inquisition did to heretics
を警告しましょう。
Creative Miss-spelling
If you must use descriptive variable and function names, misspell them. By misspelling
in some function and variable names, and spelling it correctly in others (such as
SetPintleOpening SetPintalClosing) we effectively negate the use of grep or IDE search
techniques. It works amazingly well. Add an international flavor by spelling tory or
tori in different theatres/theaters.
もし変数や関数の名前を説明的 (descriptive) なものにしなければならないのであれば
(たとえば SetPintleOpening とか SetPintalClosing のような) 間違ったスペルにしてし
まいましょう。一部の関数や変数でミススペルをし、そのほかのものは正しいスペリングを
することによって、わたしたちは grep や IDE による検索の技法を効果的に negate できます。
これは信じられないくらいうまくいきます。
spelling tory や tori in different theatres/theaters によって、国際色を追加しましょう。
Be Abstract (抽象的であれ)
In naming functions and variables, make heavy use of abstract words like it,
everything, data, handle, stuff, do, routine, perform and the digits e.g.
routineX48, PerformDataFunction, DoIt, HandleStuff and do_args_method.
関数や変数の命名においては、data, handle, stuff, do, routine, perform といった
抽象的な単語と数字を積極的に使います。つまり、routineX48、PerformDataFunction,
DoIt, HandleStuff and do_args_method という名前にするのです。
A.C.R.O.N.Y.M.S. (略語)
Use acronyms to keep the code terse. Real men never define acronyms; they understand
them genetically.
コードを terse にし続けるために略語を使います。本物の男たちは略語を定義しません。
彼らはそれらを genetically に理解するのです。
Thesaurus Surrogatisation シソーラス
To break the boredom, use a thesaurus to look up as much alternate vocabulary as
possible to refer to the same action, e.g. display, show, present. Vaguely hint
there is some subtle difference, where none exists. However, if there are two
similar functions that have a crucial difference, always use the same word in
describing both functions (e.g. print to mean "write to a file",
"put ink on paper" and "display on the screen"). Under no
circumstances, succumb to demands to write a glossary with the special purpose
project vocabulary unambiguously defined. Doing so would be an unprofessional breach
of the structured design principle of information hiding.
退屈さを打破するために、たとえば display, show, present のような、同じ動作を表すことが
できる語彙を可能な限り見つけられるようにシソーラスを使いましょう。同義語が存在していな
い場合でも、ほんのちょっとだけ違っているものがあるという漠然としたヒントになります。
ただし、決定的な違いを持つ二つの似通った関数があった場合には、両方の関数を表すのに常に
同じ単語を使うようにしましょう (e.g. print to mean "write to a file",
"put ink on paper" and "display on the screen").
どんな環境であっても、特別な目的をもったプロジェクトのためのあいまいなく定義されている
語彙を収めた用語集を書くという要求に succumb (降伏、屈服?) します。
Doing so would be an unprofessional breach of
the structured design principle of information hiding.
Use Plural Forms From Other Languages
A VMS script kept track of the "statii" returned from various "Vaxen".
Esperanto , Klingon and Hobbitese qualify as languages for these purposes. For
pseudo-Esperanto pluraloj, add oj. You will be doing your part toward world peace.
VMS スクリプトは様々な"Vaxen"から return された "statii"の記録を
とっています。エスペラント語、クリンゴン語、ホビット語 といったものはこの目的に適した
言語です。
For pseudo-Esperanto pluraloj, add oj.
You will be doing your part toward world peace.
CapiTaliSaTion
Randomly capitalize the first letter of a syllable in the middle of a word. For
example ComputeRasterHistoGram().
ComputeRasterHistoGram() のように、単語の途中にあるシラブルの最初の文字をランダム
に大文字にします。
Reuse Names (名前の再利用)
Wherever the rules of the language permit, give classes, constructors, methods,
member variables, parameters and local variables the same names. For extra points,
reuse local variable names inside {} blocks. The goal is to force the maintenance
programmer to carefully examine the scope of every instance. In particular, in Java,
make ordinary methods masquerade as constructors.
言語の規則が許可しているときはいつでも、クラス、コンストラクター、メソッド、メンバ
ー変数、パラメーター、ローカル変数に対して同じ名前をつけます。For extra points,
{] ブロックの中でローカル変数の名前を再利用します。その目的はメンテナンスプログラマ
ーがすべてのインスタンスごとにスコープを注意深く検査することを強制させるためです。
特にJavaにおいては普通のメソッドをコンストラクターに偽装します。
Accented Letters アクセントつき文字
Use accented characters on variable names. E.g.
typedef struct { int i; } int;
where the second int's i is actually i-acute. With only a simple text editor, it's
nearly impossible to distinguish the slant of the accent mark.
変数名にアクセントつきの文字を使いましょう。これはつまり
typedef struct { int i; } int;
というものがあったときに、二番目の int の i をアキュートつきのi(i-acute) にすると
いったものです。シンプルなテキストエディターだけを使っていてはアクセント記号の傾き
を区別することはほぼ不可能です。
Exploit Compiler Name Length Limits (名前の長さに関するコンパイラーの限界を超える)
If the compiler will only distinguish the first, say, 8 characters of names, then vary
the endings e.g. var_unit_update() in one case and var_unit_setup() in another. The
compiler will treat both as var_unit.
コンパイラーがもし名前の最初の、たとえば 8文字だけしか区別していないのであれば、その
後ろにいろいろつけてあるときには var_unit_update()を使い、別のときには var_unit_update()
を使うようにします。コンパイラーはこれら二つを同じ var_unit として扱うでしょう。
Underscore, a Friend Indeed
Use _ and __ as identifiers.
_ や __ を識別子として使いましょう。
Mix Languages 言語を混ぜる
Randomly intersperse two languages (human or computer). If your boss insists you
use his language, tell him you can organise your thoughts better in your own language,
or, if that does not work, allege linguistic discrimination and threaten to sue your
employers for a vast sum.
二つの言語 (人間の言葉とコンピューターの言語)をランダムに混ぜ合わせます。もしあなたの
上司が彼の言語を使うことを強制してきたら、自分の言語のほうが自分の考えを組み立てやすい
と主張しましょう。
or,
if that does not work,
allege linguistic discrimination
and threaten to sue your employers for a vast sum.
Extended ASCII 拡張ASCII
Extended ASCII characters are perfectly valid as variable names, including ß, Ð, and
ñ characters. They are almost impossible to type without copying/pasting in a simple
text editor.
ß, Ð, ñ を含む拡張 ASCII のキャラクターは変数名として完全に正当なものです。これら
のキャラクターは単純なテキストエディターではコピペすることなしに入力することはほぼ不
可能です。
Names From Other Languages 他の言語からの名前
Use foreign language dictionaries as a source for variable names. For example, use
the German punkt for point. Maintenance coders, without your firm grasp of German,
will enjoy the multicultural experience of deciphering the meaning.
変数名のソースとして外国語の辞書を使いましょう。たとえば point を表すのにドイツ語の
punkt を使います。without your firm grasp of German な Maintenance coderたちはその
意味を解読するために複数文化を楽しむことになるでしょう。
Names From Mathematics 数学分野からの名前
Choose variable names that masquerade as mathematical operators, e.g.:
数学の演算子であると偽るような変数名を選びます。
openParen = (slash + asterix) / equals;