Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
技術的負債の定量化
# ref: 第8回 Perlによる大規模システム開発・設計のツボ(3):Perl Hackers Hub|gihyo.jp … 技術評論社
# http://gihyo.jp/dev/serial/01/perl-hackers-hub/000803
# コンポーネントの単一責務性の違反指数(SRP)
# SRP=R+U+((L/100)-5)
# R:修正リビジョンのユニーク数
# U:修正ユーザのユニーク数
# L:モジュールのライン数
function get_SRP() {
local target_filepath=$1
echo $(( \
$(git --no-pager blame --line-porcelain $target_filepath | sed -n 's/^summary //p' | sort | uniq -c | sort -rn | wc -l) + \
$(git --no-pager blame --line-porcelain $target_filepath | sed -n 's/^author //p' | sort | uniq -c | sort -rn | wc -l) + \
( $(cat $target_filepath | wc -l) / 100 - 5) \
))
}
# コンポーネントの循環的複雑度(CC)
# CC=循環的複雑度が20を超えるメソッド数
function get_CC() {
local target_filepath=$1
echo $( \
rubocop --format simple --only Metrics/CyclomaticComplexity --config <(echo -e "CyclomaticComplexity:\n Max: 20") $target_filepath | grep 'Cyclomatic complexity' | wc -l \
)
}
# コンポーネントの負債指数(P)
# P=SRP×CC+(SRP+CC)
function get_P() {
local srp=$1
local cc=$2
echo $(( $srp * $cc + ( $srp + $cc ) ))
}
# コンポーネント負債指数(P)が大きい順に "P SRP CC ファイル名" を標準出力
for file in `git ls-files app lib | grep -E '\.rb$'`; do
local srp=$(get_SRP $file)
local cc=$(get_CC $file)
echo $( get_P $srp $cc ) $srp $cc $file
done | tee technical_dept_result.log | sort -k1,1 -nr
# 累積値 "ΣP ΣSRP ΣCC" を標準出力
cat technical_dept_result.log | awk 'BEGIN{p=0;srp=0;cc=0}{p+=$1;srp+=$2;cc+=$3;}END{print p" "srp" "cc}'
@KenshoFujisaki

This comment has been minimized.

Copy link
Owner Author

commented Jun 24, 2015

コミットごとに負債値を出力

IFS=$'\n'
for line in $(git --no-pager log --oneline --date=short --pretty=format:"[%ad] %h [%an] %s" --merges origin/master | grep -v http); do
  echo $line | tee -a result_20150625.log
  git checkout $(echo $line | awk '{print $2}')
  ./technical_dept.sh | tee -a result_20150625.log
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.