Managing Code in Scratch

Managing Code

One important soft-skill that is worth mentioning is understanding the importance of managing code in Scratch projects. Building good habits with this from the beginning will save lots of time and effort as you begin to scale to larger projects. Your code can become cluttered or unorganized because of the screen real estate occupied by Scratch’s blocks.

While the methods outlined are specific for Scratch, the concept behind them is not. When working with any programming language or environment, especially when managing multiple project files, you’re going to need to have a method for organizing your code. Practicing this skill now in Scratch will ensure that you build a solid foundation and develop sound organizational skills when coding.

Blocks and Scripts

Now that you’ve created the motion script, you’ll notice that you had to combine the different blocks together into a script. In Scratch, a script is a combination of at least two blocks that connect with each other and begin with a Hat Block. A Hat Block is another name for the Event blocks due to their curved top border. You can’t add any other blocks above an Event block. These blocks are Hat Blocks because they’re the topmost block in a script. You can have multiple scripts triggered by multiple events, but each one would have its own Hat.

The connection order of the blocks is crucial, as Scratch executes code from the top down. Additionally, you’ve likely noticed that the blocks have different shapes associated with them. This is an inventive way that Scratch provides a safeguard from logical fallacies in Scratch scripts. Logically, it makes sense that scripts begin with an event block as there is a necessity for something (whether an event or input) to cause the script to trigger. Encouraging youth to be mindful of these connections is helpful as it will work to build recognition and internalization of programming logic.

It’s important to encourage youth to develop a personal Scratch workflow as far as how they structure and build out their projects. There are some tricks that can be used to streamline this process, but for the most part you want them to arrive at their own methods as long as they’re in accord with best practices and build good habits.

The Backpack

The backpack is a new feature in Scratch 2.0 (the version that you’re using) that doesn’t get as much attention as it should. The backpack is a storage dock associated with each user account for storing assets, including code, across projects. This feature facilitates code organization and management.

The backpack is located in its own window below the editor and is collapsable. You can maximize the window when you want to drag and drop code to/from it.

Benefits of the Backpack

One of the key benefits of using the backpack is that it provides an easy to access code manager tool within Scratch. Once you begin to develop a collection of game mechanics that you use across projects, this benefit will quickly stand out as an enormous efficiency boost.

Here is a quick screenshot of a backpack containing scripts:

IntGames - Backpack Closeup

Each of these scripts was dragged from a project and can be continually reused in new projects. To add assets to your backpack, just expand the backpack and then drag and drop the asset into the window. To use code stored in the backpack just drag it into from the backpack to the code editor.

Backpacks can also store media assets such as images and sounds as well. This is useful if you have a particular sound effect or object that you like to use across projects.

Additional Tips and Tricks

One thing to be aware of with Scratch is that there isn’t a reliable undo button. You may find that you accidentally delete an important script. You can use the backpack as a pseudo backup system for really important code. This also allows for some amount of version control.