Search code examples
smalltalkpharo

What is the difference between classVariableNames: '' and instanceVariableNames: '' when setting up a Singleton in Pharo Smalltalk?


One is located on the instance side:

Object subclass: #MyClass
    instanceVariableNames: ''
    classVariableNames: ''
    category: 'MyApp'

The other accessible on the class side:

MyClass class
    instanceVariableNames: ''

Solution

  • Here goes, I found bits of information here and there.

    Managed to find a good explanation here, pasted in a few lines for reference purposes. People should read the entire column. http://esug.org/data/Articles/Columns/EwingPapers/cvars&cinst_vars.pdf

    Classes that use class variables can be made more reusable with a few coding conventions. These coding conventions make it easier to create subclasses. Sometimes developers use class variables inappropriately. Inappropriate use of class variables results in classes that are difficult to subclass. Often, the better implementation choice for a particular problem is a class instance variable instead of a class variable.

    What are class variables? Classes can have

    • class variables, and

    • class instances variables.

    Class variables are referenced from instance and class methods by referring to the name of the class variable. Any method, either a class method or an instance method can reference a class variable.