A Lovely Computer

Capítulo 5. A Message to nil

Part II. The Developer Owns It7 min · en inglés

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.

Gratis, solo falta un paso

El resto del libro es gratis

Deja tu correo y te mando un enlace para confirmarlo. Después del clic, todos los capítulos se abren aquí, en este dispositivo, durante un año. Cuando salga un libro nuevo, escribiré a esa dirección; responde “basta” a cualquier correo y no vuelvo a escribir.

¿Tienes cuenta INITE? Entra y el libro se abre sin correo.

No se cobra nada por el libro. Si quieres apoyarlo, puedes donar desde cualquier capítulo.

Cómo se guarda la dirección