Laying out code

Piers Cawley

2025 Editor’s Note:
What the hell was I thinking?

A couple of articles back, I reviewed Smalltalk Best Practice Patterns and mentioned that it’s a book that concentrates on the tactics of programming rather than the big strategic stuff. Beck even takes his life in his hands and lays down a set of patterns for laying out code. The (very short) set of patterns he comes up with do seem to generate remarkably clear code.

I’m a big fan of this book, and I’ve lifted many of the ideas about how to structure Smalltalk code and used them when I’m writing OO Perl (to the extent that I sometimes find myself wishing that Perl had Smalltalk like message selectors), so now I’m toying with the idea of using Beck’s rules, or something like them, for laying out my Perl code.

2025 Editor’s Note:
What the hell was I thinking?

A couple of articles back, I reviewed Smalltalk Best Practice Patterns and mentioned that it’s a book that concentrates on the tactics of programming rather than the big strategic stuff. Beck even takes his life in his hands and lays down a set of patterns for laying out code. The (very short) set of patterns he comes up with do seem to generate remarkably clear code.

I’m a big fan of this book, and I’ve lifted many of the ideas about how to structure Smalltalk code and used them when I’m writing OO Perl (to the extent that I sometimes find myself wishing that Perl had Smalltalk like message selectors), so now I’m toying with the idea of using Beck’s rules, or something like them, for laying out my Perl code.

Here’s a method from Pixie::Proxy (one of the scarier classes in Pixie I must admit):

sub _px_extraction_thaw {
  my $self = shift;
  my $pixie = Pixie->get_the_current_pixie;
  my $ret = $pixie->cache_get($self->_oid);
  if ( defined $ret ) { $pixie->obliterate($self); return $ret; }
  $self->px_lock_strategy( $pixie->get_the_current_lock_strategy );
  if ($self->px_class->px_is_immediate) {
      my $oid = $self->_oid;
      $pixie->obliterate($self);
    return $pixie->_get($oid);
  }
  else {
      $self->px_the_store($pixie);
      $pixie->cache_insert($self);
    return $self;
  }
}

Now, here it is again reformatted according to my adaptation of Beck’s rules.

sub _px_extraction_thaw {
  my $self = shift;
  my $pixie = Pixie->get_the_current_pixie;
  my $ret = $pixie->cache_get($self->_oid);
  if ( defined $ret ) { $pixie->obliterate($self); return $ret; }
  $self->px_lock_strategy( $pixie->get_the_current_lock_strategy );
  if ($self->px_class->px_is_immediate)
  { my $oid = $self->_oid;
    $pixie->obliterate($self);
    return $pixie->_get($oid) }
  else
  { $self->px_the_store($pixie);
    $pixie->cache_insert($self);
    return $self; }
}

The main change in appearance comes from using what Beck calls rectangular blocks. Moving to this style has shortened the method by three lines, which isn’t bad. Moving to using cuddled elses

A cuddled else looks like } else { and is evil, bad and wrong.

would only have saved one line and I don’t like them anyway because I tend to lose the else. With rectangular blocks, the control structure is still evident, which is nice. It’s quite strange looking at that code; it looks odd, but I don’t think it looks actively bad. I’m off to recast a whole module in this style and see if I can boil things down to as compact a set of rules as Beck came up with. Or I might decide it looks horrible, but I do think it’s worth a look.

Has anyone else tried anything like this? Did it work for you?

  • 0 likes
  • 0 reposts
  • 0 replies
  • 0 mentions