September 11th, 2009
The following example, taken from an introductory text in object oriented programming, demonstrates a common flaw in object oriented design. Can you spot it?
public class Rectangle {
private double width;
private double height;
public void setWidth(double width) {
this.width = width;
}
public void setHeight(double height) {
this.height = height;
}
public double getHeight() {
return this.height;
}
public double getWidth() {
return this.width;
}
public double getPerimeter() {
return 2*width + 2*height;
}
public double getArea() {
return width * height;
}
}
public class Square extends Rectangle {
public void setSide(double size) {
setWidth(size);
setHeight(size);
}
}
(I’ve changed the language and rewritten the code to protect the guilty.)
Read the rest of this entry »
Posted in Programming | 49 Comments »
June 30th, 2009
Pair programming is like magic in more ways than one. It dramatically improves programmer productivity and reduces bug count, and yet it does so through a technique that’s completely counter-intuitive. You can’t help but think that there’s some trick yet to be exposed; that pair programming is just slight of hand. In this article, I will endeavor to pull back the curtain and reveal the secrets of the pair programming magicians.
Specifically, I identify six reasons pair programming succeeds:
- Continuous Code Review
- Fewer blockages
- Masking distractions
- Guaranteed focus
- Multiple points of view
- Reduced training cost and time
Read the rest of this entry »
Posted in Programming | 36 Comments »
June 5th, 2009
The fundamental reason to prefer draconian error handling is because it helps find bugs. I was recently reminded of this when Peter Murray-Rust thought he had found a bug in XOM. In brief, it was refusing to parse some files other tools let slip right through. In fact, XOM’s strict namespace handling had uncovered a cascading series of bugs that had been missed by various other parsers including Xerces-2j and libxml.
But before I describe what happened, let’s see if you can eyeball this bug. I’ll make it easier by cutting out the irrelevant parts so you know you’re looking right at the bug. Here’s the instance document we start with:
<!DOCTYPE svg SYSTEM
"http://www.w3.org/TR/2000/03/WD-SVG-20000303/DTD/svg-20000303-stylable.dtd">
<svg/>
And the referenced DTD is:
<!ENTITY % StylableSVG "INCLUDE" >
<!ENTITY % ExchangeSVG "IGNORE" >
<!ENTITY % SVGNamespace "http://www.w3.org/2000/svg-20000303-stylable" >
<!ENTITY % Shared PUBLIC "-//W3C//DTD SVG 20000303 Shared//EN" "svg-20000303-shared.dtd" >
%Shared;
Then in svg-20000303-shared.dtd we find this:
<!ATTLIST svg
xmlns CDATA #FIXED "%SVGNamespace;"
%stdAttrs; >
Not obvious, is it? In fact, I looked at this one for quite a while, and consulted several spec documents before Tatu Saloranta figured out what was actually wrong here. If it helps the relevant part of the XML specification is Section 4.4, XML Processor Treatment of Entities and References.
Give up? OK. Here’s what’s happening:
Read the rest of this entry »
Posted in XML | 9 Comments »
May 27th, 2009
A couple of weeks ago I spent a considerable amount of time chasing down bugs involving null in a large code base: null checks after a variable had already been dereferenced, nulls passed to methods that would immediately dereference them, equals()
methods that didn’t check for null, and more. Using FindBugs, I identified literally hundreds of bugs involving null handling; and that got me thinking: Could we just eliminate null completely? Should we?
What follows is a thought experiment, not a serious proposal. Still it might be informative to think about it; and perhaps it will catch the eye of the designer of the next great language.
Read the rest of this entry »
Posted in Programming | 37 Comments »
January 16th, 2009
Sometimes I still feel like we’re in 1982 when it comes to really basic things like turning off a computer. Why do we have to select shutdown from a menu? Why do we have to carefully save each open file? Why don’t programs stop when we tell them to? (Time Machine has now been spinning for hours, and won’t stop even though I’ve told it to.) Why is this so much more complex than it needs to be?
In the future, here’s how shutdown should work:
- You flip the power switch.
That’s it. No shutdown menu item. No wait for the system to hibernate. No opportunity for applications to save data. Nothing.
Read the rest of this entry »
Posted in User Interface | 48 Comments »