R.Tanaka.Ichiro's Blog

主にC# な話題です

目次

Blog 利用状況

ニュース

コーディングスタイル

読みやすいコード

とは、多くのプログラマの最大公約数のようなものだろうか?

例えば、先日聞いた話ではコーディング規約に三項演算子すら禁止している会社があるらしい。
理由は、多くの社員が、そんなものを使わないから。

一般的に使われない書き方を誰かが使うと、他の多くの人がコードを読む時に難解になるという考え方は正しいが、これは単なる慣れの問題である場合も少なくない。

どこまで慣れるべきなのかについては、また別の話題になる。

最終的に、慣れるまでにかかるコストと、慣れた後に回収できるコストとのバランスによって判断するならば、スタイル一つ一つのパターンが、どれだけ生産性が高くバグを含みにくいのかを検証する必要があるだろう。

この検証は、組織単位で行わずとも誰かが検証した結果を全組織で流用できる。

なんて思ったので、誰かそういうのやりませんか?

投稿日時 : 2008年6月25日 14:16

Feedback

# re: コーディングスタイル 2008/06/25 14:24 らふぁ

http://www.objectclub.jp/community/codingstandard/
おいらはここを参考にしてます。
ここ数年メンテされていないのがアレですが…

ただ、人の入れ替わりが頻繁な環境だと、慣れるまでの時間を
無駄な時間扱いされてしまうのが困りものですよねぇ。

# re: コーディングスタイル 2008/06/25 14:27 じゃんぬねっと

会社単位で決めてくれればおk。
でも根拠のないものには確かに反発したくはなる。

# re: コーディングスタイル 2008/06/25 14:34 アキラ

ぼくのコーディングスタイルはこんな感じです
http://d.hatena.ne.jp/faith_and_brave/20071226/1198659828

禁止事項とか、名前付けのルールは、"納得できる"根拠があればいいです。

# re: コーディングスタイル 2008/06/25 14:35 シャノン

> 誰かそういうのやりませんか?

ザ・言いだしっぺの法則w

# re: コーディングスタイル 2008/06/25 14:38 シャノン

「コーディング規約」「書き方」「スタイル」等の言葉を「設計」や「デザインパターン」に置き換えても、同じことが言えますね。

# re: コーディングスタイル 2008/06/25 16:50 凪瀬

勉強したくないので新しいパラダイムを使わないでください!っていう主張との境目が難しく。
社内全体に新しいものを取り入れない空気が蔓延することの方がよほどリスクがでかいと思うけどな。

# re: コーディングスタイル 2008/06/25 17:03 melt

自分は三項演算子やらのちょっとしたスタイルの問題がコードを読みづらくするとは全然思わないですね。

そんなのより、無意味な変数、無駄なコメント、冗長な処理、意図が不明瞭な関数、境界の曖昧なクラス、そういうものの方がよっぽど読みにくいと思います。
スタイルを修正させるぐらいならそっちを修正させる方がよっぽど効果が高いと思うのですが...。

# re: コーディングスタイル 2008/06/25 17:22 NAL-6295

私のコーディングスタイルはこんな感じですかね。
http://d.hatena.ne.jp/NAL-6295/20080409

でも、一言にすると「スコープを最小に。」です。

三項演算子とか、言語仕様読めばわかるようなレベルは
「じゃあ、読んでください。」
ですね。
匿名メソッドなんかも禁止の対象になりそうな現場が多そうで戦々恐々ですね。(匿名という名前から誤解するパターン)

# re: コーディングスタイル 2008/06/25 18:50 ネタ好き未記入

糞会社となると社員がオブジェクト指向を覚えるのが嫌なので、オブジェクト指向禁止とかを体験したことがあります。
正常なコーディングスタイルと怠惰の区別をつけるルールとかあると面白いと思います。だれかこのネタでスピーチして欲しいです。

# re: コーディングスタイル 2008/06/25 19:46 Hirotow

私は基本的にはIDEのコードフォーマットに従い、
変数名は、
static field:s_valName形式
instance field:m_valName形式
control field:ctlVerbNoun形式
parameter:title str numなど極力3文字にする
loop counter:ijkと順番に使用
local variable:s(tream)r(eader)やc(urrent)n(ode)など全て小文字で略すかstrPathのようなハンガリアン記法。リストは原則複数形。

# re: コーディングスタイル 2008/06/25 22:37 ネタ好き未記入

R・田中さんの言うとおりで、いくらコーディングスタイルが優れていてもコストが高すぎたら問題です。
かといって、コーディングスタイルによって、新人の指導という側面や、その会社のノウハウの蓄積という面もあると思います。
ですから、コーディングスタイルに【レベル】という概念を導入するべきだと思います。そうすれば、コストを事前に見積もったり、新人をソルジャーへと徐々に鍛え上げられると思います。

# [C] 3項演算子が駄目なら他の方法を考える 2008/06/25 23:29 The beast of a halfpace

[C] 3項演算子が駄目なら他の方法を考える

