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.

Role Suggesting Instance Variable NameYou need to name Common State.What should you name an instance variable?The two important pieces of information to communicate about any variable are:What is its purpose?How is it used?The purpose or role of a variable is important to the reader because it helps direct their attentionappropriately. Typically when you read code you have a purpose in mind. If you understand therole of a variable and it is unrelated to your purpose, you can quickly skim over irrelevant code thatuses that variable. Likewise, if you see a variable that is related to your purpose you can quicklynarrow your reading to relevant code by looking for that variable.How a variable is used, the messages it is sent, are its “type”. <strong>Smalltalk</strong> doesn’t have declaredtypes, but that doesn’t mean they aren’t important. Understanding the messages sent to a variabletells you what objects can be safely placed as values in that variable. Substitution of objects is theheart of disciplined maintenance and reuse.Different variables appear in different contexts, so what you need to communicate with their namesis different. The context for instance variables is most similar to the context for temporaryvariables. The only way you have for communicating the role of an instance variable is through itsname. If the variables in Point were called “t1” and “t2” instead of “x” and “y”, you’d have a lot ofreading to do before you could tell which was the horizontal component and which the vertical.Naming the variables by their roles gives you that information directly.On the other hand, the type of an instance variable is easily discovered from the code in which itresides. It is easy to find where the variable is used, and from there discover what messages it issent. You also get hints from Creation Parameter Setting Methods or Setting Methods that set thevalue of the variable. If I asked you what the type of “x” was in:Point>>x: aNumberx := aNumberyou’d be able to tell me instantly. In the interest of keeping names simple, short, and readable, youcan safely leave out any mention of the type of a variable in its name.Name instance variables for the role they play in the computation. Make the name plural ifthe variable will hold a Collection.<strong>Coding</strong> <strong>Patterns</strong> page 76 of 147 9/30/2006

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

Saved successfully!

Ooh no, something went wrong!