How to name instance variables 
Author Message
 How to name instance variables

I  recently ran into this problem on Dolphin 4.
I wrote a class-method which contained  the  block

  [:name |   ... ].

The compiler complained:
  "Can't redefine instance variable 'name'."

Turns out the class Class has the instance variable 'name'.
Therefore I can't use a temporary variable called 'name'
in *any of my class methods*.

Therefore I think, instance variables should be named in
a fashion that makes them hard to use accidentally, by
adding a sufffix or prefix to them .  In the above case
I hope there would have been an instance variable called
'name_ivar' or something in the class Class.

I'm afraid something would seriously break if I tried
to rename the instance variable 'name' of Class to
something else.

Any thoughts, ideas ?

Thanks
-Panu Viljamaa



Thu, 13 Oct 2005 02:31:07 GMT  
 How to name instance variables


Quote:
>I  recently ran into this problem on Dolphin 4.
>I wrote a class-method which contained  the  block

>  [:name |   ... ].

>The compiler complained:
>  "Can't redefine instance variable 'name'."

>Turns out the class Class has the instance variable 'name'.
>Therefore I can't use a temporary variable called 'name'
>in *any of my class methods*.

>Therefore I think, instance variables should be named in
>a fashion that makes them hard to use accidentally, by
>adding a sufffix or prefix to them .  In the above case
>I hope there would have been an instance variable called
>'name_ivar' or something in the class Class.

>I'm afraid something would seriously break if I tried
>to rename the instance variable 'name' of Class to
>something else.

>Any thoughts, ideas ?

Raming a class variable because it conflicts with a local variable
name you want to use is...not very wise.

Just use something else for your block argument name; it's its
ambigious, use 'aName' or 'theName'.  If it's specific, use 'carName'
or 'personName' or whataver it is you are using the name of.

Take care,
Jay



Thu, 13 Oct 2005 02:25:39 GMT  
 How to name instance variables
When I'm writing C or C++ or Java (most of the time, unfortunately)
I practice a comprehensive Hungarian naming scheme that really pays off.
I tried that for a while in Smalltalk,
but very little of it really paid off as it does elsewhere.
One aspect that persisted was beginning argument names with a or an.
It would be nice to look at a name and know if it was a temp or instvar.
I can do this in my C/C++/Java, but don't do it in Smalltalk.

I can understand your compiler *warning* you about this,
but forbidding it seems like the wrong thing to do.
The usual scoping rules make it clear what is meant.

Steve
--
Steven T Abell
Software Designer
http://www.brising.com

In software, nothing is more concrete than a good abstraction.

Quote:

> I  recently ran into this problem on Dolphin 4.
> I wrote a class-method which contained  the  block

>   [:name |   ... ].

> The compiler complained:
>   "Can't redefine instance variable 'name'."

> Turns out the class Class has the instance variable 'name'.
> Therefore I can't use a temporary variable called 'name'
> in *any of my class methods*.

> Therefore I think, instance variables should be named in
> a fashion that makes them hard to use accidentally, by
> adding a sufffix or prefix to them .  In the above case
> I hope there would have been an instance variable called
> 'name_ivar' or something in the class Class.

> I'm afraid something would seriously break if I tried
> to rename the instance variable 'name' of Class to
> something else.

> Any thoughts, ideas ?

> Thanks
> -Panu Viljamaa



Thu, 13 Oct 2005 03:12:16 GMT  
 How to name instance variables
panu,

Quote:
> I  recently ran into this problem on Dolphin 4.
> I wrote a class-method which contained  the  block

>   [:name |   ... ].

> The compiler complained:
>   "Can't redefine instance variable 'name'."
> Turns out the class Class has the instance variable 'name'.
> Therefore I can't use a temporary variable called 'name'
> in *any of my class methods*.
> Therefore I think, instance variables should be named in
> a fashion that makes them hard to use accidentally, by
> adding a sufffix or prefix to them .
> Any thoughts, ideas ?

I like to think of things the other way around.  Why not come up with an
approach to name local variables so they will never conflict with class,
class instance or instance variables?

I normally use the "a" and "an" articles to distinguish local variables.
The resulting name of a local variable never conflicts and it is easy to
tell when a variable is local to the method.

Regards,

Randy



Thu, 13 Oct 2005 06:11:00 GMT  
 How to name instance variables

Quote:
>I like to think of things the other way around.  Why not come up with an
>approach to name local variables so they will never conflict with class,
>class instance or instance variables?

>I normally use the "a" and "an" articles to distinguish local variables.
>The resulting name of a local variable never conflicts and it is easy to
>tell when a variable is local to the method.

Sure..  Or you can do it like [ :eachName | ]

I often use "each" for blocks and "a" or "an" for arguments.

Ian

Quote:
>Regards,

>Randy

---
http://www.upright.net/ian/


Thu, 13 Oct 2005 06:48:05 GMT  
 How to name instance variables


Quote:
> When I'm writing C or C++ or Java (most of the time, unfortunately)
> I practice a comprehensive Hungarian naming scheme that really pays off.
> I tried that for a while in Smalltalk,
> but very little of it really paid off as it does elsewhere.
> One aspect that persisted was beginning argument names with a or an.
> It would be nice to look at a name and know if it was a temp or instvar.
> I can do this in my C/C++/Java, but don't do it in Smalltalk.

> I can understand your compiler *warning* you about this,
> but forbidding it seems like the wrong thing to do.
> The usual scoping rules make it clear what is meant.

I'm with Steve here. The problem is with the compiler, not with your way of
naming entities.

Regards,

Peter van Rooijen

Quote:
> Steve
> --
> Steven T Abell
> Software Designer
> http://www.brising.com

> In software, nothing is more concrete than a good abstraction.


> > I  recently ran into this problem on Dolphin 4.
> > I wrote a class-method which contained  the  block

> >   [:name |   ... ].

> > The compiler complained:
> >   "Can't redefine instance variable 'name'."

> > Turns out the class Class has the instance variable 'name'.
> > Therefore I can't use a temporary variable called 'name'
> > in *any of my class methods*.

> > Therefore I think, instance variables should be named in
> > a fashion that makes them hard to use accidentally, by
> > adding a sufffix or prefix to them .  In the above case
> > I hope there would have been an instance variable called
> > 'name_ivar' or something in the class Class.

> > I'm afraid something would seriously break if I tried
> > to rename the instance variable 'name' of Class to
> > something else.

> > Any thoughts, ideas ?

> > Thanks
> > -Panu Viljamaa



Thu, 13 Oct 2005 14:19:39 GMT  
 How to name instance variables

Quote:

> I  recently ran into this problem on Dolphin 4.
> I wrote a class-method which contained  the  block

>   [:name |   ... ].

Compiles OK (with a warning) in Dolphin 5.

    -- chris



Thu, 13 Oct 2005 16:13:36 GMT  
 How to name instance variables

Quote:


>> I  recently ran into this problem on Dolphin 4.
>> I wrote a class-method which contained  the  block

>>   [:name |   ... ].

>Compiles OK (with a warning) in Dolphin 5.

>    -- chris

Yes, and not surprisingly, fine in S# as well.  However, probably not a best
practice pattern as you may want to use the instance variable inside the
block.

Ian

---
http://www.upright.net/ian/



Fri, 14 Oct 2005 03:06:27 GMT  
 How to name instance variables

Quote:

> When I'm writing C or C++ or Java (most of the time, unfortunately) I
> practice a comprehensive Hungarian naming scheme that really pays off. I
> tried that for a while in Smalltalk, but very little of it really paid
> off as it does elsewhere.

Two things that fight against you on this in Smalltalk is

a) There's very little good in specifying something as int versus floar
in Smalltalk.  Ninety nine times out of a hunderd, just Number will do.
So the disctinction of a  lpzcstr is sorta lost in Smalltalk, for
example.

b) Smalltalk is more behavior oriented than type oriented.  You really
don't care what the thing is, as long as it has the required behavior.
"Program to the interface" versus "Program to the class"

Take care,
Jay



Sat, 15 Oct 2005 02:53:08 GMT  
 How to name instance variables

Quote:

> ...I normally use the "a" and "an" articles to distinguish local
> variables.The resulting name of a local variable never conflicts and
> it is easy totell when a variable is local to the method.

I use 'a' , 'an'  with arguments since these prefixes naturally come
before a type-name  ('aString' etc.).  For local variables I don't
really care (nor does anyone using my method) what their type
is. I therefore name them as 'roles'. For instance 'name'.

For instance variables I use a longish suffix like '_ivar'.
The longevity is not a problem since I always try to use
accessor methods, and this naming convention makes it almost
necessary to do so.

Thanks for comments and answers
-Panu Viljamaa

Quote:

>Regards,

>Randy



Sat, 15 Oct 2005 11:20:50 GMT  
 How to name instance variables

Quote:


>>>  [:name |   ... ].

>>Compiles OK (with a warning) in Dolphin 5.

>Yes, and not surprisingly, fine in S# as well.  However, probably not a best practice pattern as you may want to use the instance variable inside the block.

