SlideShare a Scribd company logo
1 of 35
Download to read offline
コメントについて
本気で考えてみた
    やきに駆動 2.0
      2012/10/27
           @irof
とりあえず書き殴ってみた
コメント書いてます?




Question mark in Esbjerg by alexanderdrachmann
コメント書いてます?




                                 なんで?
Question mark in Esbjerg by alexanderdrachmann
コメントを書く動機


コードがわかり辛い
実装の仕方がわからない
なんとなく
とりあえず
コメントを書く目的



コードの理解を助けるため
コードに書けないことを伝えるため
コメントは未来のために
Rosetta Stone by Spixey
コメントに書く内容

ドキュメントコメント
 何が達成できるかを書く
 コードを読まずに済ませる
コメント
 意図や存在理由を書く
 コードのメンテナンスを助ける
いいコードには
多くのコメントがある
いいコードには
     多くのコメントがある
見苦しいコードには
多くのコメントが必要になる
いいコメント?




Question mark made of puzzle pieces by Horia Varlan
いいコメント?
               正しい
              明瞭である
             情報を追加する
              書かない




Question mark made of puzzle pieces by Horia Varlan
悪いコメント?




Question mark made of puzzle pieces by Horia Varlan
悪いコメント?
                                                       嘘・矛盾がある
                                                         大量にある
                                                       情報が増えない
                                                      悪いコードのゴマカシ




Question mark made of puzzle pieces by Horia Varlan
どうせ書くならいいものを
ウンコメントカタログ
履歴
日本語訳
コードより多い
嘘
設計書マッピング
https://gist.github.com/3292173
コメントで
      ダメなコードを
  取り繕うことはできない
悪いコードにコメント
をつけるな。
書き直せ。
コメントを削減する
素直に消す


履歴コメントとかは即座に消す。
全部バージョン管理ツールに任せて
しまいましょう。
……とは言え、残してるようなとこ
は無いですよね?ね?
まともな名前をつける


定数をマジックナンバーにしない
変数名に意味を込める
メソッド名で意図を表す
 説明メッセージを使用する
メソッド・クラスの抽出



クラスやメソッドの抽出を行う。
抽出されたものの責務が明確になる
ので、きっとコメントは要らなくなる
に違いない。
対称性の原則に従う



同じメソッド内に登場する構文とかは
同じレベルのものであること。
対称性を意識するとコードの可読性
はかなり上がる。はず。
テストで表現する

コメントは変化に対応しなくても即座
にフィードバックをくれるわけじゃな
い。
コメントに複雑な条件などを書くくら
いならテストで表現する方法もなく
はない。
テストが十分に読みやすければ。
読む人を意識する

「誰でも読めるように」コメントを
書いてるとキリがありません。
コメントはコードに書かれるので、
プログラミングとシステムの概要は
理解していると考えていいはず。
コンテキストを意識することで、不
要なコメントは書かない。
まとめ
コメントは消臭剤
そもそも臭くないところには
     置かない
無臭じゃないので過剰に置く
    と逆に臭い
コメントは装飾
                                  悪いコードを覆い隠すことは
                                      できない




Cakes later on... / Debs (ò‿ó)♪
コメントは純粋に良いものではあり
ません。
ボブおじさん曰く「必要悪」
コメントを書くのは、プログラミン
グ言語での自身の表現力が及んでい
ないことの現れです。
コメント書いたら負け。でも。
書くべき時は書くべし
おしまい。
 肉が食べたいです
<参考文献>
* CleanCode
* CodeCraft
* 実装パターン
* 達人プログラマー
* プログラミング作法
* プログラマが知るべき
97のこと
* リファクタリング

More Related Content

Featured

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 

コメントについて考えてみた