Emacs Extensions for
GE Smallworld Magik Development
Screen-casts, tutorials, and productivity tools for Magik programmers who use Emacs. From tab-mode and code folding to the Magik Debugger and object inspector — explore features built by a developer, for developers.
Learn MoreNine screen-casts published between November 2010 and January 2011 — covering everything from ECB mode to the Tree Item GUI control. Read the story behind the project →
Get in Touch
A Practical Magik Folding Strategy for Large Methods
Large Magik methods become difficult to maintain long before they become impossible to run. A method that mixes database queries, collection processing, user-interface updates and error handling can stretch across hundreds of lines, making navigation slower and debugging less focused.
Code folding gives developers a temporary view of the method’s structure without changing the source. The aim is not to hide complexity permanently. It is to create a reliable working view in which the important control flow remains visible while implementation details can be collapsed when they are not needed.
For teams working across Sydney, Melbourne, Brisbane or Perth, this matters during screen-sharing sessions and handovers between consultants. A developer in Adelaide may need to inspect a production issue with someone in Canberra, while a Melbourne team may be working around a customer’s narrow support window. A consistent folding strategy keeps those conversations practical, even when the method is unfamiliar.
| Folding strategy | Best use | Main benefit | Main risk |
|---|---|---|---|
| Method-level folding | Moving between methods | Quickly reduces a large file | Hides too much context |
| Structural block folding | Loops, conditionals and protection blocks | Preserves the algorithm’s shape | Depends on accurate Magik syntax support |
| Responsibility-based regions | Database, validation and presentation work | Matches how developers reason about the method | Artificial regions can become stale |
| Debugging-focused folding | Investigating one failing path | Keeps attention on active code | Can obscure setup and cleanup |
| Minimal folding | Short or well-factored methods | Maintains full visibility | Less useful for legacy code |
Start with the method boundary
The first fold should usually be the entire method body. In a Magik source file, method declarations and their corresponding endings provide a natural navigation boundary. A Magik-aware Emacs mode can use that structure to recognise where a method begins and ends, allowing the developer to collapse neighbouring methods while keeping the current method open.
This is particularly useful in files containing many related methods. Instead of scrolling past accessors, constructors, collection helpers and database utilities, fold the methods that are not part of the current task. The resulting buffer becomes a compact index of the class or mixin. Method names remain visible, so the file still communicates its public and internal responsibilities.
Avoid opening every method automatically when the buffer is first visited. An all-open file encourages vertical scrolling and makes it harder to see the relationship between declarations. A better default is to expose method names and fold their bodies, then open only the method under review.
When a method is genuinely large, keep its declaration and first few lines visible. Initialisation, argument checks and the main local variables often explain the method’s purpose. Folding from the first executable statement can remove useful orientation information and make a collapsed method look more mysterious than it needs to be.
Fold around meaningful control flow
Once the method boundary is under control, fold the major structural blocks inside it. Magik code commonly contains conditional branches, loops, protected operations, blocks and procedure-like sections. These constructs are more useful folding targets than arbitrary groups of lines because their boundaries correspond to decisions in the algorithm.
A good folded view should reveal the method’s skeleton. A reviewer ought to see a validation branch, a query, a loop over returned records and a cleanup or reporting step without opening every statement. If all of those elements collapse into anonymous regions, the outline loses its value. Region labels should communicate purpose where the editor supports them; otherwise, the first visible line inside a block should be descriptive enough.
Do not fold every nested conditional by default. Three levels of collapsed blocks can turn a readable sequence into a row of indistinguishable markers. Keep the outer branch open and fold the detailed branches beneath it, or open the path currently being tested. This gives the developer a useful balance between overview and local detail.
Indentation is part of the strategy. Folding commands depend on the editor recognising nesting accurately, and inconsistent indentation makes both visual navigation and structural parsing less dependable. In legacy Magik, tidy indentation before reviewing folds can reveal an incorrectly closed block, an accidental extra level or a branch that has grown beyond its intended role.
Use responsibility-based regions carefully
Large methods often contain several kinds of work that are technically valid but conceptually separate. A single method might read records, normalise values, build a collection, apply business rules and update a display. Folding by responsibility lets a developer inspect one concern at a time while retaining the broad sequence.
For example, a method may be easier to operate when its visible shape is:
validate inputs
load source records
transform and filter values
apply domain rules
publish results
handle cleanup
The exact implementation remains in the source, but the working view communicates the method’s responsibilities. This makes folding useful during code review, where a reviewer can open the transformation section without losing sight of validation and cleanup.
Use such regions only when they correspond to real boundaries in the code. Invented headings that do not match the implementation become misleading after the next change. If a developer moves half of the database logic into the validation section, the folds may still appear neat while the structure becomes harder to trust.
Visual markers can help when a team uses them consistently. Developers interested in how text can be represented graphically may find this visual text reference useful as a reminder that presentation should clarify structure rather than decorate it. In Emacs, the practical equivalent is a restrained set of meaningful headings, faces or fold indicators.
For Australian consulting teams, this restraint pays off when code is passed between a permanent employee and an external specialist. A short-lived region label such as “temporary fix” can confuse someone reviewing the method six months later. Labels should describe stable responsibilities: “load network records”, “validate geometry” or “commit changes” will age better.
Make debugging folds task-specific
Folding is most valuable during debugging when it reduces unrelated detail without concealing the state changes that matter. Begin by opening the method boundary, the setup code and the suspected branch. Fold helper calls, repetitive collection operations and output formatting until the failing path reads as a short sequence.
Keep exception, protection and cleanup blocks visible when a failure could be caused by resource handling. A collapsed protected section may hide the statement that releases a lock, closes a transaction or restores an application state. In Magik systems connected to long-running applications or operational databases, these details can matter as much as the original error.
When using Emacs debugging tools, coordinate the fold state with the current point and call stack. If stepping enters a folded region, open that region before evaluating expressions or inspecting objects. The editor should follow the debugging question: open the code needed to understand the current values, and fold code that cannot affect the current hypothesis.
Object inspection also benefits from a stable visual frame. Keep the method declaration, key arguments and the variable that carries the problematic collection visible. Then fold the mechanics that populate it. This prevents the common debugging mistake of losing track of whether a value was sourced from a query, transformed by a loop or replaced by a fallback branch.
Do not treat a fold as evidence that a block is unimportant. A folded database call may still determine the contents of every object inspected later. Folding changes visibility, not execution. Developers should reopen a block before making a behavioural change, especially when working on a remote production issue late in the afternoon in Perth or during a support window aligned with eastern-state offices.
Build a repeatable Emacs workflow
A practical workflow begins with predictable commands. Use one command or key binding for method-level folding, another for opening the current block, and a third for opening everything when necessary. The exact bindings depend on the Magik Emacs setup, but the mental model should stay stable across projects.
Start a review with the file folded to method boundaries. Open the target method, then reveal its outer control-flow blocks. Next, open only the branch or responsibility being changed. Before saving, unfold the method and inspect its full indentation and endings. This final pass catches edits made in a context where several surrounding blocks were hidden.
Keep folding state separate from source conventions. Fold indicators and editor configuration belong to the developer’s working environment, while comments and method names belong to the shared codebase. A colleague using a different Emacs configuration should still understand the method when all folds are open.
Teams can document a small set of conventions in their Magik development notes. For instance, method names should be specific enough to remain useful when bodies are folded, major responsibility boundaries should be visible in the first line of a block, and deeply nested logic should be refactored rather than permanently hidden. These conventions support both local developers and consultants brought in for a discrete Smallworld engagement.
Folding also exposes design problems. If a method requires ten separate folds before its intent becomes clear, the right response may be extraction into helper methods. The fold view is then a diagnostic tool: it shows whether the method has a coherent narrative or whether unrelated operations have accumulated over time.
A reliable strategy is therefore selective rather than maximal. Fold what is repetitive, stable or outside the current question. Leave visible the declarations, major decisions, data movement and cleanup. With a Magik-aware Emacs configuration, this approach turns a long method into a navigable working map while preserving access to every statement.
Set up the folding commands in your Magik Emacs environment, try the workflow on one legacy method, and record the conventions that help your team navigate it fastest. HydePark Consulting can also help teams working with Magik, Emacs and Smallworld development establish practical coding and debugging workflows that suit their existing codebase.
Core Features
Tab Mode & ECB
Quick tab switching and Emacs Code Browsing mode for navigating Magik codebases efficiently.
Magik Smeller
Code analysis tool that helps identify potential issues in Magik source files.
Code Folding
Hide/Show mode for collapsing and expanding Magik code blocks to focus on what matters.
Visual Bookmarks
Quick visual bookmarks for jumping between key locations in your Smallworld session buffers.
Object Inspector
Inspect Magik objects and display them in an Emacs Deep Print buffer for detailed examination.
Magik Debugger
Set breakpoints and monitor slots and variables directly from within Emacs.
Development Tools
Direct links between Emacs and the Smallworld Development Tools application, including Click Monitor.
Screen-casts & Tutorials
Screen-cast 1: Tab Mode, ECB & More
Covers tab-mode, ECB, Magik Smeller, code folding, visual bookmarks, pragma toggling, moving code, external editor, and MS Explorer.
Screen-cast 5: Object Inspection & Deep Print
Inspect a Magik object, prompt for an expression evaluated within a Smallworld session, and display results in a Deep Print buffer.
Screen-cast 7: Magik Debugger
Useful tools for application developers: Object Inspector and Magik Debugger with breakpoints and slot/variable monitoring.
Screen-cast 9: Tree Item GUI Control
Tree Item is a GUI control providing extensive facilities for displaying lists with rows, columns, trees, and in-place editing.