- 最終更新
- 2026/10/21
3秒でわかる
ひとつの部品にはひとつの関心事だけを持たせる設計の考え方。変更したい理由ごとにコードを分けておくと、直す範囲が読まなくても分かるようになります。
30秒図解
もう少し詳しく
どういうものか
責務分離(関心の
判断の基準は
なぜ必要か
混ざったままだと、
具体例
取得と
// 分ける前 取得・集計・DOM 操作が 1 つの関数に同居している
async function showTotal(userId) {
const res = await fetch(`/api/orders?user=${userId}`);
const orders = await res.json();
let total = 0;
for (const o of orders) total += o.price * o.count;
document.getElementById("total").textContent = `${total} 円`;
}
// 分けたあと
const fetchOrders = (userId) =>
fetch(`/api/orders?user=${userId}`).then((r) => r.json());
const calcTotal = (orders) =>
orders.reduce((sum, o) => sum + o.price * o.count, 0);
function renderTotal(el, total) {
el.textContent = `${total} 円`;
}分けたあとの calcTotal は、
つまずきやすいところ
分ければ
もう
覚え方
「変更したい


