Archive for the ‘Software Development’ Category

Job security

Saturday, March 12th, 2011

A software developer reportedly wrote malicious code to ensure job security:

It wasn’t a club but a computer virus that shut down the Whac-A-Mole and more than 400 other games built by Bob’s Space Racers in Holly Hill. The company traced the problem back to computer programmer Marvin Wimberly.

Faced with a pay cut, police believe Wimberly programmed games to fail, ensuring he would be needed and keep making money.

[...]

Each game, after turning on and off a certain number of times, sometimes 50, sometimes 500, would fail. Wimberly would be paid to fix it, and police reports say, he would insert a new virus with a new countdown.

Of course, the big story here isn’t that the malicious action happened, but that it was discovered and traced back to the allegedly deliberate actions of an employee, and that police have been involved.

My guess would be that this type of thing goes on all the time, and is either not noticed or not taken seriously. I suspect it’s mostly not a deliberate sin of comission but a subtle sin of omission: developers don’t refactor, or don’t document, or don’t upgrade their design to integrate with new components. Quite probably they come up with ostensible reasons why it can’t or shouldn’t be done that are sufficiently convincing.

The irony, of course, is that doing a better job should make you more employable, not less. Even when unemployment was at its peak, the companies I’ve been working with have been terribly short of quality developers.

Here are some of factors that I think contribute to this:

  • People end up working in software development who don’t have the necessary interest or aptitude, but carry on with it because giving up would cause them to lose face, or because even a badly-paid software job is better than a great deal of alternatives.
  • Management in companies where software is only a small component of the business don’t necessarily understand the software development process, and as a result are overly deferential to the opinions of developers. Furthermore, they overestimate the cost of replacing an incompetent developer with a competent one, and underestimate the long-term benefit.
  • Bad developers are rewarded too well, and good developers too poorly. The best developers deliver something like 20 times the benefit of the worst developers, but are lucky to get paid twice as much. This blunts the incentive for a mediocre or poor developer to improve themselves, and encourages those at the bottom end of the scale to hang on rather than move to a different career to which they may be more suited.

Compile-time type identification

Monday, February 21st, 2011

Yesterday I was working on a class to produce natural-language translations of complex C++ types, and I said that there was no general way to map a fundamental type to a name. Of course, there is:

template<typename T>
class TypeDecryptor {
public:
    static string getName() {
        return typeid(T).name();
    }
};

Of course, not quite everything in the garden is lovely. This provides an implementation-defined string, which as far as I can see is not even required to be unique for a given type. In point of fact, it’s extremely unlikely an implementation will return a non-unique string since each type has to be mapped to a string for linkage purposes anyway. The problem is that the string isn’t necessarily human-readable.

I think that on reasonable implementations there should be a function available to unmangle this string back into a human-readable type, which means you can potentially make a completely general type decryptor.

C++ type-declaration decoder

Monday, February 21st, 2011

expert C programming front coverDespite the somewhat self-aggrandising title, I have to admit that Expert C Programming: Deep C Secrets by Peter Van Der Linden is the single most beneficial programming book I’ve ever read. I believe the point that I came across it marked the first step on a road from being an amateurish hacker who was happy with anything as long as it compiled, to being a software professional. Of course, by this point I’d already long been paid as if I were a software professional, simply because I have a degree in mathematics from a high-ranking university. Such is the way of things.

Anyway, the most useful section of Deep C Secrets is a section that gives a simple algorithm for understanding a complicated C type declaration. You know the kind of thing:

void (*signal(int sig, void (*func) (int) ) ) (int) ;

The algorithm, by the way, is given in a section that has the delicious title “The Piece of Code that Understandeth all Parsing.” I’d forgotten how funny that book is.

The main problem with this is that it’s a run-time operation, and has to take the type declaration as a string. Parsing it involves a whole bunch of logic that already exists in the compiler, but has to be re-implemented. When I was stumbling through some declarations in C++ Templates: The Complete Guide it dawned on me that maybe C++ can do better.

Understanding any type declaration can be broken down into two parts:

  • Knowing where to start
  • Knowing which piece to process next

The first of these is often rendered difficult by the fact that there are several identifiers in a typedef, and you have to know which is the one being defined (because this is where you start to parse the type). In an anonymous type, there may be no identifiers at all:

doStuff( static_cast&lt; int (*) ()&gt; foobar );

In this case you start with the *, but it’s not easy to see how to know this in general.

The second difficulty (knowing which piece of the type to handle next) is complicated by the fact that you may need to proceed left-to-right or right-to-left, which depends on precedence rules that most people understand only implicitly, and often only by instinct.

Using the C++ rules for template argument deduction, we can ignore most of these issues. The core idea is to declare a template that takes a single compound type and expresses the type in terms of one or more simpler components. For example, we can write a class that deals with a pointer:

template
class TypeDecryptor {
public:
	static string getName() {
		ostringstream output;
		output &lt;&lt; "pointer to "
                      &lt;&lt; TypeDecryptor::getName();
		return output.str();
	}
};

What this says is that the getName() method on a TypeDecryptor applied to a pointer type will return the string “pointer to” followed by whatever the type decryptor tells us the pointed-to type should be called. We can do something very similar for const:

template
class TypeDecryptor {
public:
	static string getName() {
		ostringstream output;
		output &lt;&lt; "const " &lt;&lt; TypeDecryptor::getName();
		return output.str();
	}
};

We’ve already got something useful, because it can deal with all that int const * const * stuff that people sometimes have problems with. OK, so you also need a TypeDecryptor specialisation for each of the fundamental types, which just prints out the type name:

template&lt;&gt;
class TypeDecryptor {
public:
	static string getName() {
		return "int";
	}
};

Annoyingly, I can’t find any way to generalise this, so you need an explicit specialisation for any fundamental or user-defined class type you want to support. It could be streamlined with a macro, of course.

So at this point, our decryptor can do things like this:

cout &lt;&lt; TypeDecryptor::getName() &lt;&lt; endl;
// Outputs "pointer to const pointer to const int"
 
cout &lt;&lt; TypeDecryptor::getName() &lt;&lt; endl;
// Outputs "const pointer to const pointer to int"

But this is just getting started. Similar things can be done with function pointers, arrays, references, pointers-to-member, etc. One of the more complex cases is:

template
class TypeDecryptor {
public:
	static string getName() {
		ostringstream output;
		output &lt;&lt; "pointer to a member function (on type "
			   &lt;&lt; TypeDecryptor::getName()
			   &lt;&lt; "), taking one argument of type "
			   &lt;&lt; TypeDecryptor::getName()
			   &lt;&lt; " and returning "
			   &lt;&lt; TypeDecryptor::getName();
		return output.str();
	}
};

This allows us to deal with pointers-to-member-function with one argument. Annoyingly, you need an explicit specialisation for every different number of function arguments there can be (one for zero-argument functions, one for one-argument functions, etc…) and also a different specialisation for pointers to data member from pointers to member functions. This means the number of explicit specialisations gets quite large quite quickly.

On the plus side, you don’t need to know anything about type precedence rules to write this code, nor have to make any decisions about where in the type declaration to start processing. The C++ compiler does all the hard work. Without too much effort I was able to get something that could parse:

char (Person::*)(int (&amp;)[42])

into

pointer to a member function (on type Person), taking one argument of type reference to array (of size 42) of instances of int and returning char