[Newbies] Re: pretty printing

Zulq Alam me at zulq.net
Sun Sep 28 15:48:48 UTC 2008


An awkward pretty print is a good sign you need to simplify. I would use 
another method for the case statement. You couldn't use collect because 
for each character as you are not collecting characters but strings when 
you escape.

Perhaps something like...


escapeString: aString
   ^ String streamContents:
     [:aStream |
     aString do:
       [:eachCharacter |
       aStream nextPutAll: (self escapeCharacter: eachCharacter)]

escapeCharacter: aCharacter
    aCharacter = $< ifTrue: [^ '&lt;'].
    aCharacter = $> ifTrue: [^ '&gt;'].
    aCharacter = $' ifTrue: [^ '&apos;'].
    aCharacter = $& ifTrue: [^ '&amp;'].
    ^ aCharacter asString

I use #streamContents: a lot as #, causes a lot of unnecessary Strings 
to be created and also unnecessary work.

I rarely use the pretty printer but instead aim to keep my methods short 
and simple.

Regards,
Zulq.

Mark Volkmann wrote:
> Here is how one of my methods was pretty printed.
> 
> escape: aString
>     "escapes special characters in XML text"
>     | result |
>     result := ''.
>     aString
>         do: [:char | result := result
>                         , (char caseOf: {
>                                 [$<] -> ['&lt;'].
>                                 [$>] -> ['&gt;'].
>                                 [$'] -> ['&apos;'].
>                                 [$"] -> ['&quot;'].
>                                 [$&] -> ['&amp;']}
>                                  otherwise: [char asString])].
>     ^ result
> 
> Yikes! To my beginner eyes, that doesn't look good. What's the deal with 
> the excessive indentation?
> 
> BTW, comments better ways to do what this code does are welcomed. Could 
> I use collect for this?
> 
> ---
> Mark Volkmann



More information about the Beginners mailing list