The Artima Developer Community
Sponsored Link

Agile Buzz Forum
A Lot Less for a Little More

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
James Robertson

Posts: 29924
Nickname: jarober61
Registered: Jun, 2003

David Buck, Smalltalker at large
A Lot Less for a Little More Posted: Jun 12, 2008 12:16 AM
Reply to this message Reply

This post originated from an RSS feed registered with Agile Buzz by James Robertson.
Original Post: A Lot Less for a Little More
Feed Title: Travis Griggs - Blog
Feed URL: http://www.cincomsmalltalk.com/rssBlog/travis-rss.xml
Feed Description: This TAG Line is Extra
Latest Agile Buzz Posts
Latest Agile Buzz Posts by James Robertson
Latest Posts From Travis Griggs - Blog

Advertisement

Prologue

It all started because I just wanted to play with a two text editor prototype. So I threw the following code (or something close to it, couple superfluous things removed) in a workspace:

window := ApplicationWindow new.

p := Panel new.

p beVerticalSpread.

p add: (TextEditorView model: 'Hello' asValue).

p add: (TextEditorView model: 'Wordl' asValue).

window component: p.

window open

It opened. But sadly, I couldn't enter any text into it. I kinda knew this in the back of my mind. And that there was some more "hooking up" I needed to do. After looking around, I added the following two lines:

window keyboardProcessor: KeyboardProcessor new.

p components do: [:each | window keyboardProcessor addKeyboardReceiver: each].

Someone Else's Wisdom

A while ago, I sat down one day to play with either Haskell or ML, I forget which. Functional Languages had been much talked about at the time quite a bit, so I got out a tutorial, and played around a bit. I didn't last long, so easy to want to learn something and then be distracted by something else. In the process, there was a bit of "philosophy" which has stuck with me to this day. I don't remember it exactly of course, but will paraphrase how I remember it.

This language has no assignment operator. 90% of all program errors are related to state. This language's solution to that is that it has no state. You don't ever store anything. At first, this total eschewing of state may seem a bit extreme. Think of it like a tibetan monk sequestered in a monastery. Going completely without state may seem harsh at first, but once we get rid of all the state in our programs, new avenues open up."

Or something like that. I'm butchering it. But for me, it helped codify something that growing experience had been whispering to me over time. The less state you keep in a program, the better. I'm still happily writing Smalltalk, even after playing the functional game a couple times, but it has very much influenced me to keep a sharp eye out for redundant data, to accept the responsibility of maintaining it when I employ caching, to basically try to have as few instance variables as possible.

KeyboardProcessor: Everyone wants a piece of the family jewels

ApplicationWindow holds a keyboardProcessor (though it's not initialized). An ApplicationWindows eventDispatcher will store one too, it just lazy initializes it from its window though. UIBuilders keep one. Though they keep a window too. And numerous widget controllers have an instance variable for one. With a setter, and lots of direct references. And this means that lots of builder code creates widgets, then hands them a keyboardProcessor:, the same one they're going to hand to the containing window. And lots of one off window construction methods repeat the same. Build the widget tree, then let widgets know what one of the instance variables of their topComponent is. And if you don't do this... the widgets don't respond to keyboard.

I decided to gamble and be courageous. I wondered if I could just turn all those controller keyboardProcessor references into:


self topComponent ifNotNil: [:window | window keyboardProcessor]

I stubbed in these methods on VisualPart/Controller and went to work. It was a common refactoring. Abstract references to the instance variable. Use the rewrite tool to have all callers of keyboardProcessor1 just be keyboardProcessor (from the abstract classes). Remove the local setter/getter. Remove the instance variable. Open a new browser, make sure all is well still.

Results

It worked. It's always so cool to me that I can rewrite things like list views and text editors in the same memory space as the one I'm modifying, and it all keeps working. I don't have a removed method count. There's one less instance variable in probably near 30 classes. And probably 20 or so removed methods. A few rewritten ones. One added one. Lots of methods with one or two lines less code. All that less... and now my original example works. It's so cool when removing stuff makes things work.

Read: A Lot Less for a Little More

Topic: Smalltalk Solutions Daily Update: Automating Builds Previous Topic   Next Topic Topic: Building a Ruby on Rails application in a week

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use