第二家店开了。你想把第一家店的应用原样拿来用。
到这里通常会变成这样。
if (shopName == 'Riverside') { ... } else { ... }
然后第三家再来一次,第四家再来一次。两年后,文件里还留着一家早就关掉的店的分支,而没人敢删。
应用不该知道店
这个样例只有一条规则。
/// The tempting shape is a `shopName` switch somewhere. Then the second shop
/// is a code change, the third shop is a code change, and eventually the file
/// has a branch for a shop that closed two years ago.
///
/// So nothing here knows any shop's name. The config file is an argument.
店是一个配置文件,而配置文件在应用外面。
shop.mbd/ ← 应用。两家店共用
configs/
riverside.json ← 门店 1
hilltop.json ← 门店 2
同样的文件,不同的店

Charge 7000 won
shop.mbd — 3 files, sha256:f5f905958eac
[riverside.json] RIVERSIDE · 07:00 - 20:00 · 3 items on the menu
[hilltop.json] HILLTOP · 09:00 - 18:00 · 4 items on the menu
same screen file, two shops: RIVERSIDE due 7000 won vs HILLTOP due 12100 won
整个包都做了哈希。两家店之间不管有什么不同,都不在那里面。
验证就是一行 grep
这一篇的主张,可以浓缩成一个检查。
for NAME in Riverside Hilltop; do
if grep -RIiq "$NAME" shop.mbd shop_server/bin; then
echo " \"$NAME\" appears inside the app — the difference has leaked in"; exit 1
fi
done
neither shop name appears in shop.mbd or shop_server/bin
应用里一旦出现店名,就判失败。 将来谁一着急塞进一行 if (shop == 'Hilltop'),这个检查当场就红。这正是它存在的理由 — 那一行永远是在着急的时候进来的。