Skip to content

Instantly share code, notes, and snippets.

@davidchambers
Last active December 13, 2015 18:49
Show Gist options
  • Save davidchambers/4958487 to your computer and use it in GitHub Desktop.
Save davidchambers/4958487 to your computer and use it in GitHub Desktop.

flatter

flatter is a command-line tool for determining a source file's "indentedness", which provides a rough indication of how well factored the code is.

$ flatter src
#!/usr/bin/env coffee
fs = require 'fs'
{resolve} = require 'path'
identity = (x) -> x
analyze = (path) ->
lines = fs.readFileSync(path, 'utf8').split(/\r\n|\r|\n/)
significantLines = lines.filter identity
indents = significantLines.map (line) -> /^[ \t]*/.exec(line)[0].length
indents = indents.reduce (sum = 0, n) -> sum + n
path: path
lines: lines.length
indents:
total: indents
average: indents / significantLines.length
log = (stats) ->
console.log """
#{stats.path}
lines: #{stats.lines}
average indent: #{stats.indents.average.toFixed 1}
"""
walk = (path) ->
if fs.statSync(path).isDirectory()
walk resolve path, p for p in fs.readdirSync path
else
log analyze path
walk resolve process.cwd(), p for p in process.argv[2..]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment