Unlocking Innovation: How Pharo Rewrites Live Coding Rules

Written by

in

Building Dynamic Apps: A Practical Guide to Pharo Pharo is a modern, open-source Smalltalk environment that flips traditional software development on its head. In Pharo, everything is an object, and the code lives inside the runtime environment. There are no static files, no compilation pauses, and no boundaries between your editor and your running application. This guide will walk you through building dynamic applications using Pharo’s unique live-coding ecosystem. The Pharo Paradigm: Live Coding

Traditional programming uses a strict cycle: write code, compile, run, and debug. Pharo replaces this linear loop with a continuous live environment. The Living Image

When you program in Pharo, you work inside an “image.” The image contains all the objects, classes, and running state of your application. You do not write code in a separate text editor; you modify the live objects directly. If you change a method while a loop is running, the loop immediately begins using the new logic. Explanatory Debugging

In Pharo, the debugger is a primary development tool, not a safety net for errors. You can write a test, run it, let it fail, and use the debugger to write the missing methods on the fly. The execution resumes seamlessly from the point of failure. Setting Up Your Workspace

To build applications, you need the right tools inside your Pharo image. 1. Download Pharo Launcher

The Pharo Launcher is the official tool to manage your images and virtual machines. Download it for your operating system and create a fresh image based on the latest stable Pharo release. 2. Master the Tools Open your new image to find the essential windows:

System Browser: Your central hub to create packages, classes, and methods.

Playground: A scratchpad to write and execute snippets of code instantly using shortcuts like Cmd + d (Do it) or Cmd + p (Print it).

Transcript: A standard output window used to log messages during development. Creating Your First App: A Task Tracker

Let us build a simple, dynamic model for a task management application to see Pharo syntax and object creation in action. Step 1: Create a Package and Class

Open the System Browser, right-click the package pane, and create a new package named TaskTracker.

Select your new package and define a class by modifying the template in the bottom pane:

Object subclass: #TodoTask instanceVariableNames: ‘title isCompleted’ classVariableNames: “ package: ‘TaskTracker’ Use code with caution. Press Cmd + s to save and compile the class instantly. Step 2: Define Initialization and Accessors

Right-click your new class and select Refactoring -> Generate Accessors to automatically create getter and setter methods for title and isCompleted.

Next, create an initialization method to give new tasks a default state. Click on the initialization protocol and add this method:

initialize super initialize. title := ‘Untitled Task’. isCompleted := false. Use code with caution. Step 3: Add Dynamic Behavior Let us add a method to toggle the status of a task: toggleStatus isCompleted := isCompleted not. Use code with caution. Test your new class inside the Playground:

task := TodoTask new. task title: ‘Learn Pharo Live Coding’. task toggleStatus. task isCompleted. “Inspecting this will return true” Use code with caution.

Select the code, right-click, and choose Inspect. A live inspector window opens, showing you the real-world instance of your object and its internal state. Going Visual: Building a User Interface

Pharo features powerful UI frameworks like Spec2 and Bloc to turn your backend objects into interactive desktop or web apps. Quick Desktop UI with Spec2

Spec2 allows you to build user interfaces declaratively. Here is how you can create a simple window to view your tasks:

SpPresenter subclass: #TodoPresenter instanceVariableNames: ‘taskList’ package: ‘TaskTracker’ Use code with caution.

Define the layout and widgets by creating the following methods:

initializePresenters taskList := self newList. taskList items: #( ‘Learn Pharo’ ‘Build UI’ ‘Deploy App’ ). initializeWindow: aWindowPresenter aWindowPresenter title: ‘My Pharo Tasks’; initialExtent: 400 @ 300. Use code with caution.

Run TodoPresenter new open in your Playground. A fully native, interactive window opens immediately. Best Practices for Pharo Developers

Test-Driven Development (TDD): Use SUnit, Pharo’s built-in testing framework. Write your tests first, execute them, and use the debugger to generate the production code as you go.

Version Control with Iceberg: Pharo includes Iceberg, a tool that seamlessly maps the internal image classes to readable Git repositories on GitHub or GitLab. Commit often.

Save the Image: Think of your image like a virtual machine state. Save it frequently to preserve your exact workspace layout, open windows, and running objects. Conclusion

Pharo offers an unparalleled level of feedback and agility. By removing the barrier between writing code and running code, it changes how you think about software architecture. Start inspecting objects, writing code in the debugger, and let the live environment accelerate your development flow.

To help expand your Pharo project, let me know if you want to explore:

Connecting your application to a database (like Voyage or SQLite)

Turning this into a web application using the Seaside framework

Deepening your understanding of Smalltalk syntax and message passing

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *