Coding Like a Girl
Beautiful code, test-first - Signal vs. Noise (by 37signals):
"I was talking with a friend recently and discovered we have a failing in common: we both have a tendency towards a peculiar form of perfectionism. He was telling me how he feels like he has to sit and tweak his code over and over until it not only acts right, but looks right. It cannot be merely functional, it must be beautiful, as well."
(Via Code Like a Girl.)
While programming in Squeak, I find that I am compulsive about making the code beautiful. And the Smalltalk language makes that really possible because of its syntax. Also, reading the book Smalltalk: Best Practice Patterns helps too. And I do not mean doing ridiculous stuff like making beautiful function headers. I mean making Smalltalk code look like it should: short, simple and human readable.
I am not an expert at this, but from looking at the source code and reading the book, here are some of the things that I notice.
- Minimal comments
Some people like having comments there. I agree that sometimes comments are necessary but with code like Smalltalk or Ruby, having comments would easily make the ratio of comments to code increase dramatically.
Bin >> run
"Tell my station to process me."
self station process: self
- Put methods in the right protocols
I am not sure why other languages do not have this facility, but you can group methods in Smalltalk into protocols. This makes it really easy to see those methods that you need. For instance, want to find out how to create the object, look at the methods in theinstance creation
protocol. Want to convert from objectA to objectB? Look into theconverting
protocol. Grouping methods into the proper protocols really makes it easier to find those that you need. - Use polymorphism instead of compare statements
Smalltalk did away with long if...else and case statements. And because of that , it forces you to think in a more dynamic sense as well. - Short methods
Most methods in Smalltalk should be about 3 - 4 lines of code. And this is actually enforced by the way that methods are being displayed in the code browser. Unlike traditional source code viewers, in Squeak, you can only view one method in one window at any time. Of course you are free to open multiple windows, but each window can only show one method at a time. And the size of the pane for showing the method is not that big either. So it forces you to write short methods since it is really hard for you to keep track of stuff otherwise. - Using collections
Smalltalk provides a rich set of collections: Arrays, Dictionaries, OrderedCollections, SortedCollections, Sets, etc. In fact because these collections are so fundamental, they are being used everywhere. It is completely nonsensical to cook up your own data structure when there are so many provided for you. - Readable code
chatSession sendMessage: 'Hello there!' to: '[email protected]'
You understand what it means instantly because it reads correctly. Not many languages out there have the sense to make their parameters key-word based.
The point being that code that is not following those standards feel really, really bad and sloppy. It is almost like leaving spaghetti stains on your white shirt. Sure no one would care about it since you are still clothed and all, but it just becomes an eye sore.
Regardless of the programming language, one should always stick with the proper convention of doing stuff. Program in C, stick to the C convention. Program in Java, stick to the Java style.
Tweetcomments powered by Disqus