# 三項演算子 2008/06/26 0:12 ma2のblog - わんくま版(仮)

三項演算子

# re: コーディングスタイル 2008/06/26 9:38 NAL-6295

追記:
とはいえ、三項演算子は、とんと使わないですね。
if文でいいや。って。

# re: コーディングスタイル 2008/06/26 13:46 R・田中一郎

らふぁ さん

>ただ、人の入れ替わりが頻繁な環境だと、慣れるまでの時間を
>無駄な時間扱いされてしまうのが困りものですよねぇ。

そうですね。
慣れないと読めない場合もありますし、難しい問題です。

---------------------------------------------
じゃんぬねっと さん

>でも根拠のないものには確かに反発したくはなる。

コーディング規約に限った話ではなく、根拠のないものって多いですよね。

---------------------------------------------
アキラ さん

>禁止事項とか、名前付けのルールは、"納得できる"根拠があればいいです。

やはり「根拠」ですね。わかります。

---------------------------------------------
シャノン さん

>ザ・言いだしっぺの法則w

やめれw

>「コーディング規約」「書き方」「スタイル」等の言葉を「設計」や「デザインパターン」に置き換えても、同じことが言えますね。

慣れによる安心感というものがあるのでしょうかね。
間違った「慣れ」は、早々に修正すべきですが、それ以前に、何が間違っているのかを定義する必要があるんですよね。

---------------------------------------------
凪瀬 さん

>勉強したくないので新しいパラダイムを使わないでください!っていう主張との境目が難しく。

良く聞きますね。
それと、このやり方でずっとやってきていて問題がないから変えたくないという間違った「慣れ」です。

>社内全体に新しいものを取り入れない空気が蔓延することの方がよほどリスクがでかいと思うけどな。

全くその通りだと思います。
正しい方法を続けることは間違っていないけれど、正しい方法は新しいパラダイムの出現によって変化しますからね。
柔軟に対応しないと競争力がなくなるリスクを伴います。

---------------------------------------------
melt さん

>そんなのより、無意味な変数、無駄なコメント、冗長な処理、意図が不明瞭な関数、境界の曖昧なクラス、そういうものの方がよっぽど読みにくいと思います。

そうですね。
これがきちんとできないと、スパゲティコードができあがりますからね。

---------------------------------------------
NAL-6295 さん

>三項演算子とか、言語仕様読めばわかるようなレベルは
>「じゃあ、読んでください。」
>ですね。

本来、一瞬で理解できますからね。

>匿名メソッドなんかも禁止の対象になりそうな現場が多そうで戦々恐々ですね。(匿名という名前から誤解するパターン)

それは、ちょっと辛いかも・・・

---------------------------------------------
ネタ好き未記入 さん

>正常なコーディングスタイルと怠惰の区別をつけるルールとかあると面白いと思います。だれかこのネタでスピーチして欲しいです。

世の中には、言いだしっぺの法則というものがあるらしいです。

---------------------------------------------
Hirotow さん

>私は基本的にはIDEのコードフォーマットに従い、

このように、一貫した規則で書くのは基本なのですが、これすらできない人もいますよね。

# re: コーディングスタイル 2008/06/26 14:00 ぷさいくろう

変数のスクープを最小にするといえば
メソッドで定義すればいいstatic変数を
クラスのメンバ変数でするのをよく見かける。

# re: コーディングスタイル 2008/06/26 16:55 R・田中一郎

それはもう基礎知識のレベルですねw

# re: コーディングスタイル 2008/06/26 16:56 NAL-6295

>変数のスクープを最小にするといえば
>メソッドで定義すればいいstatic変数を
>クラスのメンバ変数でするのをよく見かける。

私も良く見かけます。
後は、ルールに背いても頑なに変数をメソッドの先頭で宣言するスタイルを維持する人も。

# re: コーディングスタイル 2008/06/27 10:58 よねけん

#みなさんのコメントが一段落したところで、
#ただのちゃちゃですw

> とは、多くのプログラマの最大公約数のようなものだろうか?

