Interesting Code Transformations

Here is a typical piece of ‘C’ code, part of a sha256 digest routine.

void sha256_next (SHA256 *sha, uchar *what, int len)
{
	while( len-- ) {
		sha->length++;
		sha->buff[sha->next] = *what++;
		if( ++sha->next == 512/8 )
			sha256_digest (sha);
	}
}

When I look at a piece of code like this, I often feel dread, because it is chock full of idioms that the maintainer, or translator, of such code might not fully understand. So, when it comes time to maintaining it, bugs are introduced because those idioms get broken.

Here’s a more explicit version of the same:

static const int maxperbuff = 512/8;  // 64 bytes

void sha256_next (SHA256 *sha, uchar *what, int len)
{
    while( len > 0 ) 
    {
      sha->length = sha->length + 1;
      sha->buff[sha->next] = *what;  // assign current value of pointer
      what = what + 1;               // advance pointer
      sha->next = sha->next + 1;
      if( sha->next == maxperbuff )
      {
        sha256_digest (sha);
      }

      len = len - 1;
    }
}

That’s a little bit more verbose, but to my eyes a bit easier to understand and maintain. Things are a little more explicit. The ‘++’ and ‘–‘ are dealt with explicitly. The bracketing of the ‘if’ statement is a little less error prone. The *what++ is more explicit, separating the dereference from the increment, which is always a cause of concern for me.

Once it’s in this form, which doesn’t use so much operator trickery, it’s a bit easier to translate to other forms. Here’s the Lua version:

local maxperbuff = 512/8;  -- 64 bytes

function sha256_next(sha, what, len)
    local offset=0;

    while( len > 0) do
      sha.length = sha.length + 1;
      sha.buff[sha.next] = what[offset];
      offset = offset+1;
      sha.next = sha.next + 1;
      if( sha.next == maxperbuff ) then
        sha:digest();
      end
      len = len - 1
    end
end

For the most part, remove the opening ‘{‘, change the closing ‘}’ to ‘end’, and remove all the type declaration stuff. Change ‘->’ to ‘.’, and you’re pretty much done. As an added bonuse, change from pointer arithmetic, to straight array indexing, and you’re all set.

The lesson for me here is not that one language is better/worse than the other, rather that when I program in a fairly straight forward manner, without relying on time honored short cuts, my code becomes clearer, and easier to maintain. It’s irrespective of the language actually being used. In fact, once you start coding simply, it’s easier to translate between the various languages, because you’re not relying on anything special.

It used to be that those special little language features were highly beneficial. Doing a ‘++’ operation might translate to a single machine instruction. These days, doing var = var + 1, might also translate into a single machine instruction. But, even if it doesn’t, it’s probably not my performance bottleneck, so I will err on the side of readability instead.

And so it goes. The more I code, the less I want to maintain my code, so the simpler I make it.

Advertisements

One Comment on “Interesting Code Transformations”

  1. I am not much of a coder but it is a part of my work and one thing I am definite about is the need to think long term. That is exactly where you are. Keep spreading the word.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s