05
A Message to nil
In March 2011 I wrote this loop in one of my files:
for (Device *dev in devices)
{
[self setValue:dev forKey:[dev valueForKey:@"type"]];
}
The file was called DeviceConfigurator.m and belonged to a program for the test bench on which we tried out our router, the NEO. Its database held a list of devices, and before every test each of them had to be handed its settings. In the file’s header, where the company name should be, there stands to this day __MyCompanyName__, a placeholder the development environment fills in by itself and which you are supposed to replace, but routers interested me more than copyright at the time.
There is not a single condition in this loop, although the devices on the bench were of two kinds, the routers themselves and the machines at the edges that pushed traffic through them, and they were configured differently. The kind of device was stored in the database as a plain string, in a field called type, and the loop takes that string and asks the object to set a value under that name. The system, given the name neo, assembles from it the method name setNeo: and looks for a method of that name on the object, and given endpoint it looks for setEndpoint:. Both methods are written lower down in the same file, and each knows how to configure its own kind of device. Which of them will be called nobody knows at compile time, the compiler included, because the method’s name is taken from a database when the program is already running.
In Java or C++ the same thing is written as a switch statement, where every kind of device is listed by hand and the compiler checks that all the methods mentioned exist. Nobody checked my loop. Had someone added a third kind of device to the database, the program would have built without a single warning and crashed on the bench, with an NSUndefinedKeyException, at the second the new device’s turn came. I thought this a fair trade, because a new kind of device was added with one method and one row in the database, and the loop never had to be touched.
Free, one step away
The rest of the book is free
Leave your email and I will send a link to confirm it. Once you click it, every chapter opens here, on this device, for a year. When a new book comes out I will write to this address; reply “stop” to any email and I will not write again.
Have an INITE account? Sign in and the book opens without an email.
I do not charge for the book. If you would like to support it, you can donate from any chapter.
How your email is stored