Skip to main content
Engineering LibreTexts

2.08: Let’s Try Our Code

  • Page ID
    37591
  • \( \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}}\)

    That’s it: the Quinto game is complete!

    If you have followed all of the steps, you should be able to play the game, consisting of just 2 classes and 7 methods.

    \(\bigstar\) In a workspace, type SBEGame new openInWorld and do it.

    The game will open, and you should be able to click on the cells and see how it works.

    Well, so much for theory. . . When you click on a cell, a notifier window called the PreDebugWindow window appears with an error message! As depicted in Figure \(\PageIndex{1}\), it says MessageNotUnderstood: SBEGame\(\gg\)toggleState.

    There is a bug in our game when a cell is clicked!
    Figure \(\PageIndex{1}\): There is a bug in our game when a cell is clicked!

    What happened? To find out, let’s use one of Smalltalk’s more powerful tools: the debugger.

    \(\bigstar\) Click on the debug button in the notifer window.

    The debugger will appear. In the upper part of the debugger window you can see the execution stack, showing all the active methods; selecting any one of them will show, in the middle pane, the Smalltalk code being executed in that method, with the part that triggered the error highlighted.

    \(\bigstar\) Click on the line labelled SBEGame\(\gg\)toggleNeighboursOfCellAt:at: (near the top).

    The debugger will show you the execution context within this method where the error occurred (Figure \(\PageIndex{2}\)).

    At the bottom of the debugger are two small inspector windows. On the left, you can inspect the object that is the receiver of the message that caused the selected method to execute, so you can look here to see the values of the instance variables. On the right you can inspect an object that represents the currently executing method itself, so you can look here to see the values of the method’s parameters and temporary variables.

    Using the debugger, you can execute code step by step, inspect objects in parameters and local variables, evaluate code just as you can in a workspace, and, most surprisingly to those used to other debuggers, change the code while it is being debugged! Some Smalltalkers program in the debugger almost all the time, rather than in the browser. The advantage of this is that you see the method that you are writing as it will be executed, with real parameters in the actual execution context.

    The debugger.
    Figure \(\PageIndex{2}\): The debugger, with the method toggleNeighboursOfCell:at: selected.

    In this case we can see in the first line of the top panel that the toggleState message has been sent to an instance of SBEGame, while it should clearly have been an instance of SBECell. The problem is most likely with the initialization of the cells matrix. Browsing the code of SBEGame\(\gg\)initialize shows that cells is filled with the return values of newCellAt:at:, but when we look at that method, we see that there is no return statement there! By default, a method returns self, which in the case of newCellAt:at: is indeed an instance of SBEGame.

    \(\bigstar\) Close the debugger window. Add the expression “\(\uparrow\) c” to the end of the method SBEGame\(\gg\)newCellAt:at: so that it returns c. (See Code \(\PageIndex{1}\).)

    Code \(\PageIndex{1}\) (Squeak): Fixing The Bug

    SBEGame»newCellAt: i at: j
        "Create a cell for position (i,j) and add it to my on--screen
        representation at the appropriate screen position. Answer the new cell"
        | c origin |
        c := SBECell new.
        origin := self innerBounds origin.
        self addMorph: c.
        c position: ((i -- 1) * c width) @ ((j -- 1) * c height) + origin.
        c mouseAction: [self toggleNeighboursOfCellAt: i at: j].
        ↑c
    

    Recall from Chapter 1 that the construct to return a value from a method in Smalltalk is \(\uparrow\), which you obtain by typing ^.

    Often, you can fix the code directly in the debugger window and click Proceed to continue running the application. In our case, because the bug was in the initialization of an object, rather than in the method that failed, the easiest thing to do is to close the debugger window, destroy the running instance of the game (with the halo), and create a new one.

    \(\bigstar\) Do: SBEGame new openInWorld again.

    Now the game should work properly.


    This page titled 2.08: Let’s Try Our Code is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by Andrew P. Black, Stéphane Ducasse, Oscar Nierstrasz, Damien Pollet via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.