12と8の最大公約数は4です。
このようにどのプログラマにとっても
十分な力を発揮できなくする足かせが
「コーディングスタイル」ということでOKですか(汗

最大公約数の代わりにどんな言葉がよいだろう?
と考えてみたらありきたりな「平均」しか思い浮かばない罠。

# re: コーディングスタイル 2008/06/27 13:24 R・田中一郎

NAL-6295 さん

>後は、ルールに背いても頑なに変数をメソッドの先頭で宣言するスタイルを維持する人も。

うーん・・・
皆さん、大変なんですね。

-------------------------------------------
よねけん さん

>12と8の最大公約数は4です。

きた・・・w
誰か絶対言うと思ったw

# I am genuinely delighted to glance at this web site posts which includes lots of useful facts, thanks for providing these kinds of information. 2021/08/30 3:33 I am genuinely delighted to glance at this web sit

I am genuinely delighted to glance at this web site posts which includes lots of
useful facts, thanks for providing these kinds of
information.

# I am genuinely delighted to glance at this web site posts which includes lots of useful facts, thanks for providing these kinds of information. 2021/08/30 3:34 I am genuinely delighted to glance at this web sit

I am genuinely delighted to glance at this web site posts which includes lots of
useful facts, thanks for providing these kinds of
information.

# I am genuinely delighted to glance at this web site posts which includes lots of useful facts, thanks for providing these kinds of information. 2021/08/30 3:35 I am genuinely delighted to glance at this web sit

I am genuinely delighted to glance at this web site posts which includes lots of
useful facts, thanks for providing these kinds of
information.

# I am genuinely delighted to glance at this web site posts which includes lots of useful facts, thanks for providing these kinds of information. 2021/08/30 3:36 I am genuinely delighted to glance at this web sit

I am genuinely delighted to glance at this web site posts which includes lots of
useful facts, thanks for providing these kinds of
information.

# Remarkable! Its truly awesome post, I have got much clear idea on the topic of from this paragraph. 2021/09/03 2:33 Remarkable! Its truly awesome post, I have got muc

Remarkable! Its truly awesome post, I have got much clear idea on the topic of from
this paragraph.

# Remarkable! Its truly awesome post, I have got much clear idea on the topic of from this paragraph. 2021/09/03 2:34 Remarkable! Its truly awesome post, I have got muc

Remarkable! Its truly awesome post, I have got much clear idea on the topic of from
this paragraph.

# Remarkable! Its truly awesome post, I have got much clear idea on the topic of from this paragraph. 2021/09/03 2:35 Remarkable! Its truly awesome post, I have got muc

Remarkable! Its truly awesome post, I have got much clear idea on the topic of from
this paragraph.

# Remarkable! Its truly awesome post, I have got much clear idea on the topic of from this paragraph. 2021/09/03 2:36 Remarkable! Its truly awesome post, I have got muc

Remarkable! Its truly awesome post, I have got much clear idea on the topic of from
this paragraph.

# Hi there! I simply want to offer you a big thumbs up for the great info you have got right here on this post. I'll be returning to your website for more soon. 2021/09/04 16:31 Hi there! I simply want to offer you a big thumbs

Hi there! I simply want to offer you a big thumbs up for the great info
you have got right here on this post. I'll be returning to your
website for more soon.

# Hi there! I simply want to offer you a big thumbs up for the great info you have got right here on this post. I'll be returning to your website for more soon. 2021/09/04 16:32 Hi there! I simply want to offer you a big thumbs

Hi there! I simply want to offer you a big thumbs up for the great info
you have got right here on this post. I'll be returning to your
website for more soon.

# Hi there! I simply want to offer you a big thumbs up for the great info you have got right here on this post. I'll be returning to your website for more soon. 2021/09/04 16:33 Hi there! I simply want to offer you a big thumbs

Hi there! I simply want to offer you a big thumbs up for the great info
you have got right here on this post. I'll be returning to your
website for more soon.

# Its like you read my thoughts! You appear to grasp so much approximately this, like you wrote the guide in it or something. I think that you just can do with some p.c. to drive the message house a little bit, but instead of that, this is magnificent blo 2021/12/06 13:00 Its like you read my thoughts! You appear to grasp

Its like you read my thoughts! You appear to grasp so
much approximately this, like you wrote the guide
in it or something. I think that you just can do with some p.c.
to drive the message house a little bit, but instead of that, this is magnificent blog.
An excellent read. I'll definitely be back.

# Its like you read my thoughts! You appear to grasp so much approximately this, like you wrote the guide in it or something. I think that you just can do with some p.c. to drive the message house a little bit, but instead of that, this is magnificent blo 2021/12/06 13:01 Its like you read my thoughts! You appear to grasp

Its like you read my thoughts! You appear to grasp so
much approximately this, like you wrote the guide
in it or something. I think that you just can do with some p.c.
to drive the message house a little bit, but instead of that, this is magnificent blog.
An excellent read. I'll definitely be back.

# Its like you read my thoughts! You appear to grasp so much approximately this, like you wrote the guide in it or something. I think that you just can do with some p.c. to drive the message house a little bit, but instead of that, this is magnificent blo 2021/12/06 13:01 Its like you read my thoughts! You appear to grasp

Its like you read my thoughts! You appear to grasp so
much approximately this, like you wrote the guide
in it or something. I think that you just can do with some p.c.
to drive the message house a little bit, but instead of that, this is magnificent blog.
An excellent read. I'll definitely be back.

# Its like you read my thoughts! You appear to grasp so much approximately this, like you wrote the guide in it or something. I think that you just can do with some p.c. to drive the message house a little bit, but instead of that, this is magnificent blo 2021/12/06 13:02 Its like you read my thoughts! You appear to grasp

Its like you read my thoughts! You appear to grasp so
much approximately this, like you wrote the guide
in it or something. I think that you just can do with some p.c.
to drive the message house a little bit, but instead of that, this is magnificent blog.
An excellent read. I'll definitely be back.

タイトル
名前
Url
コメント