Changing strings

Ned Konz ned at bike-nomad.com
Sun Apr 21 22:01:14 UTC 2002


On Sunday 21 April 2002 01:33 pm, jennyw wrote:

> a := 'squeak'.
> b:= 'squeal'.
> 'squeak' at: 6 put: $l.
> a == b.
>
> returns false, so I guess it may not be a problem very often.
> Nonetheless, I find it disturbing that if a an instance variable is
> initialized using a string literal, and if a user knows what that
> string literal is, then the user can change the value of that
> instance variable without an accessor.

I don't know what kind of users you have, but mine don't know anything 
about variables.

Now, a programmer using instances of a particular class that returns 
string literals might run into problems with this.

Your solution using copy is, generally, what's done, but mostly it's 
the responsibility of the caller to not clobber the literals.

If your interface is such that you think other objects will be taking 
the return value of a method and then modifying it, you should 
probably use copy.

This is no different, by the way, than returning an Array; a caller 
could modify the contents of the array as well.

Actually, every time you're returning an instance variable directly, 
you're risking this. However, Smalltalk is not generally a paranoid, 
"keep other programmers from touching things" language.

After all, you can always do something like:

theirObject instVarNamed: #seekritVeryPrivateVariable put: 42.

-- 
Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE




More information about the Squeak-dev mailing list