Code lines and other metrics in F #

How to get good statistics about my F # code?

I could imagine things like

  • the number of lines of my code
  • number of files
  • Characters?
  • number of functions, classes, modules, etc.
+7
source share
3 answers

Why not use some simple shell utilities?

Answers are ok

wc -l *.fs ls -l *.fs | wc -l wc -c *.fs grep module *.fs | wc -l grep type *.fs | wc -l grep "^let\|member" *.fs | wc -l 

Update: Some examples for recursive folders - hope the template is obvious

 wc -l `find . -name "*.fs" ` find . -name "*.fs" | wc -l wc -c `find . -name "*.fs" ` grep module `find . -name "*.fs" ` | wc -l 
+9
source

Here is a version of F # that recursively counts fs and fsx (assuming you set the F # runtime):

 open System.IO open System.Text.RegularExpressions let rec allFiles dir = seq { yield! Directory.GetFiles dir yield! Seq.collect allFiles (Directory.GetDirectories dir) } let rgType = new Regex(@"type", RegexOptions.Compiled) let rgModule = new Regex(@"module", RegexOptions.Compiled) let rgFunction = new Regex(@"^let|member", RegexOptions.Compiled) let count (rg: Regex) s = s |> rg.Matches |> fun x -> x.Count type Statistics = { NumOfLines: int; NumOfFiles: int; NumOfChars: int; NumOfTypes: int; NumOfModules: int; NumOfFunctions: int; } let getStats = allFiles >> Seq.filter (fun f -> f.EndsWith ".fs" || f.EndsWith ".fsx") >> Seq.fold (fun acc f -> let contents = f |> File.ReadLines { NumOfLines = acc.NumOfLines + Seq.length contents; NumOfFiles = acc.NumOfFiles + 1; NumOfChars = acc.NumOfChars + Seq.sumBy String.length contents; NumOfTypes = acc.NumOfTypes + Seq.sumBy (count rgType) contents; NumOfModules = acc.NumOfModules + Seq.sumBy (count rgModule) contents; NumOfFunctions = acc.NumOfFunctions + Seq.sumBy (count rgFunction) contents; } ) { NumOfLines = 0; NumOfFiles = 0; NumOfChars = 0; NumOfTypes = 0; NumOfModules = 0; NumOfFunctions = 0 } 
+4
source

Since F # is compiled into IL-code and Common Type System (CTS) and PDB files , F # assemblies can be analyzed using the NDepend tool, and you will get 82 code metrics about your code (+ all other functions of the tool ). A free trial is available here .

Disclaimer, I am a member of the NDepend team.

0
source

All Articles