Skip to content

Instantly share code, notes, and snippets.

@sortega
Created July 13, 2013 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sortega/5991126 to your computer and use it in GitHub Desktop.
Save sortega/5991126 to your computer and use it in GitHub Desktop.
Quick and dirty script to measure how null-checking prevalence in Java programs.
#!/usr/bin/env perl
use strict;
use warnings;
sub locs {
my $repo = shift;
`sloccount $repo | grep ^java` =~ /^java:\s+(\d+)/;
return $1;
}
sub conditionals {
my $repo = shift;
my $conditionals = 0;
my $nulls = 0;
open(my $lines, '-|', "find $repo -name '*.java' -exec cat {} \\; | egrep '\\<(if|while|for)\\>\\s*\\('")
or die("Cannot read $repo\n");
while(my $line = <$lines>) {
$conditionals++;
$nulls++ if $line =~ /\bnull\b/;
}
close($lines);
return 100 if $conditionals == 0;
return 100 * $nulls / $conditionals;
}
sub repo_age {
my $repo = shift;
my $timestamp = `cd $repo && git log --reverse --format=%ct . | head -n 1`;
chomp $timestamp;
return (time - $timestamp) / (3600 * 24 * 365);
}
sub print_stats {
my $repo = shift;
printf "\"%s\",%d,%d,%5.2f\n", $repo,
locs($repo), repo_age($repo), conditionals($repo);
}
my $base = $ARGV[0];
opendir(my $d, $base) or die "Cannot read dir $base\n";
print "repo,locs,years,nullitis\n";
while(my $name = readdir $d) {
my $path = "$base/$name";
next if $name =~ /^\./;
next unless -d $path;
print_stats($path);
}
close($d);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment