13.07.2015 Views

Smalltalk Best Practice Patterns Volume 1: Coding - Free

Smalltalk Best Practice Patterns Volume 1: Coding - Free

Smalltalk Best Practice Patterns Volume 1: Coding - Free

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

FormattingNo other topic generates more heat and less light than code formatting. Everybody has their ownstyle, and attempts to impose another style are met with ferocious resistance. So, why am I willingsticking my head into this buzz saw?The first reason is “because it is there.” I wanted to push patterns to their limit, to see how wellthey apply to a detail-oriented, exception-ridden, and emotion-filled topic. I wrote these patternsover the course of a couple of months. As new special cases came up, I either had to modify thepatterns, add a new pattern, or format my code according to the existing patterns. Before long, I nolonger was finding cases where I had to change the patterns. I am quite pleased that all offormatting in <strong>Smalltalk</strong> fits into ten patterns.The second reason is “because it is important.” Not necessarily formatting code according to thesepatterns, but formatting them according to some set of consistent rules gives a team a smoothnessto their interaction. If everybody formats the same way, then reviews and code transfer are neverdelayed while someone “cleans up” the code. Done right, formatting can convey a lot ofinformation about the structure of code at a glance.The third reason is to advance the discussion of formatting. Stating the rules as patterns makes mygoals and tradeoffs explicit. If you have a different style, you can use these patterns as an exampleto cast your own rules as patterns. Then you can compare, explicitly, what problems each set ofpatterns solves and what problems each ignores.The priorities of these patterns are:1. To make the gross structure of the method apparent at a glance. Complex messages and blocks,in particular, should jump out at the reader.2. To preserve vertical space. There is a huge difference between reading a method that fits intothe text pane of a browser and reading one that forces you to scroll. Keeping methods compactvertically lets you have smaller browsers and still be able to read methods without scrolling.This reduces window management overhead and leaves more screen space for otherprogramming tools.There are many styles of <strong>Smalltalk</strong> coding for which these formatting patterns would be a disaster.Long methods and methods that use complex expressions look terrible formatted this way.However, thorough use of Composed Method and Explaining Temporary Variable, along with theattitude that you are writing for a reader and not the computer, will go a long way towards helpingyou produce code that is simple to format and simple to read.<strong>Coding</strong> <strong>Patterns</strong> page 126 of 147 9/30/2006

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!