SlideShare a Scribd company logo
1 of 34
Download to read offline
より良いコードを書くための

2019.6.28 koji.kobayashi
名 前 付 け

目次
1. なぜ名前が重要なのか
2. 悪い名前の問題点
3. 良い名前とは
4. 適切な名前とは
5. 正しい名前の書き方
6. 便利なツール
7. まとめ
問題
Quiz
Quiz
ソフトウェアシステム(Eclipse)内における
「名前(識別子)」は、ソースコードのうち
約何%を占めている?
① 30%
② 50%
③ 70%
正解は… ③
https://www.cqse.eu/publications/2005-concise-and-consistent-naming.pdf
There are only two hard things in Computer Science:
cache invalidation and naming things.
-- Phil Karlton
「コンピュータサイエンスで難しいことは2つしかない。
 キャッシュの無効化と名前付けである」
 
 by 元Netscape開発者 Phil Karlton
目次
1. なぜ名前が重要なのか
2. 悪い名前の問題点
3. 良い名前とは
4. 適切な名前とは
5. 正しい名前の書き方
6. 便利なツール
7. まとめ
なぜ名前が重要なのか
プログラマーの仕事と「名前」は切っても切れない関係
● クラス、メソッドなど日々何かしらの「名前付け」を行なっている
● 他人が書いたコードの「名前」を読んでいる
※OSSでは、良い機能だが良い名前が思いつかないという理由で、その機能を追加し
ないということもある
名前はプログラムにおいて重要な事柄
良いコードを書くため
(理解しやすい、変更に強い、読みやすい)
目次
1. なぜ名前が重要なのか
2. 悪い名前の問題点
3. 良い名前とは
4. 適切な名前とは
5. 正しい名前の書き方
6. 便利なツール
7. まとめ
悪い名前の問題点
● 理解が難しい
○ 直感に反する名前、誤った名前は読むときに何を指しているのか、何が起きるのかが分かりにくい
○ 結局中身を見る手間が生じる
○ 時間が経てば忘れるのでまた読み直す
● 勘違いが起こる
○ 名前から推測して使ったメソッドが思わぬ副作用があった場合にバグになる
● 変更が難しい
○ 誤った名前の機能が至る所に使われていた場合、修正が困難(テーブル名とか特に)
○ すでにその名前が使われている場合、別の名前を考えなければならない
● 読みにくい
○ 命名規則がバラバラだったり、スペルミスがあるとスムーズに読めず、脳内の処理に余計なコスト
がかかる
目次
1. なぜ名前が重要なのか
2. 悪い名前の問題点
3. 良い名前とは
4. 適切な名前とは
5. 正しい名前の書き方
6. 便利なツール
7. まとめ
良い名前とは
命名において最も重要な考慮すべき点は、
変数が表現するものを正確に完全に言い表してい
ること
適切な名前を選び
正しく書かれた
良い名前とは…
もの
目次
1. なぜ名前が重要なのか
2. 悪い名前の問題点
3. 良い名前とは
4. 適切な名前とは
5. 正しい名前の書き方
6. 便利なツール
7. まとめ
適切な名前とは
名前と挙動が一致しているもの
= 名前から想像される意味とシステム上の挙動が一致していること
名前:addTodo
todoList
addTodo(todo) {
Todo.add(todo);
}
class Todo {
private _todoList = [];
// 略
public add(todo) {
this._todoList.push(todo);
}
}
=
名前から想像される意味 システム上の挙動
todo
名前と挙動が一致しないパターン
1. 名前の意味と挙動がずれている
2. 名前の意味が狭すぎる
3. 名前の意味が広すぎる
適切な名前とは
名前と挙動が一致しないパターン
1. 名前の意味と挙動がずれている
名前:addTodo
適切な名前とは
名前から想像される意味 システム上の挙動
Todoを追加
Todo.remove(todo);
≠
名前と挙動が一致しないパターン
2. 名前の意味が狭すぎる
名前:addTodo
適切な名前とは
名前から想像される意味 システム上の挙動
Todoを追加
Todo.add(todo);
Todo.shuffle();
<
名前から想像できない処理がある
名前と挙動が一致しないパターン
3. 名前の意味が広すぎる
名前:getPage
適切な名前とは
名前から想像される意味 システム上の挙動
getPage(url) {
response = request(‘GET’, url);
return response.body;
};
>
挙動はインターネットからとる
Database
page page page
internet file system
適切な名前とは
もうひとつ大事なこと…
極力「シンプル」な名前を付ける
次のうち、命名としてどちらがより良い名前?
① 完全で長い名前 (refreshContentsAndPurgePremiumUsersCache)
② 不完全で簡潔な名前 (refreshContents)
正解は… ①
https://signalvnoise.com/posts/3250-clarity-over-brevity-in-variable-and-method-names
適切な名前とは
挙動との一致が不完全で簡潔な名前
挙動との一致が不完全で長い名前
挙動との一致が完全で長い名前
挙動との一致が完全で簡潔な名前
×
×
適切な名前とは
従業員
personWhoIsHiredByCompany
employee
解錠
openlock
unlock
×
×
目次
1. なぜ名前が重要なのか
2. 悪い名前の問題点
3. 良い名前とは
4. 適切な名前とは
5. 正しい名前の書き方
6. 便利なツール
7. まとめ
正しい名前の書き方
1. 文法
2. 語彙(ボキャブラリー)
3. スペリング
4. その他(より読みやすくする工夫)
正しい名前の書き方
動詞(何かをする)
1. 文法
● ルーチン(関数、メソッドなど「一連の処理をまとめたコードのかたまり」)
● 動詞と目的語の順番に気をつける
○ ❌ emailSend()、formSubmit()
● 引数との関係性をはっきりとさせる
○ send(box) : boxを送る
○ sendTo(box) : boxに送る
正しい名前の書き方
名詞(何であるか)
1. 文法
● 説明を加える場合: (形容詞 | 過去分詞 +)名詞(+ 前置詞 + 名詞)の順番
(形容詞 | 過去分詞)+ 名詞 名詞 + 前置詞 + 名詞(動名詞、代名詞など)
deletedFile(削除されたファイル) messageForCustomers(顧客用メッセージ)
nestedList(ネストになったリスト) distanceFromPageTop(ページ上部からの距離)
※過去分詞とは「~された(状態の)」を表す
紛らわしい言葉に注意! 例えば clean は形容詞でもあり動詞でもある。紛らわしい場合は文脈にあった別名を考える。
cleanFile → newFile, cleanedFile, blankFile, emptyFile …!
正しい名前の書き方
2. 語彙(ボキャブラリー)
自分の知っているだけの単語でもそれなりに通じるが、
より自然で簡潔で間違いにくいものに改良するために必要なのが語彙力。
stop, quit, exit, abort, cancel, suspend, shutDown,
kill, break …
「止める」
日本語を介さずにイメージで捉える
Google画像検索が意外に使える
get と fetch の違い
獲得する、もらう、受け取る、手に入れる
という意味
get fetch
fetch は go and get / bring「行って取ってくる」意
味。目的のものがある場所に行ってそれを持って来
るイメージで、get や bring に「行って」のニュアンス
が加わっているようなもの
http://park1.wakwak.com/~english/illust/w_fetch.html
正しい名前の書き方
3. スペリング
● スペルミスは些細なようで一番やってはいけないミス
○ コード自体の信用を失う
○ 英語圏では履歴書や課題にスペルミスがあった時点で不合格になることもあるらしい
● 頻出単語ほど間違いやすい
○ calendar, forward, receive, tomorrow, recommend, address …
● 単数形と複数形、現在形と過去形の違いなどにも気をつける
○ ❌ childs ⭕ children
○ ❌ sended ⭕ sent
● 怪しいと感じたら面倒くさがらずに調べる癖をつけよう
正しい名前の書き方
その他(より読みやすくする工夫)
ブール値
● とにかく大事なのは「確実にブール値だとわかる」名前であること
「saved」
「saveされたかどうか」「セーブされた何か」どちらなのかわからない。
isSaved とするだけで確実にわかる。
● なんでも is~ をつければいいというものではない
~が存在するか ~が含まれているか ~すべきか ~する必要があるか
exists~ contains~ should~ need~
https://qiita.com/yskszk/items/5a7f99c974773f03a82a
正しい名前の書き方
● 単数と複数を分けるときに「s」を付けただけだと分かりにくい場合がある
○ 単数 todoItem
複数 todoItems
○ 同一プロジェクトに混在していると混乱が起きる場合がある
○ todoItems → todoList, todoItemList, todoItemGroup など、
厳密には必要ない言葉をあえて付け加えることで、 差が大きくなり違いが分かりやすくなる こともあ
る
その他(より読みやすくする工夫)
名前に差をつける
目次
1. なぜ名前が重要なのか
2. 悪い名前の問題点
3. 良い名前とは
4. 適切な名前とは
5. 正しい名前の書き方
6. 便利なツール
7. まとめ
便利なツール
● 辞書アプリ(Mac純正アプリ)
● 英和、和英、英英など他にもたくさんの種類の辞書を使える
● とにかくサクッと簡単に使える
● Mac 利用者は常に立ち上げておいた方がいい!?
● schema.org (https://schema.org/)
○ schema.orgとは、Google、Microsoft、Yahoo! の検索エンジン三社が仕様策定に取り組む組織。
schema.orgの仕様通りにHTMLにマークアップする事で、 Google,Yahoo!,Microsoftなどの検索エ
ンジンに正確な情報を収集してもらい、ユーザーに最適な検索結果を提供できる
○ 様々な用語(ボキャブラリー)が定義されている
○ 特にデータベースのテーブル名の命名に役に立つ
目次
1. なぜ名前が重要なのか
2. 悪い名前の問題点
3. 良い名前とは
4. 適切な名前とは
5. 正しい名前の書き方
6. 便利なツール
7. まとめ
まとめ
● 良い名前は、理解しやすく、変更に強く、読みやすい
○ 理想は名前だけを追ってシステムを理解できること
(細かい処理内容まで追わなくて理解できること)
● 名前付けの原則は
「名前から想像される意味」と「システム上の挙動」を一致させること
● スペルミスは一番やってはいけないミス
● 語彙力をつけるとよりスマートに、分かりやすい名前をつけられる
○ かといって高度すぎたり難しすぎる言葉は逆に分かりにくくなるのでバランスが大事
● 常に読み手を意識して、誤解を招くような名前を避ける
おわり
ありがとうございました
参考資料
● WEB+DB PRESS Vol.110 名前付け大全
● 真偽値を返す関数のネーミング- Qiita
● 英語の単数形・複数形について

More Related Content

Recently uploaded

Windowsアップデート後の黒い画面を修正する方法|データ復元|ブラックスクリーン
Windowsアップデート後の黒い画面を修正する方法|データ復元|ブラックスクリーンWindowsアップデート後の黒い画面を修正する方法|データ復元|ブラックスクリーン
Windowsアップデート後の黒い画面を修正する方法|データ復元|ブラックスクリーンivanwang53
 
あらゆる通信環境で切れない「ネットモーション」のモバイルアクセス [NetMotion]
あらゆる通信環境で切れない「ネットモーション」のモバイルアクセス [NetMotion]あらゆる通信環境で切れない「ネットモーション」のモバイルアクセス [NetMotion]
あらゆる通信環境で切れない「ネットモーション」のモバイルアクセス [NetMotion]Taka Narita
 
Windows Defenderのフル・クイック・カスタム・オフラインスキャンを実行する方法.docx
Windows Defenderのフル・クイック・カスタム・オフラインスキャンを実行する方法.docxWindows Defenderのフル・クイック・カスタム・オフラインスキャンを実行する方法.docx
Windows Defenderのフル・クイック・カスタム・オフラインスキャンを実行する方法.docxivanwang53
 
Windows 10、Windows 11の付箋を簡単に復元する6つの方法|データ復元
Windows 10、Windows 11の付箋を簡単に復元する6つの方法|データ復元Windows 10、Windows 11の付箋を簡単に復元する6つの方法|データ復元
Windows 10、Windows 11の付箋を簡単に復元する6つの方法|データ復元ivanwang53
 
動的 & 非同期コンポーネント / Dynamic & Async Components
動的 & 非同期コンポーネント / Dynamic & Async Components動的 & 非同期コンポーネント / Dynamic & Async Components
動的 & 非同期コンポーネント / Dynamic & Async Componentsokitamasashi
 
ダウンロードがダウンロード(Downloads)フォルダに表示されない」問題の対処法
ダウンロードがダウンロード(Downloads)フォルダに表示されない」問題の対処法ダウンロードがダウンロード(Downloads)フォルダに表示されない」問題の対処法
ダウンロードがダウンロード(Downloads)フォルダに表示されない」問題の対処法ivanwang53
 

Recently uploaded (6)

Windowsアップデート後の黒い画面を修正する方法|データ復元|ブラックスクリーン
Windowsアップデート後の黒い画面を修正する方法|データ復元|ブラックスクリーンWindowsアップデート後の黒い画面を修正する方法|データ復元|ブラックスクリーン
Windowsアップデート後の黒い画面を修正する方法|データ復元|ブラックスクリーン
 
あらゆる通信環境で切れない「ネットモーション」のモバイルアクセス [NetMotion]
あらゆる通信環境で切れない「ネットモーション」のモバイルアクセス [NetMotion]あらゆる通信環境で切れない「ネットモーション」のモバイルアクセス [NetMotion]
あらゆる通信環境で切れない「ネットモーション」のモバイルアクセス [NetMotion]
 
Windows Defenderのフル・クイック・カスタム・オフラインスキャンを実行する方法.docx
Windows Defenderのフル・クイック・カスタム・オフラインスキャンを実行する方法.docxWindows Defenderのフル・クイック・カスタム・オフラインスキャンを実行する方法.docx
Windows Defenderのフル・クイック・カスタム・オフラインスキャンを実行する方法.docx
 
Windows 10、Windows 11の付箋を簡単に復元する6つの方法|データ復元
Windows 10、Windows 11の付箋を簡単に復元する6つの方法|データ復元Windows 10、Windows 11の付箋を簡単に復元する6つの方法|データ復元
Windows 10、Windows 11の付箋を簡単に復元する6つの方法|データ復元
 
動的 & 非同期コンポーネント / Dynamic & Async Components
動的 & 非同期コンポーネント / Dynamic & Async Components動的 & 非同期コンポーネント / Dynamic & Async Components
動的 & 非同期コンポーネント / Dynamic & Async Components
 
ダウンロードがダウンロード(Downloads)フォルダに表示されない」問題の対処法
ダウンロードがダウンロード(Downloads)フォルダに表示されない」問題の対処法ダウンロードがダウンロード(Downloads)フォルダに表示されない」問題の対処法
ダウンロードがダウンロード(Downloads)フォルダに表示されない」問題の対処法
 

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
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 2024Neil Kimberley
 
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)contently
 
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 2024Albert Qian
 
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 InsightsKurio // The Social Media Age(ncy)
 
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 2024Search Engine Journal
 
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 summarySpeakerHub
 
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 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 Tessa Mero
 
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 IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
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 managementMindGenius
 
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...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
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...
 

より良いコードを書くための名前付け