As you say  within the block you can't access
the instance variable. Had the instance variable
been named in a specific way reserved to instance
variables  in general,  I would be free to use 'name'
as the block argument and still access the instance
variable too.

(I guess I should upgrade to Dolphin5 then)

But I assume even D5 prevents you from using a
temporary variable named 'name' in any of its
class methods (?). It seems to me this is an
unnecessary constraint on the programmer.

In short I'm thinking that instance variables
should  have their own 'namespace' determined
by some syntactic means. Just like a class name
must always start uppercase, we could require that
an instance variable always has a suffix like
'ivar' or something.

Instance variable naming is critical because for
one thing you can't rename them in subclasses.

Thanks

-Panu Viljamaa



Sat, 15 Oct 2005 11:32:57 GMT  
 How to name instance variables

Quote:


> >>>  [:name |   ... ].

> >>Compiles OK (with a warning) in Dolphin 5.

>...
> But I assume even D5 prevents you from using a
> temporary variable named 'name' in any of its
> class methods (?). ...

No, the same applies i.e. it is just a compiler warning which you can choose
to ignore if you wish.

Regards

Blair



Sun, 16 Oct 2005 00:58:51 GMT  
 How to name instance variables

Quote:

>As you say  within the block you can't access
>the instance variable. Had the instance variable
>been named in a specific way reserved to instance
>variables  in general,  I would be free to use 'name'
>as the block argument and still access the instance
>variable too.

I disagree.  Names of instance variables are usually fairly simple.  Names
of temporary variables often have a purpose for something.  I often try to
describe the intent and purpose of the temporary variable, and combine that
idea into the name of the temporary, so that it is appropriately named.  If
all else fails, we can always name it tempName or such.  I really don't
think prefixing or postfixing instance variables makes for good readable
code.

Ian

---
http://www.upright.net/ian/



Sun, 16 Oct 2005 01:25:21 GMT  
 How to name instance variables
I must agree.
Your (panu's) suggestion is somewhat the other way round.
An instance variable is something much more general than a local block
variable (or parameter). The best way to avoid naming conflicts is to
name the very specific local variable according to its intention. This
also makes the code much better readable.

It surely wouldn't help if we changed the best practices in such a
scheme simply to avoid naming conflicts between instance variables and
variables that have a very short life cycle. There are enough languages
out there which contain such weird design decisions...

Joe

Ian Upright schrieb:

Quote:

>>As you say  within the block you can't access
>>the instance variable. Had the instance variable
>>been named in a specific way reserved to instance
>>variables  in general,  I would be free to use 'name'
>>as the block argument and still access the instance
>>variable too.

> I disagree.  Names of instance variables are usually fairly simple.  Names
> of temporary variables often have a purpose for something.  I often try to
> describe the intent and purpose of the temporary variable, and combine that
> idea into the name of the temporary, so that it is appropriately named.  If
> all else fails, we can always name it tempName or such.  I really don't
> think prefixing or postfixing instance variables makes for good readable
> code.



Sun, 16 Oct 2005 02:27:15 GMT  
 How to name instance variables

Quote:

> ...  I really don't think prefixing or postfixing instance variables
> makes for good readable code.

This depends on how *often* you use instance variables.

If you use accessor methods systematically, each
instance variable is referred to in only one or two
methods - getter and setter - which are so simple
that readability due  to a longer variable name is
a non-issue.

(Longer instance-variable names in fact encourage
us to write a shorter  accessor for them)

On the other hand I've more than once stumbled upon
the bug where I thought I was using a local variable,
but it turned out to be an instance variable instead.

My point is that it's helpful if you can tell the
type of the variable you are dealing with from its
form. For instance, you already can distinguish
Global variables from other variables, simply
because they start capitalized.

I understand that many of us find prefixes and
suffixes un-esthetical. So here's one more
alternative solution:

Name instance variables so that their last character
is capitalized - the opposite of globals.

-Panu Viljamaa



Sun, 16 Oct 2005 08:09:35 GMT  
 
 [ 45 post ]  Go to page: [1] [2] [3] [4]

 Relevant Pages 

1. instance variables by name?

2. Variable component and instance names

3. Using a variable name for a variable name??

4. Instances incorrectly sharing instance variables?

5. field name and key name as a variable

6. field name and key name as a variable

7. instances of classes and instances of instances

8. variable contents to variable name

9. array name in variable variable

10. getting variable name from the variable itself?

11. Variables containing variable names

12. name of a variable stored in another variable

 

 
Powered by phpBB® Forum Software