Skip to main content
Engineering LibreTexts

3.3: Defining the Class LOCell

  • Page ID
    39579
    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    At this point there are, of course, no classes in the new package. However, the main editing pane displays a template to make it easy to create a new class (see Figure 3.2.2).

    This template shows us a Pharo expression that sends a message to a class called Object, asking it to create a subclass called NameOfSubClass. The new class has no variables, and should belong to the category (package) PBE-LightsOut.

    Creating a new class

    We simply edit the template to create the class that we really want. Modify the class creation template as follows:

    • Replace Object with SimpleSwitchMorph.
    • Replace NameOfSubClass with LOCell.
    • Add mouseAction to the list of instance variables.

    You should get the following class definition:

    SimpleSwitchMorph subclass: #LOCell
        instanceVariableNames: 'mouseAction'
        classVariableNames: ''
        package: 'PBE-LightsOut'
    

    This new definition consists of a Pharo expression that sends a message to the existing class SimpleSwitchMorph, asking it to create a subclass called LOCell. (Actually, since LOCell does not exist yet, we passed the symbol #LOCell as an argument, representing the name of the class to create.) We also tell it that instances of the new class should have a mouseAction instance variable, which we will use to define what action the cell should take if the mouse should click on it.

    At this point you still have not created anything. Note that the top right of the panel changed to orange. This means that there are unsaved changes. To actually send this subclass message, you must save (accept) the source code. Either right-click and select Accept, or use the shortcut CMD-s (for ”Save”). The message will be sent to SimpleSwitchMorph, which will cause the new class to be compiled. You should get the situation depicted in Figure \(\PageIndex{1}\).

    Once the class definition is accepted, the class is created and appears in the class pane of the browser (see Figure \(\PageIndex{1}\)). The editing pane now shows the class definition. Below you get the Quality Assistant’s feedback: It runs automatically quality rules on your code and reports them.

    The class LOCell.
    Figure \(\PageIndex{1}\): The newly-created class LOCell.

    About comments

    Pharoers put a very high value on the readability of their code, but also good quality comments.

    Method comments. People have the tendency to believe that it is not necessary to comment well written methods: it is plain wrong and encourages sloppiness. Of course, bad code should renamed and refactored. Obviously commenting trivial methods makes no sense. A comment should not be the code written in english but an explanation of what the method is doing, its context, or the rationale behind its implementation. When reading a comment, the reader should be comforted that his hypotheses are correct.

    Class comments. For the class comment, the Pharo class comment template gives a good idea of a strong class comment. Read it! It is based on CRC for Class Responsibility Collaborators. So in a nutshell the comments state the responsibility of the class in a couple of sentences and how it collaborates with other classes to achieve this responsibilities. In addition we can state the API (main messages an object understands), give an example (usually in Pharo we define examples as class methods), and some details about internal representation or implementation rationale.

    Select the comment button and define a class comment following this template.

    On categories vs. packages

    Historically, Pharo packages were implemented as ”categories” (a group of classes). With the newer versions of Pharo, the term category is being deprecated, and replaced exclusively by package.

    If you use an older version of Pharo or an old tutorial, the class template will be as follow:

    SimpleSwitchMorph subclass: #LOCell
        instanceVariableNames: 'mouseAction'
        classVariableNames: ''
        category: 'PBE-LightsOut'
    

    It is equivalent to the one we mentioned earlier. In this book we only use the term package. The Pharo package is also what you will be using to version your source code using the Monticello versioning tool.


    This page titled 3.3: Defining the Class LOCell is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.