"Star"tlingly Bad Code

Scott Hanselman’s got my attention this morning with his recent endeavour to launch a community driven reference application for WPF. If you haven’t already read about BabySmash! go read about it here and check it out on codeplex here.

Scott mentions that the whole thing was put together whilst his wife was watching a rather unappealing movie. He then goes on to mention that the code is bad – really bad. What gets me though is that he has the audacity, nay the gall to suggest that just because he is capable of writing lame-ass code that by some bizarre form of association that his readers too sometimes write code like this. Now – I’ve downloaded the source code for BabySmash! – and let me tell you the code is pretty ugly. If Scott thinks that I’d ever let code like that be published under my name then… wait… ah… BUGGER! He’s used some of my code! What’s worse – he used probably the MOST lame-ass piece of code I’ve ever blogged (oh heck – yes OK I’m sure there are even worse examples of mine).

The code in question was a class I blogged about in November last year. I should point out that I made it very clear at the top of the post that this was more or less a rant – “dribble code” if you will. Its a very simply class that derives from Shape to draw a Star.

The method I used to draw the star was to take a triangle and simply “stamp” it out multiple times, rotating it around a central axis. The very clever WPF GetOutlinedPathGeometry is then used to clean up all the mess and consolidate into just the outline of the star.

Here’s the original code that appears now in Scott’s BabySmash:

public static Geometry CreateStarGeometry(int numberOfPoints)
GeometryGroup group = new GeometryGroup();
group.FillRule = FillRule.Nonzero;
Geometry triangle = PathGeometry.Parse("M 0,-30 L 10,10 -10,10 0,-30");
double deltaAngle = 360 / numberOfPoints;
double currentAngle = 0;
for (int index = 1; index < numberOfPoints; index++)
currentAngle += deltaAngle;
triangle = triangle.CloneCurrentValue();
triangle.Transform = new RotateTransform(currentAngle, 0, 0);
Geometry outlinePath = group.GetOutlinedPathGeometry();
return outlinePath;

The code that I replaced that with the first time I actually used the Star class in an application (for a rating indicator) is shown below. It takes what I think is a much neater approach and is a little easier to configure with an inner and outer radius.

public static Geometry CreateStarGeometry2(
int numberOfPoints,
int outerRadius,
int innerRadius,
Point offset) { List<PathSegment> segments = new List<PathSegment>(); double angleOffset = Math.PI * 2 / numberOfPoints; for (double angle = 0; angle < Math.PI * 2; angle += angleOffset) { double innerAngle = angle + angleOffset / 2; Point outerPoint = new Point(
Math.Sin(angle) * outerRadius + offset.X,
Math.Cos(angle) * -outerRadius + offset.Y); Point innerPoint = new Point(
Math.Sin(innerAngle) * innerRadius + offset.X,
Math.Cos(innerAngle) * -innerRadius + offset.Y); segments.Add(new LineSegment(outerPoint, true)); segments.Add(new LineSegment(innerPoint, true)); } List<PathFigure> figures = new List<PathFigure>(); figures.Add(new PathFigure(
new Point(0 + offset.X, -outerRadius + offset.Y),
segments, true)); Geometry star = new PathGeometry(figures); return star; }

As embarrassed as I am that my Star class has meandered its way to such a large audience – I must admit I kinda got a kick out of seeing this in Scott’s code .

In terms of the BabySmash! application itself – I think this is a great idea. Firstly from the point of view that I also have two young kids who love spending time with me on the computers. The younger two year old is at the stage where she recognizes most of the alphabet and loves typing random letters and seeing them appear on the screen. I wrote my WinForms animation sample primarily because my oldest(then 4) got a kick out of watching the bits dance around the screen (a later version included A-Z characters). [Alas he’s now turned 5 and has since moved on to solving Bloxorz levels – scary!]

Secondly the idea of a community driven WPF application just sounds like a great idea. I’ve been very slowly trying to build my own “hobby” application using WPF. Stumbling and being sidetracked at every turn – all very educational for me – but certainly delaying any semblance of a deliverable. Maybe Scott’s approach is the way to go – hack it together ASAP – then let the community work together to discuss, refactor and enhance?

One thought on “"Star"tlingly Bad Code”

Comments are closed.