プログラムの差分を確かめる
- 貼る
- 見る
プログラムを修正したあと、どこを変えたのか確認したい。設定ファイルが、前のものと何が違うのか知りたい。2つの文章の違いを調べる なら、コードも比べられます。
手順
- 「文章A(前)」に、変更前のコードを貼り付けます。
- 「文章B(後)」に、変更後のコードを貼り付けます。
- 「比べる」を押します。
- 「違いを見る」で、追加された行と削除された行を確認します。
向いている場面
- 設定ファイル(JSON、YAML、INIなど)の変更確認
- 小さなスクリプトの修正の確認
- コピーしたコードの、元との違いの確認
- コードレビュー前の、自分の変更の確認
使い方のコツ
- 比べる範囲を、関数やファイル単位に絞る
- 空白だけの変更を無視したいときは、「行の前後の空白を無視する」を使う
- JSONを比べるときは、先に整形して、キーの順番をそろえる
本格的な開発では
チームで開発するなら、Gitなどのバージョン管理が適しています。このツールは、手元で、ちょっと2つのコードを比べたいときに便利です。
注意すること
- 行単位の比較です。1行の中の、どこが変わったかは、前後の行から判断する。
- 3000行までです。大きなファイルは、分けて比べる。
- 機密のコードも、外に送られないので、貼り付けられる。ただし、共有範囲には注意する。
設定ファイルの比較のコツ
- 環境ごとの設定(本番と開発)を比べる
- 変更した設定だけを、確認する
- パスワードやキーは、伏せてから共有する
例
本番と開発の設定ファイルが、なぜか動作が違った。2つを貼り付けて比べたところ、接続先の1行だけが違っていた。
つまずきやすい点
- 改行コードが違う:WindowsとMacなどの、改行コードの違いが、差分に出る場合がある。
- タブとスペースの違い:見た目が同じでも、差分になる。
よくある質問
大きなプロジェクト全体を比べられますか?
いいえ。ファイル単位で、貼り付けて比べます。
まとめ
2つのコードや設定を貼り付けて比べる。手元の簡単な確認に使い、本格的にはバージョン管理を使いましょう。
2つの文章の違いを調べるを使う(無料)あわせて使えるツール
- 2つのPDFの違いを比べる(差分チェック):2つのPDFの文字の違いを、色分けして表示します。
- 重複した行を消す・並べ替える:重複した行を消して、並べ替えます。
- 文字数カウント(エントリーシート・レポート用):文章の文字数・行数・原稿用紙の枚数などを数えます。
同じツールの、ほかの解説
Checking Differences in Code
- Paste
- Look
After modifying a program, you want to check what you changed. You want to know how a configuration file differs from the previous one. Compare Two Texts (Diff) can compare code too.
Steps
- Paste the code from before the change into "Text A (before)".
- Paste the code after the change into "Text B (after)".
- Press "Compare".
- Check the added and removed lines under "See the differences".
Good for
- Checking changes to configuration files (JSON, YAML, INI and so on)
- Checking fixes to small scripts
- Checking how copied code differs from the original
- Checking your own changes before code review
Tips for use
- Narrow the range to compare to a function or file
- To ignore whitespace-only changes, use "Ignore spaces at the start and end of lines"
- When comparing JSON, format first and align the key order
For serious development
For team development, version control such as Git is suitable. This tool is handy when you simply want to compare two pieces of code at hand.
Cautions
- It's a line-by-line comparison. Judge which part of a line changed from the lines around it.
- Up to 3000 lines. Compare large files in parts.
- Confidential code isn't sent outside, so it can be pasted. But mind how widely you share.
Tips for comparing configuration files
- Compare settings per environment (production versus development)
- Check only the settings you changed
- Mask passwords and keys before sharing
Example
Production and development configuration files somehow behaved differently. Pasting both and comparing showed just one line, the connection target, differed.
Common stumbling blocks
- Different line-break codes: differences between Windows and Mac line-break codes may show as differences.
- Tabs versus spaces: show as a difference even if they look the same.
Frequently asked questions
Can a whole large project be compared?
No. Paste and compare file by file.
Summary
Paste two pieces of code or settings and compare. Use it for quick checks at hand, and use version control for serious work.
Use "Compare Two Texts (Diff)" (free)Tools that go well with this
- Compare Two PDFs (Find Differences): Show the text differences between two PDFs, color-coded.
- Remove Duplicate Lines and Sort a List: Remove duplicate lines and sort them.
- Character Counter (for Applications and Reports): Count characters, lines and manuscript sheets of your text.