Skip to content

Instantly share code, notes, and snippets.

View michaelfeathers's full-sized avatar

Michael Feathers michaelfeathers

View GitHub Profile
@michaelfeathers
michaelfeathers / gist:3838607
Created October 5, 2012 07:45
Turbulence Light-er
def complexity filename
File.read(filename).lines.grep(/^([ \t]*)/) { $1 }.map(&:length).reduce(0, :+)
end
filenames = `git log --name-only | grep \.rb$`.lines \
.map(&:strip) \
.reject { |fn| fn.include? "_spec" } \
.reject { |fn| fn.include? "vendor" }
@michaelfeathers
michaelfeathers / gist:3837149
Created October 4, 2012 23:39
Turbulence Light - generates a turbulence dataset for a Ruby git repository
def complexity filename
File.read(filename).lines.grep(/^([ \t]*)/) { $1 }.map(&:length).reduce(0, :+)
end
commit_filenames = `git log --name-only | grep \.rb$`.lines \
.map(&:strip) \
.reject { |fn| fn.include? "_spec" } \
.reject { |fn| fn.include? "vendor" }
@michaelfeathers
michaelfeathers / gist:3832740
Created October 4, 2012 10:19
Descending sort of Ruby files in a repository by number of commits
git log --name-only --no-merges | grep \.rb$ | sort | uniq -c | sort -nr
@michaelfeathers
michaelfeathers / gist:3827233
Created October 3, 2012 14:33
Code volume
# A poor man's complexity measure for a file or group of files
puts ARGF.lines.grep(/^([ \t]*)/) { $1 }.map(&:length).reduce(0, :+)
import Data.List
lineBreak :: String -> String
lineBreak = joinBrokenLines . joinWordsInBrokenLines . brokenLines . words
brokenLines :: [String] -> [[String]]
brokenLines [] = []
brokenLines wordList = brokenLine : brokenLines remainingWords
where (brokenLine, remainingWords) = splitAt (brokenLineWordCount wordList) wordList
@michaelfeathers
michaelfeathers / gist:2413318
Created April 18, 2012 12:34
Each versus Reduce
/* On Array */
VALUE
rb_ary_each(ary)
VALUE ary;
{
long i;
for (i=0; i<RARRAY(ary)->len; i++) {
@michaelfeathers
michaelfeathers / gist:1855765
Created February 17, 2012 22:19
Five lines that turn Ruby into a different language
class Object
def method_missing m, *args
Object.respond_to?(m, true) ? Object.send(m, self, *args) : super
end
end
-- Modeled on the behavior of https://github.com/burkhartt/Tim.SentenceParser/blob/master/Tim.SentenceParser/Tim.SentenceParser/SentenceParser.cs
import Char
withoutLastWord :: String -> String
withoutLastWord = unwords . init . words
sentenceBefore :: String -> Int -> String
sentenceBefore "" _ = ""
sentenceBefore _ 0 = ""
@michaelfeathers
michaelfeathers / gist:1316333
Created October 26, 2011 13:23
Ruby in Haskell style
# :: [event] -> [month,int]
def avg_lines_per_commit_by_month events
cls_by_month = lines_added_per_commit(events).group_by {|date,_| month_from_date(date) }
cls_by_month.map {|_,cls| cls.map {|cl| cl[1]}.mean }.flatten
end
# :: [event] -> Float
def percent_reduction method_events
non_deleted = method_events.select {|e| e.status != :deleted }
return 0.0 if non_deleted.count == 0
def perform
@results ||= {}
puts "Looking up query"
query = Query.find_by_id(options["query_id"])
engine = Quote::Engine.create(query)
puts "Finding quotes"
quotes = engine.find_quotes(options["options"])
puts "Found quotes"
# NOTE:
# This is here so that sorting and filtering can take advantage