ひしだまの変更履歴

ひしだまHPの更新履歴。
主にTRPGリプレイの元ネタ集、プログラミング技術メモと自作ソフト、好きなゲームや音楽です。

直訳コメントは不要だと思う

2015-07-01 23:31:57 | プログラミング

不定期に何度も盛り上がる話題、今日は直訳コメントの必要性について(笑)
→『コードを直訳したコメントは必要なのか?「要る派」と「要らない派のまとめ」【 #社畜ちゃん台詞メーカー 】』 

コメントをどこまで付ければいいかということについてはなかなか結論が出ないけれども、「コードを直訳したコメント」はさすがに要らないと思う。


例えばこんなコードは直訳コメントなので、要らないんじゃないか。

// iに1加算する
i += 1;

これが以下のようなコメントなら、直訳ではない。

// ループカウンターをインクリメント
i += 1;

とは言え、このコードの前後を見てiがループカウンターであることがすぐ分かるのであれば、こういったコメントは不要だろう。
しかし、以下のようなコメントだったら?

// 年齢を増やす
i += 1;

これならコメントに意味が出てくる気がする。
しかし この場合は、変数名を何とかしろよって話だよな^^;

// 年齢を増やす
age += 1;

こうなると直訳コメントなので、このコメントは要らない気がする。


もうちょっと別の例。

// 猫の身長に入力データを設定する
dog.setHeight(inputData);

変数名がcatだったら、直訳コメントになって、不要なコメントだった^^;
あるいは、こんなの。

// 猫の身長に入力データを設定する
cat.setWeight(inputData);

これらの場合、コメントが間違っているのか、コードが間違っているのか?

自分の場合、コーディングする前に、どんな処理を行うか考える一環としてコメントに書くことがある。
それに沿ってコーディングしていくので、いわばコメントが実装仕様になっているわけだ。
その観点で言えば、たぶんコードが間違っているんだけど、コードを変えてコメントを変え忘れることもあるわけで、一概には言えないorz
(コメントとコードをまるまるコピペして、片方だけ修正が漏れるってことがよくある…。特にコメントの修正を忘れても、コンパイルエラーにもテスト失敗にもならないから…)

こういうミスを発見するためには直訳コメントであっても意味があるのかもしれない?
あるいは余計なことを考える羽目になるので、やはり直訳コメントは無い方が良い?(コードがミスってれば、テストで発覚するし) 


以下のようなコードだったら、コメントにも意味があると思う。

// 値が一番小さいデータを取得(リストは値の昇順でソート済み)
Hoge min = list.get(0);

これが、「リストの先頭データを取得」なんていう直訳コメントだったら、意味がないと思うけどねw

結局、「事実(どういう処理か)」はコードを見れば分かるので、それを日本語化した直訳コメントは要らない。
どういう「意図」でそういうコードになっているのか、というのはコードを見ても分からないので、コメントが欲しい。

少なくとも、コメントを書くにも結構な労力がかかるので、コードの1行1行に対して必ずコメントを入れろ、なんてのは全くナンセンスだと思う。
(そういう要求をされると、直訳コメントが増える気がするし)



最新の画像もっと見る

3 コメント

コメント日が  古い順  |   新しい順
Obat Kanker Serviks (Obat Kanker Serviks)
2017-03-17 15:06:17
I'd been very encouraged to track down this website. I desired to thank you with this special study. I certainly savored every this and I've you bookmarked to look in fresh things you write. http://atlasathome.cern.ch/team_display.php?teamid=3010

The globe is altering fast. people are also being altered.day by day time we are becoming more dependant on degital system.you're making me think of this really.You have a fantastic way of sharing your thoughts. http://climateapps2.oerc.ox.ac.uk/cpdnboinc/team_display.php?teamid=10884

This can be really a brilliant post, many cheers for telling Great luck I found out about this certain blog site. http://www.primaboinca.com/team_display.php?teamid=6143

I really enjoy simply studying most of your weblogs. Simply wanted to inform you you have people like me who appreciate your work. Absolutely a fantastic publish. The information which you have provided is truly very useful. http://volunteer.cs.und.edu/csg/team_display.php?teamid=36851

Just keep writing what you are excited about, and the readers will come to your article. http://boinc.riojascience.com/team_display.php?teamid=111525
返信する
Obat Leukimia (Obat Leukimia)
2017-03-17 15:07:45
Your article had provided me with another point of view on this topic. I had no notion that things can work in this ways as well. Thank you for sharing your perspective. http://boinc.riojascience.com/team_display.php?teamid=111528

I want to thank you for your nicely written article it's useful and your writing trend helped me to the idea without any difficulty. Many thanks http://boinc.umiacs.umd.edu/team_display.php?teamid=4727

I found useful information on this topic as I will be taking care of a small business project. Thanks posting relative information and its now becoming easier to complete this project http://www.vdwnumbers.org/team_display.php?teamid=2469

We are actively implementing a great assignment and I have been exploring your website for a few hours. I'm appreciate your the idea proved valuable for me. http://atlasathome.cern.ch/team_display.php?teamid=3007

I probably appreciating each and every of it. is actually an incredible and decent impart. I need to much obliged. Great employment! You all an incredible page, and also have some extraordinary substance. http://volunteer.cs.und.edu/csg/team_display.php?teamid=36852
返信する
Obat Stroke (Obat Stroke)
2017-03-17 15:09:14
I had been very encouraged to track down this website. I desired to thanks with this special study. I certainly savored each and every it and I've you bookmarked to look in brand new things you publish. https://mindmodeling.org//team_display.php?teamid=3529

The globe will be changing fast. people are also having altered.day by day time we are becoming more dependant on degital system.you are making me think of this really.You have a pleasant technique of sharing your thoughts. http://volunteer.cs.und.edu/csg/team_display.php?teamid=36853

That is really a brilliant post, many thanks for telling Good luck I found out concerning this specific website. http://milkyway.cs.rpi.edu/milkyway/team_display.php?teamid=14972

I really enjoy simply looking at most of your weblogs. Simply wanted to inform you which you have people like me who appreciate your work. Undoubtedly a fantastic post. The information that you have provided is truly very worthwhile. http://milkyway.cs.rpi.edu/milkyway/team_display.php?teamid=7839

Just keep write what you are excited about, and the visitors will come to your article. http://zavmed.blogspot.co.id/2015/05/obat-tradisional-untuk-penyakit-stroke.html
返信する

コメントを投稿