Cargo cult programming

A simple utility function suddenly becomes not so simple:

QString radToDmsStr(double angle, bool decimal, bool useD)
{
// ...
	int width;
	if (decimal)
	{
		os << qSetRealNumberPrecision(1);
		width = 4;
	}
	else
	{
		os << qSetRealNumberPrecision(0);
		width = 2;
	}
	os << fixed << qSetFieldWidth(width) << qSetPadChar('0');
	if (s<=0.1)
		os << QString("%1").arg(s, 0, 'f', 2);
	else
		os << s;
// ...
}

There are a few problems with that code. Can you spot them? If you don’t know any Qt, you probably can’t, but they are there. It’s not exactly material for the DailyWTF, but it’s still annoying enough to make me write this. An appropriate alternative title would be “RTFM!”.

The more obvious problem is the use of QString::arg() to display a formatted floating-point value in a text stream that otherwise uses only stream manipulators to do the same.

The less obvious, but more idiotic problem is in the last argument of the arg(). Adding it was the reason why arg() was used. What does it do? It sets the floating point number precision, i.e. number of digits after the decimal sign. Which has already been done with qSetRealNumberPrecision() a few lines above – someone had made the effort to set a custom precision with a parameter of the function. But if the value is less than 0.1, the precision happily gets overwritten…

Now excuse me, I have to go waste my time finding out what was the point of the original distinction and why the new change was made. Actually, no, I won’t. I don’t get paid for this. I had to spend two hours today listening to someone babbling platitudes about software project management and quality assurance, all in the name of making progress in my education. If I want to make sure that those two hours were not wasted, I should be studying right now, not venting on this blog or debugging someone else’s mistakes.

About these ads
This entry was posted in Humour, Programming, Venting. Bookmark the permalink.

2 Responses to Cargo cult programming

  1. lum2012 says:

    No other way to contact somebody of the project.
    Ok,.. I didn’t find a function to stop time elapse or something depending on “planetary alignments” or occuring “gravitational lenses”. For example: fukushima had a jupiter – sun – earth alignment..
    Cool script idea: If one Alignment occurs – jump direct to the GPS coordinate which is in direct line…

    • lum2012 says:

      ., in real life: go away… (Some things: Bremsstrahlung, Synchrotonstrahlung, gravitational lenses and other things)

Comments are closed.