• Print Media Organization
  • scislug@gmail.com
  • UC Santa Cruz

Space to Princess Brutus: Unwritten in Code

We spend much our lives chasing enough of something, to place into something else: words for our thesis, lines for our programs, lovers for our hearts and a long list of other trinkets for the pockets of our life which may or may not be representable as a finite number of Hilbert space products–but what about the spaces between our words, line breaks in our programs and the separate worlds we inhabit that make our union with another human so much sweeter? Without space, whatwehavemaybedamnnearillegibleandthuspointlessasallhellanyway.

Space I scarcely thought about until I worked with a my friend Che on a C programming project was that in code. As a undergraduate physicist, I took a smattering of lower-division computer science classes and my instructors’ comments on code style was, “Make it more or less legible for me.” We weren’t required to adopt any code style in particular, so we each wound up with our own ad-hoc style the changed depending on how much coffee we’d drank in the past few hours, and how much sleep we’d had in the last couple of days.

One of the first functions I sent my buddy was similar to the one below, but maybe not quite as bad:

[code language=”cpp”]
/* Multiply two matrices together. First check that the matrices have
appropriate dimensions. */
#include “matrix.h”
struct matrix* mtx_multiply(struct matrix* a, struct matrix* b){
if (a->cols != b->rows) {
fprintf (stderr, “Incompatible dimensions for matrices %p and %p.\n”, a, b);
return 0;} // end if: checking matrix dimensions
struct matrix *m = mtx_new (a->rows, b->cols);
size_t row, col, add;
for(row=0 ;row rows;row++){
for( col=0;colrows;col++) {
for(add= 0;add rows;add++)
{
m->data[row][col] += a->data[row][add] * b->data[add][col];
}}}
return m;}
[/code]

The nice, or maybe horrible, thing about C, is it ignores whitespace almost entirely, it was after all an April Fool’s Joke. So the above code, and the code below execute exactly the same. However, the above borders on incomprehensibility–at the very least it’s not easy or fun to read. If you’re bored, work on a more difficult project rather than obfuscating a simple program–you’ll learn more. Unless you really, really want to, but then you wouldn’t bother reading this anyway.

[code language=”cpp”]
/* Multiply two matrices together. First check that the matrices have
appropriate dimensions. */

#include “matrix.h”

struct matrix *
mtx_multiply (struct matrix *a, struct matrix *b)
{
if (a->cols != b->rows)
{
fprintf (stderr,
“Incompatible dimensions for matrices %p and %p.\n”, a, b);
return 0;
} /* end if: checking matrix dimensions */

struct matrix *m = mtx_new (a->rows, b->cols);
size_t row, col, add;

for (row = 0; row rows; row++)
for (col = 0; col rows; col++)
for (add = 0; add rows; add++)
m->data[row][col] += a->data[row][add] * b->data[add][col];

return m;
}
[/code]

The only difference between the code is in the spacing. The line breaks are scarce and willy-nilly. The spacing between variables and operations like multiplication, assignment and comparison are erratic. It’s hard to parse the text and identify what variables are passed to which arguments. Line 11 has the confusion of the comparison operator being attached to the m->rows variable.

The coding style I followed for the second code snippet is (mostly) the GNU Coding Standard. For this project Che and I chose this standard because he was reading through the GNU Lib C Reference Manual at the time, and I wanted to use an established coding style.

I’m not a huge fan of a few things about the style, for instance the space between the function name and the opening parentheses for the arguments still looks weird to me. However, the consistency helps my eye parse through the code to hone in on whatever part I’m looking for, such as the misplaced add in the indexes of the third for loop I had to track down. The extra lines between parts of the code break up the function into for parts: check for the validity, create variables, perform operation and return the result. I thought of them as paragraphs, Che thought of them as stanzas.

Leave a Reply

Your email address will not be published.