We are using Mercurial at work. We are also using Bugzilla. Unfortunately we are not using Bugzilla 2.16 which is the version of Bugzilla that hg ships an integration hook with. So I modified the hook to work with the version of Bugzilla that we are using (3.1.4+ cvs trunk from someday in the past couple months). The script can now be found in my personal hg repo. It should work with any Bugzilla installation that comes with the email_in.pl script (since 3.0 maybe?).
Please do tell me what you think of it. It is the first Python script I've really done anything with (I'm more a Perl kind of guy). As always, feel free to do whatever you want with it (respecting the license of the original author of course). I'd love to see it get integrated back into Mercurial, but as I have made no real effort to test any of it (it works for me, but no promises and all that) I don't expect that any time soon. Perhaps someone at Mozilla would take the time to do that; this is probably useful for integrating mozilla-central with bmo.
Edit: link updated (should have pointed to tip in the first place). I've fixed both the syntax errors (a right parenthesis was in the wrong place, I must have copied it off the server incorrectly, because I swear I fixed the syntax errors that were there when it was on the server, but copying files out of nano over ajaxterm isn't exactly the easiest thing to do; while updating this, I realized that I could do a wget to get this file from my repo, so I can actually test the version at the above url. Rest assured it works this time). Thank you very much geraldfauvelle for noticing that it was broken.
Showing posts with label code. Show all posts
Showing posts with label code. Show all posts
Sunday, July 6, 2008
Sunday, June 1, 2008
possible code quality equations
So... I haven't posted in a while; here's why: I have been using the firefox 3 nightlies for a long time now and blogger was crashing the past couple of times I tried posting. As I was not about to give up Firefox 3 (it is definitely one of the best pieces of software ever, I am completely dependant on the awesomeBar; using the star for bookmarking and the speed aren't bad either; next post will talk more). It appears as though I can now post from Fx3. That means more posts again.
This was something I posted a while ago on the alt.net list and am reposting it here to keep it for remembering to try out sometime in the future:
In regards to:
http://www.sei.cmu.edu/str/descriptions/mitmpm.html#78991
...
I don't believe this equation (from that article) will give very meaningful results. For one thing it places a very odd value on the percent of comments.
For that portion, here is a table:
So, 1% comments is worth the same amount as 25%? And 10% is downright awful (note that the perCM variable is not used in the data, implying that it may not be a good variable)
171 - 5.2 * ln(aveV) - 0.23 * aveV(g') - 16.2 * ln (aveLOC) + 50 * sin (sqrt(2.4 * perCM))
The coefficients are derived from actual usage (see Usage Considerations). The terms are defined as follows:
aveV = average Halstead Volume V per module (see Halstead Complexity Measures)
aveV(g') = average extended cyclomatic complexity per module (see Cyclomatic Complexity)
aveLOC = the average count of lines of code (LOC) per module; and, optionally
perCM = average percent of lines of comments per module
Also I don't know any tools that will compute the Halstead Volume for .NET. In general I think that those 3 metrics would be useful in determining maintainability and that aveV and aveLOC should probably be on a log scale, but the numbers appear to be arbitrarily picked to support the conclusions of the research.
There is a tool that can be used to get many useful metrics out of a codebase:
http://www.ndepend.com/Metrics.aspx
I think the following on that page are particularly useful for measuring code quality:
overall:
NbLinesOfCode
PercentageCoverage
averaged (per assembly)
Instability
Abstractness
Distance from main sequence (abs(I+A-1), ideally as close to 0 as possible)
averaged (per type)
LCOM HS (Lack of Cohesion Of Methods - Henderson-Sellers; basically can tell you if your type is physically disregarding Separation of Concerns by not being cohesive)
ILCC (IL level cyclomatic complexity; afiak the only reason to use this one is because it can be computed for any .NET code, not just C#)
Depth of Inheritance Tree
averaged (per method)
IL Nesting Depth
Additionally, some other functions could be very useful:
average(PercentageCoverageMethodLevel * MethodRank) (will cause more important methods to be weighted more when computing code coverage)
or
average(PercentageCoverageTypeLevel * TypeRank) (would do the same thing for types)
I'd say if you want to have some sort of scale to come up with good individual functions for how each factor contributes to overall quality, and then either do a sum of the functions or some kind of weighted average.
For example, a decent function for basing quality solely on code coverage by unit tests could be (each function has been normalized to give output on a 10 point scale):
Q(x) = (arcTan(10x-5)+pi/2-.1)*11/pi
where x is [0,1], the decimal version of %coverage
or
Q(x) = (arcTan(10x-7)+pi/2-.1)*11/pi
where x is [0,1] = AVERAGE(the decimal version of %coverage at the method/type level * method/type rank)
LCOM HS could be:
R(x) = -12.5x^3 + 31x^2 - 28x + 10
x is [0,1]
ILCC could be:
S(x) = 10.4e^(-0.06x)
x is [1,inf)
and LOC could be:
T(x) = -0.4*ln(x) + 10
x is [1,inf)
And the final score could be the average of all of those parts.
This was something I posted a while ago on the alt.net list and am reposting it here to keep it for remembering to try out sometime in the future:
In regards to:
http://www.sei.cmu.edu/str/descriptions/mitmpm.html#78991
...
I don't believe this equation (from that article) will give very meaningful results. For one thing it places a very odd value on the percent of comments.
For that portion, here is a table:
perCM | 50 * sin (sqrt(2.4 * perCM))
0 | 0
1 | 50
2 | 40
3 | 22
4 | 2
5 | -16
...
10 | -50
15 | -15
20 | 30
25 | 50
So, 1% comments is worth the same amount as 25%? And 10% is downright awful (note that the perCM variable is not used in the data, implying that it may not be a good variable)
171 - 5.2 * ln(aveV) - 0.23 * aveV(g') - 16.2 * ln (aveLOC) + 50 * sin (sqrt(2.4 * perCM))
The coefficients are derived from actual usage (see Usage Considerations). The terms are defined as follows:
aveV = average Halstead Volume V per module (see Halstead Complexity Measures)
aveV(g') = average extended cyclomatic complexity per module (see Cyclomatic Complexity)
aveLOC = the average count of lines of code (LOC) per module; and, optionally
perCM = average percent of lines of comments per module
Also I don't know any tools that will compute the Halstead Volume for .NET. In general I think that those 3 metrics would be useful in determining maintainability and that aveV and aveLOC should probably be on a log scale, but the numbers appear to be arbitrarily picked to support the conclusions of the research.
There is a tool that can be used to get many useful metrics out of a codebase:
http://www.ndepend.com/Metrics.aspx
I think the following on that page are particularly useful for measuring code quality:
overall:
NbLinesOfCode
PercentageCoverage
averaged (per assembly)
Instability
Abstractness
Distance from main sequence (abs(I+A-1), ideally as close to 0 as possible)
averaged (per type)
LCOM HS (Lack of Cohesion Of Methods - Henderson-Sellers; basically can tell you if your type is physically disregarding Separation of Concerns by not being cohesive)
ILCC (IL level cyclomatic complexity; afiak the only reason to use this one is because it can be computed for any .NET code, not just C#)
Depth of Inheritance Tree
averaged (per method)
IL Nesting Depth
Additionally, some other functions could be very useful:
average(PercentageCoverageMethodLevel * MethodRank) (will cause more important methods to be weighted more when computing code coverage)
or
average(PercentageCoverageTypeLevel * TypeRank) (would do the same thing for types)
I'd say if you want to have some sort of scale to come up with good individual functions for how each factor contributes to overall quality, and then either do a sum of the functions or some kind of weighted average.
For example, a decent function for basing quality solely on code coverage by unit tests could be (each function has been normalized to give output on a 10 point scale):
Q(x) = (arcTan(10x-5)+pi/2-.1)*11/pi
where x is [0,1], the decimal version of %coverage
or
Q(x) = (arcTan(10x-7)+pi/2-.1)*11/pi
where x is [0,1] = AVERAGE(the decimal version of %coverage at the method/type level * method/type rank)
LCOM HS could be:
R(x) = -12.5x^3 + 31x^2 - 28x + 10
x is [0,1]
ILCC could be:
S(x) = 10.4e^(-0.06x)
x is [1,inf)
and LOC could be:
T(x) = -0.4*ln(x) + 10
x is [1,inf)
And the final score could be the average of all of those parts.
Friday, December 21, 2007
WatiN testing ASP.NET - app startup time issues
I started a test project to test a web app. One thing I noticed was that due to application startup, occasionally the first couple of tests fail with TimeoutExceptions because the application takes too long to compile some of the pages.
So, I came up with the following to work around this:
This needs to run before any unit tests.
So, I came up with the following to work around this:
using SHDocVw;
using WatiN.Core;
using WatiN.Core.Exceptions;
namespace Tests {
public class TestBase {
private static bool _siteSetupRun = false;
public TestBase() {
if (!_siteSetupRun) {
setupSite();
_siteSetupRun = true;
}
}
private static void setupSite() {
bool ok = false;
using (IE ie = new IE()) {
object nil = null;
((InternetExplorer)ie.InternetExplorer).Navigate("http://testsite/",
ref nil, ref nil, ref nil, ref nil);
while (!ok) {
try {
ie.WaitForComplete();
ok = true;
} catch (TimeoutException tex) {
if (!tex.Message.Contains("'Internet Explorer busy'")) {
throw;
}
}
}
}
}
}
}
This needs to run before any unit tests.
Sunday, December 9, 2007
Resolving extension method conflicts using a Proxy pattern.
If you happen to get this error: "The call is ambiguous between the following methods or properties: ...", here is how you would fix it:
Lets say you are using the following library:
And the library authors decide to release an extension method (along with a couple of others you wish to use):
Meanwhile you are using a third party extension method library which already contains this method:
This is a problem because you can no longer call the CapitalizeName method as an extension method if you happen to have both namespaces in your using constructs. As long as you are using functionality from both classes where the names of the methods are different you will not have any conflicts and there will not be any problems using your code. Unfortunately, the moment you add a call to the CapitalizeName method you will get a compile time error.
This will not compile because CapitalizeName cannot be resolved.
Because extension methods are static methods and can be called just as any other static method gets called, you can build a wrapper class around these third party extension methods in order to resolve the conflicts that the method names impose.
The extension method proxy class can now be used in order to resolve the naming conflict.
If you are going to use more than 1 extension method library ever in the course of working on a project, I believe it is awfully important to encapsulate the extension methods you will be using into a proxy class (or several such classes) as I have shown here. It is probably best to keep this class internal as you wouldn't want to continue to pollute the function name domain to other third party libraries which are using your code.
I will argue that all calls to extension methods from third party libraries be internalized in this way. Doing so adds value (you can add xml documentation to these methods for your coworkers and the R# tools will be able to find usages and such) and it better decouples you from minor API changes in the extension libraries.
--
Thanks to Peter Ritchie on the altnetconf mailing list for most of the above code in the first section of this post.
Problem Setup
Lets say you are using the following library:
namespace PRI.Interfaces {
public interface IEntity {
string Name { get; set; }
}
}
namespace PRI {
public class DataEntity:Interfaces.IEntity {
public string Name { get; set; }
}
}
And the library authors decide to release an extension method (along with a couple of others you wish to use):
namespace PRI.Extensions {
using PRI.Interfaces;
public static class Entity {
public static string CapitalizeName(this IEntity entity) {
entity.Name = entity.Name.ToUpper();
return entity.Name;
}
}
}
Meanwhile you are using a third party extension method library which already contains this method:
namespace Contoso.Extensions {
using PRI.Interfaces;
using System.Text;
public static class Entity {
public static string CapitalizeName(this IEntity entity) {
StringBuilder sb = new StringBuilder(entity.Name.Length);
string[] words = entity.Name.Split(new char[] { ' ' });
foreach(string word in words) {
sb.Append(char.ToUpper(word[0]));
sb.Append(word.Substring(1).ToLower());
sb.Append(" ");
}
entity.Name = sb.ToString().Trim();
return entity.Name;
}
}
}
The Problem
This is a problem because you can no longer call the CapitalizeName method as an extension method if you happen to have both namespaces in your using constructs. As long as you are using functionality from both classes where the names of the methods are different you will not have any conflicts and there will not be any problems using your code. Unfortunately, the moment you add a call to the CapitalizeName method you will get a compile time error.
namespace Program {
using System;
using PRI;
using PRI.Extensions;
using Contoso.Extensions;
class Program {
static void Main(string[] args) {
DataEntity dataEntity = new DataEntity() { Name = "frank smith" };
dataEntity.CapitalizeName();
Console.WriteLine(dataEntity.Name);
}
}
}
This will not compile because CapitalizeName cannot be resolved.
Enter the Proxy Pattern
Because extension methods are static methods and can be called just as any other static method gets called, you can build a wrapper class around these third party extension methods in order to resolve the conflicts that the method names impose.
namespace Program.ExtensionResolvers {
using PRI.Interfaces;
internal static class Resolver {
public static string CapitalizeName(this IEntity entity) {
return PRI.Extensions.Entity.CapitalizeName(entity);
}
public static string ConvertNameToTitleCase(this IEntity entity) {
return Contoso.Extensions.Entity.CapitalizeName(entity);
}
}
}
Using the Solution
The extension method proxy class can now be used in order to resolve the naming conflict.
namespace Program {
using System;
using PRI;
using ExtensionResolvers;
class Program {
static void Main(string[] args) {
DataEntity dataEntity = new DataEntity() { Name = "frank smith" };
dataEntity.ConvertNameToTitleCase();
Console.WriteLine(dataEntity.Name);
}
}
}
Recommendation
If you are going to use more than 1 extension method library ever in the course of working on a project, I believe it is awfully important to encapsulate the extension methods you will be using into a proxy class (or several such classes) as I have shown here. It is probably best to keep this class internal as you wouldn't want to continue to pollute the function name domain to other third party libraries which are using your code.
I will argue that all calls to extension methods from third party libraries be internalized in this way. Doing so adds value (you can add xml documentation to these methods for your coworkers and the R# tools will be able to find usages and such) and it better decouples you from minor API changes in the extension libraries.
--
Thanks to Peter Ritchie on the altnetconf mailing list for most of the above code in the first section of this post.
Wednesday, October 24, 2007
Schwartzian Transform in C# 2.0
In case someone was reading Joe Cheng's post about performing a Schwartzian Transform in C# 3.0 and was wondering what it would look like in 2.0 code, here it is:
private static void SchwartzSort<E, S>(List<E> list,
Converter<E, S> converter,
Comparison<S> comparison) {
List<KeyValuePair<S, E>> pairs = list.ConvertAll<KeyValuePair<S, E>>(delegate(E x) {
return new KeyValuePair<S, E>(converter(x), x);
});
pairs.Sort(delegate(KeyValuePair<S, E> x, KeyValuePair<S, E> y) {
return comparison(x.Key, y.Key);
});
for (int i = 0; i < list.Count; i++) {
list[i] = pairs[i].Value;
}
}
/// <summary>
/// Sorts a list using a Schwartzian transform (applies a conversion
/// function to each element and sorts by the value of that function).
/// </summary>
/// <typeparam name="E">The type of element to be sorted</typeparam>
/// <typeparam name="S">The type of the element to sort by</typeparam>
/// <param name="list">The list to be sorted (sort occurs in place).</param>
/// <param name="converter">
/// The converter function (converts element of type <code>E</code> to type <code>S</code>).
/// </param>
/// <param name="comparison">
/// The comparison function, compares 2 values of type <code>S</code>.
/// </param>
/// <seealso cref="Sort{E,S}(List{E},Converter{E,S})"/>
public static void Sort<E, S>(List<E> list, Converter<E, S> converter, Comparison<S> comparison) {
SchwartzSort(list, converter, comparison);
}
/// <summary>
/// Sorts a list using a Schwartzian transform using the default comparer
/// on type <code>S</code> (applies a conversion function to each element
/// and sorts by the value of that function).
/// </summary>
/// <typeparam name="E">The type of element to be sorted</typeparam>
/// <typeparam name="S">The type of the element to sort by</typeparam>
/// <param name="list">The list to be sorted (sort occurs in place).</param>
/// <param name="converter">
/// The converter function (converts element of type <code>E</code> to type <code>S</code>).
/// </param>
/// <seealso cref="Sort{E,S}(List{E},Converter{E,S},Comparison{S})"/>
public static void Sort<E, S>(List<E> list, Converter<E, S> converter) where S:IComparable {
SchwartzSort(list, converter, delegate(S x, S y) { return x.CompareTo(y); });
}
Obviously this is not quite as aesthetically pleasing as the 3.0 version, but it is still useful for any developers doing 2.0 coding out there and need to sort a list by the results of a function. Here is an example of how it would be used:
public class foobar {
private string _foo;
private string _bar;
public string Foo {
get { return _foo; }
set { _foo = value; }
}
public string Bar {
get { return _bar; }
set { _bar = value; }
}
public foobar(string foo, string bar) {
_foo = foo;
_bar = bar;
}
}
Assuming class foobar:
List<foobar> items = new List<foobar>(4);
items.Add(new foobar("a","b"));
items.Add(new foobar("asdf","jkl"));
items.Add(new foobar("green","purple"));
items.Add(new foobar("c", "a"));
Sort<foobar,string>(items, delegate(foobar item) { return item.Foo; });
Subscribe to:
Posts (Atom)